2025年10月18日 星期六

How to change the Ownership and Permissions of a computer file/folder/directory

 

Ownership and Permission



In computer operations, there is a device to ensure that personal files (like documents, folders, and hard disks) are kept private from others and prevent them from accessing, modifying, or deleting any data.



To do this, the concept of ‘owners’ and ‘permissions’ are being employed. So, every file or directory has an "owner" (usually the user who created it) and a "group".



For example, when I right click on a folder (Guitar), and choose ‘Properties’ and then select the ‘Permissions’ tab, the following will result:


There are 3 fields for ‘Access Permissions’ and 2 fields for ‘Ownership’. It suggests that the owner is ‘kai’, who can vew & modify content of the folder. ‘Others’ and ‘Group’ (a member of it) can only view the content but cannot modify it.



We may also have situations as the one below:


where the ownship is ‘root’ (the administrator), and no one is permitted to view or modify the content.



There are times that one may want or need to change the Ownership or Permissions.



In the linux environment, you can use the command chown to change the Ownership (as aresult, the new owner will have all the Permissions).

For example, to change the owner of ‘myfile.txt’ to 'john' AND the group to 'john', the syntax is like this:



  sudo chown john:john myfile.txt

 or

  sudo chown john:users myfile.txt

 

To change ownership recursively for an entire directory and all its contents:

 

  sudo chown -R john:john /path/to/directory/



If you to change the ‘Permissions’ only use the command chmod.



This command keeps the ‘Ownership’ unchanged but change only the 3 main Permissions:

- Read

- Write

- Execute



The chmod command is used with a symbolic or numeric (octal) mode.



The following symbols are used for different catagories of users and Permissions:

· u = user/owner

· g = group

· o = others (any other user on the system)

· a = all (equivalent to u+g+o)



· r = read

· w = write

· x = execute



You add (+) or remove (-) permissions.



Examples:



Give read and write permission to "others" (any user):



  sudo chmod o+rw myfile.txt



Give read, write, and execute permission to "others":

 

  sudo chmod o+rwx myscript.sh



Give read and execute permission to "others" for a directory/drive (so they can list its contents):



  sudo chmod o+rx /path/to/directory/



Set the permissions for "others" to be exactly read and write (and remove execute):

 

  sudo chmod o=rw myfile.txt



Make a file readable and writable by EVERYONE (owner, group, and others):

 

  sudo chmod a+rw myfile.txt



2025年10月12日 星期日

使用 efibootmgr 管理 UEFI 的启动项

 使用 efibootmgr 管理 UEFI 启动项

我喜歡在電腦上同時安裝多個作業系統,來嘗試和比較各個作業系統不同之處,例如我現在就裝了4個作業系統在我的電腦上,計有 WIN 11, Arch Linux, KDE Neon 和 EndeavourOS。除了 WIN 11外,我是會不時更換那些 Linux OS。

當我經常更換那些 Linux 作業系統時,問題就出現了:當一個 Linux 業系統被刪除後,它的引導程式(GRUB)仍會留在 UEFI Partition 上。UEFI Partition 只得 100 M 很快就會用完,導致可能開不了機。

幸好,在 Linux 上有一個應用,efibootmgr,可以用來處理這方面的問題。

開啟 Terminal  (Ctl-Alt-T) ,輸入 efibootmgr, 便會得出下圖:

這是我部電腦上的 UEFI 內容,每个系统都在EFI分區建立了自己的啟動文件。所有都是我現已裝的,或是曾經安裝過的 OS 啟動文件,計有 MX Linux, KDE Neon, Arch Linux, Ubuntu, EndeavourOS, Garuda Linux。最離譜的是 EndeavourOS, 它每升級一次便制做另一個新的的啟動文件,所以便有 EndeavourOS-7279,  EndeavourOS-6250,  EndeavourOS-2473 等,要刪除時須要特別小心。

從上圖我們還可看到開機時的 Boot Order、次序首先是 Arch Linux (0005),其次是 Windows Boot Manager (0000) ,如此類推。如果想將次序改變,我們輸入類似如下的命令

