跳至內容

LPI Linux 認證/管理檔案許可權和所有權

來自華夏公益教科書,開放書籍,開放世界

詳細目標

[編輯 | 編輯原始碼]

(LPIC-1 版本 5.0)

權重: 3

描述
考生應該能夠透過正確使用許可權和所有權來控制檔案訪問。

關鍵知識領域

  • 管理常規檔案和特殊檔案以及目錄的訪問許可權。
  • 使用 suid、sgid 和粘滯位等訪問模式來維護安全。
  • 瞭解如何更改檔案建立掩碼。
  • 使用組欄位授予組成員檔案訪問許可權。

以下是使用過的檔案、術語和實用程式的部分列表

  • chmod
  • umask
  • chown
  • chgrp

更改檔案所有者和組

[編輯 | 編輯原始碼]

要更改檔案或目錄的所有者,請使用 chown。

chown yann mon_fichier.txt

要更改檔案或目錄的組,請使用 chgrp。

chgrp dialout caller

gpasswd 和 yast2 程式允許您管理組。

gpasswd [-A 使用者名稱,...] [-M 使用者名稱,...] 組名

-A: 新增具有組管理員許可權的使用者。
-M: 新增組成員。

組管理員可以新增或刪除組成員

gpasswd -d toto users
gpasswd -a toto users

組管理員可以設定或刪除組的密碼。

gpasswd users
gpasswd -r users

更多許可權

[編輯 | 編輯原始碼]

當用戶執行特定指令碼或程式時,可以透過設定檔案的 uid 或 gid 位來賦予使用者更多許可權。

如果設定了該位,程序將繼承檔案所有者的許可權,而不是使用者的許可權。要設定有效 uid 或 gid,請使用 chmod。

chmod 2640 [file] # (2) gid is inheritable for group.
chmod 4640 [file] # (4) uid is inheritable for user.

/bin/passwd 是此類程式的示例。

粘滯位也可以設定,可以使程式文字段駐留在 RAM 中。chmod 1640 [檔案] (1) 檔案程式保留在 RAM 中。


檔案和目錄許可權

[編輯 | 編輯原始碼]

可以使用 ls -l 檢視檔案或目錄的許可權。

檔案許可權示例

ls -l readme
-rwxrw---- 1 toto users 14 Jul 5 10:00 readme

這意味著使用者 toto 具有讀、寫和執行許可權,組 users 的成員具有讀和寫許可權。其他人沒有許可權。(0760)

ls -l /etc/hosts
-rw-r--r-- 1 root root 14 Jul 5 10:00 /etc/hosts

這意味著使用者 root 具有讀和寫許可權,組 root 的成員和所有其他人具有讀許可權。(0644)

目錄許可權示例

ls -ld /bin
drwxr-xr-x 2 root root 4096 Jul 5 10:00 /bin

這意味著使用者 root 具有讀、寫和執行許可權,組 root 的成員和其他人具有讀和執行許可權。(0755)

ls -l /home/toto
drwxr-xr-x 10 toto  users 4096 Jul 5 1:00 /home/toto

這意味著使用者 toto 具有讀、寫和執行許可權,組 users 的成員和其他人具有讀和執行許可權。(0755)

預設許可權

[編輯 | 編輯原始碼]

建立檔案的預設許可權為 0666,建立目錄的預設許可權為 0777。大多數系統在啟動時使用 umask 程式覆蓋此許可權。通常,掩碼值為 022。這意味著組和其他的寫許可權將被阻止。要檢查或更改掩碼值,請執行

umask 
umask 066  

檔案示例

default: rw- rw- rw- (0666)
umask: 0 2 2 (0022) Block
result: rw- r-- r-- (0644)

目錄示例

default: rwx rwx rwx (0777)
umask: 0 2 2 (0022) Block
result: rwx r-x r-x (0755)

計算 umask

[編輯 | 編輯原始碼]

找到正確的 umask 並非易事,但 certainly 是可以做到的。檔案的最終許可權是 umask 的否定和預設許可權的邏輯與運算的結果。(目錄也是如此)

為了視覺化這一點,我們首先將八進位制預設許可權轉換為二進位制形式

 octal: 0666
 binary: 000 110 110 110
 octal: 0777
 binary: 000 111 111 111

然後我們取 umask。這次我們將使用 0027 作為我們的 umask 並將其轉換為二進位制。然後將其反轉 (~)。

 octal:  0027
 binary: 000 000 010 111
 ~:      111 111 101 000

現在,要獲得檔案的實際許可權,我們將其與預設許可權進行邏輯與運算,然後轉換回八進位制

 default permission: 000 110 110 110
 ~ umask:            111 111 101 000
 logical AND:        000 110 100 000
 octal representation: 0640

目錄也是如此

 default permission: 000 111 111 111
 ~ umask:             111 111 101 000
 logical AND:        000 111 101 000
 octal representation: 0750

更改檔案許可權

[編輯 | 編輯原始碼]

要更改檔案或目錄的許可權,請使用 chmod。要覆蓋現有許可權,請執行

chmod 0755 /tmp #rwx for user, rx for group and others

要更改新增或取消一些許可權而不覆蓋所有現有許可權,請執行

chmod u+w readme  # Add write permission for user
chmod +r readme  # Add read permission for everybody
chmod -r readme  # Remove read permission for everybody
chmod u+x,g=r readme  # Add execution for user and set read for group
chmod u=rwx,go=rx readme  # Set read write and execution for user, read and execution for group and others

要在遞迴模式下更改,請使用 -R 選項。

chmod -R +x /sbin/*


1) 使用字母編寫 chmod 命令列來設定以下許可權

rwxrwxr-x :
rwxr--r-- :
r--r----- :
rwxr-xr-x :
rwxr-xr-x :
r-x--x--x :
-w-r----x :
-----xrwx :

2) 使用八進位制數字編寫 chmod 命令列來設定以下許可權

rwxrwxrwx :
--x--x--x :
r---w---x :
-w------- :
rw-r----- :
rwx--x--x :

3) 對於以下 umask 值,檔案和目錄的建立許可權將是什麼?

umask = 0027
File permissions:
Directory permissions:
umask = 0011
File permissions:
Directory permissions:
umask = 0541
File permissions:
Directory permissions:
umask = 0777
File permissions:
Directory permissions:

4) 建立兩個使用者帳戶

登入 ID: tst1,組 users,使用 bash shell,主目錄 /home/tst1
登入 ID: tst2,組 public,使用 bash shell,主目錄 /home/tst2
為這兩個帳戶設定密碼。

以 tst1 使用者身份登入並複製 /bin/ls 到 tst1 的主目錄中,命名為 myls。將 myls 的所有者更改為 tst1,並將許可權更改為 0710。此許可權值意味著什麼?

以 tst2 使用者身份登入並嘗試使用 /home/tst1/myls 列出當前目錄。它能正常工作嗎?

在 /etc/group 和 /etc/gshadow 中建立一個新的組 labo,包含 tst1 和 tst2。將 myls 的所有者組更改為 labo。

再次從 tst2 帳戶嘗試執行 /home/tst1/myls 來列出當前目錄。它能正常工作嗎?


練習結果

華夏公益教科書