sudo efibootmgr -o 0000, 0005, 0003, 0004, ....

這樣便會將 Boot Order 改為 Windows Boot Manager (0000) 第一,其次才到 Arch Linux (0005),.....。


​如想刪除 UEFI 多餘的啟動項?我們可以輸入類似如下的命令

sudo efibootmgr -b 000A -B        #刪除 Garuda (000A)

sudo efibootmgr -b 0008 -B        #刪除 endeavouros-6250 (0008)

 

刪除完了以上的啟動項後,再輸入命令 efibootmgr 便得下圖︓

註︓

如想參閱 efibootmgr 的詳細用法,我們只​要輸入以下命令:

 efibootmgr -h

 

刪除完啟動項後,其實還有很多相關而不再用的 folder 存在 EFI folder 里面,最好便是一併清除掉。以下是 ARCH LINUX 的做法。

我的 EFI FOLDER 是在  /dev/nvme0n1p1 ,於是我需要輸入以下命令:

sudo mount /dev/nvme0n1p1 /mnt

dolphin --sudo mnt

以下視窗便會彈出:


Click  'Act as Administrator' --> 輸入密碼後:

 

開啟 EFI folder:


 便會看見所有我曾經安裝過的 OS 的 folder。刪除那些不再需要的 OS folder 便可。


如果以上方法行不通,我們可以利用 Command line 的方法:

因為 EFI FOLDER 是在  dev/nvme0n1p1 內,苜先將 dev/nvme0n1p1 mount 成 mnt

 

sudo mount /dev/nvme0n1p1 /mnt 

 

再將 EFI FOLDER 的內容列出來:

sudo ls -l /mnt/efi  (or sudo ls /mnt/efi)

煩似結果會如下: 

total 108
drwx------ 2 root root 4096 Oct  2 22:04 AcreetionOS
drwx------ 2 root root 4096 Sep  1 12:29 anduinos
drwx------ 2 root root 4096 Jun 11 18:37 Arch
drwx------ 2 root root 4096 Sep 20 17:44 archcraft
drwx------ 2 root root 4096 Mar 18  2025 Artix
drwx------ 2 root root 4096 Sep 14 20:47 BigLinux
drwx------ 2 root root 4096 Jul 21 12:13 BlissOS
drwx------ 2 root root 4096 Sep 23 21:40 boot
drwx------ 2 root root 4096 May 23 21:03 cachyos
drwx------ 2 root root 4096 Aug 31 12:28 debian
drwx------ 2 root root 4096 Feb  9  2025 endeavouros
drwx------ 2 root root 4096 Sep  8 10:24 fedora
drwx------ 2 root root 4096 Sep  8 09:58 Kamuriki
drwx------ 2 root root 4096 Sep 23 21:40 Linux
drwx------ 2 root root 4096 Jul 23 21:17 Mabox
drwx------ 2 root root 4096 Aug 25 02:28 Manjaro
drwx------ 4 root root 4096 Feb 28  2025 Microsoft
drwx------ 2 root root 4096 Sep  1 02:15 MX
drwx------ 3 root root 4096 Sep 23 21:40 nixos
drwx------ 2 root root 4096 Sep 11 17:43 Parch
drwx------ 2 root root 4096 Sep 12 01:22 parchlinux
drwx------ 2 root root 4096 Sep 12 01:56 Q4OS_Aquarius
drwx------ 7 root root 4096 Oct 10 13:35 refind
drwx------ 2 root root 4096 Sep 23 21:40 systemd
drwx------ 2 root root 4096 Jul 15 12:11 tools
drwx------ 2 root root 4096 Jul 17 10:07 ubuntu
drwx------ 2 root root 4096 Sep  7 05:29 XIVAStudio

假如我想將 nixOS Folder 清除,我可輸入以下指令。

sudo rm -r /mnt/efi/nixos

再將 EFI FOLDER 的內容列出來, nixOS Folder 便再也不存在。其他的就如此煩推。