用户和用户組

出自 Arch Linux 中文维基

GNU/Linux 通過用户和用户組實現訪問控制 —— 包括對文件訪問、設備使用的控制。Linux 默認的訪問控制機制相對簡單直接,不過還有一些更加高級的機制,包括 ACLCapabilitiesLDAP authentication.

概覽[編輯 | 編輯原始碼]

用户一般指使用計算機的人。在本文語境中,該詞指用來識別用户的用户名稱,既可以是 Mary 或 Bill 這樣的真名,也可以是 Dragonlady, Pirate 這樣的暱稱。關鍵是,計算機給每個賬户分配了特定的名稱,而用户則使用這些名稱訪問計算機。除了人之外,一些系統服務也以有部分限制,由享有部分特權的用户賬户身份運行。

由於安全需要,「用户管理」應運而生,以加以明確限制各個用户賬户的權限。 超級用户 root 於計算機裡擁有至高無上的管理權限,所以一般只作管理用。非特權用户則可以用 susudo 程序以臨時獲得特權。

個體可以擁有多賬户,只不過彼此名稱當然不同。但有一些用户名稱已事先被系統佔用,比如 "root".

此外,任意用户可能從屬某個「用户組」。此外用户也能夠新加入某些已經存在的用户組,以獲取該組所擁有的特權。

注意: 新手請務必謹慎地使用這方面的工具,並且要避免對除自己以外的其他已存在用户發生誤操作。
提示:「root」重新導向至此。關於「根目錄」的內容,參見分區#根分區

權限與屬主[編輯 | 編輯原始碼]

摘自 In UNIX Everything is a File (UNIX 中一切皆文件)

集眾多靈感及理念之大成,UNIX 作業系統打造出了它的設計、接口、文化甚至革新。重中之重,有一句道理:「一切皆文件」可謂 UNIX 的真諦之一。
根據這一設計原則,必須要有統一的模型,用以管理對大量 I/O 資源的訪問:文檔、目錄、磁盤、CD-ROM、調製解調器、鍵盤、打印機、顯示器和終端等等,甚至也包括了進程、網絡之間的通信。而解決之策,就是為所有這些資源提成一個抽象層,UNIX 之父們稱之為「文件」。所有文件都通過一致的 API 以提供訪問,因此光只用同一套簡單的命令,就可以讀寫磁盤、鍵盤、文檔以及網絡設備。

摘自 Extending UNIX File Abstraction for General-Purpose Networking (針對常規的網絡應用,擴展出 UNIX 文件抽象層)

UNIX 及兼容系統提供了一個即基本又強悍的抽象層——文件。很多系統服務和設備的應用程式接口,一開始都被設計為文件或文件系統之類的東西。這賦予程序全新的姿態——通過文件抽象層,我們就可以以全新的方式使用眾多現成的、且用途單一的小工具。例如 cat 工具,原本只用來讀取文件,再將其內容發送到標準輸出,但現在它也可以直接訪問特殊設備文件(通常在 /dev 目錄中),加以讀取 I/O 設備。在很多系統上,音頻記錄和播放也可以通過一令執行:分別是 cat /dev/audio > myfile 以及 cat myfile > /dev/audio.

GNU/Linux 系統中的每一個文件都從屬一個用户(屬主)和一個用户組(屬組)。另外,還有三種類型的訪問權限:讀(read)、寫(write)、運行(execute)。我們可以針對文件的屬主、屬組、而設置相應的訪問權限。再次,我們可以通過 ls 命令的長列表格式以查詢文件屬主、屬組和權限:

$ ls /boot/ -l
total 18492
drwxr-xr-x 3 root root    12288 Aug 21 03:27 grub
-rw-r--r-- 1 root root 12487150 Aug 29 18:24 initramfs-linux-fallback.img
-rw-r--r-- 1 root root  2990626 Aug 29 18:23 initramfs-linux.img
-rw-r--r-- 1 root root  3440576 Aug 26 15:17 vmlinuz-linux 

第一列是文件訪問權限(例如,文件initramfs-linux.img的權限為-rw-r--r--),第三列和第四列分別是屬主和屬組(本例中所有文件屬主都是root用户,屬組都是root組)。

$ ls -l /media/
total 16
drwxrwx--- 1 root vboxsf 16384 Jan 29 11:02 sf_Shared

上述例子中,sf_Shared目錄由root用户和vboxsf組所有。使用stat命令也可以查看文件所有權和權限:

屬主:

$ stat -c %U /media/sf_Shared/
root

屬組:

$ stat -c %G /media/sf_Shared/
vboxsf

訪問權限:

$ stat -c %A /media/sf_Shared/
drwxrwx---

訪問權限由三組字符組成,分別代表屬主、屬組、其他人的權限。例如,-rw-r--r--表示屬主有讀寫權限、但無運行權限(rw-),屬組用户和其他用户只有讀取權限(r--)。又如,drwxrwx---表示文件屬主和屬組用户有讀、寫、執行權限(rwx),而禁止其他用户任何訪問(---)。第一個字符」d「代表文件類型(目錄)。

通過find命令查找屬於某個用户或某個組的文件:

# find / -group "用户组名"
# find / -group "用户组编号"
# find / -user "用户"

文件的屬主、屬組可以通過chown命令更改。文件的權限可以通過chmod命令修改。

詳情參見:chown(1)chmod(1)Linux 文件權限

Shadow[編輯 | 編輯原始碼]

Arch Linux 的用户、羣組和密碼管理工具由 shadow 提供,這個軟件包是 base 元軟件包的依賴。

文件列表[編輯 | 編輯原始碼]

警吿: 不要手動編輯這些文件。有些工具可以更好的處理鎖定、避免數據庫錯誤。
文件 作用
/etc/shadow 保存用户安全信息
/etc/passwd 用户賬户信息
/etc/gshadow 保存組賬號的安全信息
/etc/group 定義用户所屬的組
/etc/sudoers 可以運行 sudo 的用户
/home/* 主目錄

用户管理[編輯 | 編輯原始碼]

使用who命令,可以查看目前已登錄的用户。要查看系統上的用户,以 root 執行 passwd -Sa 輸出的數據格式可以參考 passwd(1)

使用useradd命令添加用户:

# useradd -m -G "附加组" -s "登录shell" "用户"
  • -m/--create-home:創建用户主目錄/home/[用户名];在自己的主目錄內,即使不是root用户也可以讀寫文件、安裝程序等等。
  • -G/--groups:用户要加入的附加組列表;使用逗號分隔多個組,不要添加空格;如果不設置,用户僅僅加入初始組。
  • -s/--shell:用户默認登錄shell的路徑;啟動過程結束後,默認啟動的登錄shell在此處設定;請確保使用的shell已經安裝,默認是 Bash
警吿: 為了登錄,登錄 shell 必須位於 /etc/shells 中, 否則 PAMpam_shell 模塊會阻止登錄請求。不要使用 /usr/bin/bash 替代 /bin/bash, 除非這個路徑已經在 /etc/shells中正確配置.
  • 有時候需要禁止某些用户執行登錄動作,例如用來執行系統服務的用户。將shell設置成 /usr/bin/nologin 就可以禁止用户登錄。(nologin(8)).
  • 需要用 passwd 為新用户設置密碼。
  • 如果設置了用户初始組的名稱或數字ID;該組必須是存在的;如果沒有設置該選項,useradd會根據/etc/login.defs文件中的USERGROUPS_ENAB環境變量進行設置。默認(USERGROUPS_ENAB yes) 會用和用户名相同的名字創建羣組,GID 等於 UID

更多選項説明請查看 useradd(8)

添加登錄用户[編輯 | 編輯原始碼]

要用默認設置添加一個名為archie的用户:

# useradd -m archie
提示:使用 useradd --defaults 可以查看 shell 的默認值。默認是 Bash。使用 -s/--shell 選項可以設置其他值。/etc/shells 記錄了可以使用的登錄 shell。

此命令會自動創建 archie 羣組,並成為 archie 的默認登錄羣組。建議每一個用户都設置自己的默認羣組,因為umask 默認值是 002, 所以同一個默認羣組的用户會有創建文件的寫權限。參閱 User Private Groups[失效連結 2021-11-19 ⓘ]

要賦予一個羣組某個目錄的寫權限,可以在父目錄中設置:

# chmod g+s our_shared_directory

通過下列命令設置用户密碼,雖然不是必須的,還是強烈建議設置密碼

# passwd [用户名]

If a GID change is required temporarily you can also use the newgrp command to change the user's default GID to another GID at runtime. For example, after executing newgrp groupname files created by the user will be associated with the groupname GID, without requiring a re-login. To change back to the default GID, execute newgrp without a groupname.

添加系統用户[編輯 | 編輯原始碼]

為進程、守護進程分配不同的系統用户可以更安全的管控目錄及文件的訪問。下面命令創建一個不創建 home 目錄的非登錄用户(可以加入 -U 參數創建一個和用户名相同的羣組):

# useradd -r -s /usr/bin/nologin username

If the system user requires a specific user and group ID, specify them with the -u/--uid and -g/--gid options when creating the user:

# useradd -r -u 850 -g 850 -s /usr/bin/nologin username

修改登錄名或主目錄[編輯 | 編輯原始碼]

更改用户主目錄:

# usermod -d /my/new/home -m username

-m 選項會自動創建新目錄並移動內容。

更改用户登錄名:

# usermod -l newname oldname
警吿: 確保你不是使用你要修改的用户名登錄,同時按下(Ctrl+Alt+F1)打開一個新的終端,使用root用户登錄,或用其他用户登錄後使用su命令登錄為root用户。

操作得當的話,在Arch(或其他Linux發行版)中更改用户名是安全的,並且很簡單。你可以更改用户所屬的組。按照以下步驟進行,可以保留受影響用户的UID和GID,而不會搞亂你已經設置好的文件權限。還有一種方法是手動編輯 /etc/passwd 文件。

  • 如果要使用sudo,請更新文件/etc/sudoers把新的用户(以root登錄使用visudo命令)添加進去。
  • 如果修改了~/.bashrc的PATH環境變量,並把新的用户添加進去。。
  • 更改用户名後,我不得不重新安裝Thunderbird擴展(Enigmail)。
  • 系統(桌面快捷方式,腳本等)裡使用了舊的用户主目錄的地方,都需要進行修改。要在腳本中避免這樣的問題,可以使用~$HOME變量來表示主目錄。

其他用户管理示例[編輯 | 編輯原始碼]

將用户加入 羣組,用逗號分隔:

# usermod -aG 群组 username
警吿: 如果不使用 -a 選項,用户會離開沒有列在羣組的其它羣組。

要修改用户的登錄 shell:

# usermod -s /bin/bash username

通過下列命令設置GECOS字段(用户信息,例如用户全名):

# chfn [用户名]

(這樣將會以交互式模式啟動chfn

此外,可以設置 GECOS comment:

# usermod -c "Comment" username

使用userdel命令刪除用户:

# userdel -r [用户名]

-r選項表示一併刪除用户主目錄和郵件。

提示:adduserAUR 可以以交互的方式執行 useradd, chfnpasswd,參考 FS#32893.

用户信息存儲[編輯 | 編輯原始碼]

本地用户信息儲存在/etc/passwd文件中。要查看系統上所有用户賬户:

$ cat /etc/passwd

一行代表一個用户,格式如下,每行分七個部分,用英文冒號「:」分開:

account:password:UID:GID:GECOS:directory:shell

此處:

  • account:用户名,不能為空,而且要符合標準的*NIX命名規則。
  • password:加密的密碼,可以使用一個小寫的"x"(不帶括號)表示密碼保存在/etc/shadow文件裡。
  • UID GID:每個用户和組有一個對應的UID和GID(用户ID和組ID)。Arch裏面,第一個非 root 普通用户的默認 UID 是 1000,後續創建的用户UID也應大於1000,特定用户的GID應該屬於指定的首要組,組的ID數值列在/etc/group文件裡。
  • GECOS:可選的註釋字段,通常記錄用户全名
  • directory:用於登錄命令設置$HOME環境變量。某些服務的用户主目錄設置為"/"是安全的,但不建議普通用户設置為此目錄。
  • shell:是用户默認登錄的shell,通常是Bash,還可選擇其他的命令解釋器,默認是"/bin/bash"(不帶括號),如果你用的是別的shell,在這裏設置其路徑,此部分是可選的,可留空。
注意: Arch Linux 使用影子密碼。passwd文件對所有人可讀,在裏面存儲密碼(無論是否加密過)是很不安全的。在password字段,通常使用一個佔位字符(x)代替。加密過的密碼儲存在/etc/shadow文件,該文件對普通用户限制訪問。

示例:

jack:x:1001:1003:Jack Smith,some comment here,,:/home/jack:/bin/bash

示例分解説明:用户登錄名為jack,密碼保存在/etc/shadow,UID為1001,首要組的ID是1003 (users組),全名Jack Smith並加了一些註釋,主目錄是/home/jack,使用Bash作為默認shell。

The pwck command can be used to verify the integrity of the user database. It can sort the user list by GID at the same time, which can be helpful for comparison:

# pwck -s

Arch Linux defaults of the files are created as .pacnew files by new releases of the filesystem package. Unless Pacman outputs related messages for action, these .pacnew files can, and should, be disregarded/removed. New required default users and groups are added or re-added as needed by systemd-sysusers(8) or the package install script.

用户組管理[編輯 | 編輯原始碼]

/etc/group文件儲存了系統中用户組的信息,詳情參見:group(5)。還有一個很少使用的 gshadow,詳情請參考 gshadow(5).

使用groups命令查看用户所在組的名稱:

$ groups [用户名]

若省略用户名,默認顯示當前用户所在組。

id命令提供額外的信息,包括用户UID以及相關用户組GID:

$ id [用户名]

查看所有組:

$ cat /etc/group

使用groupadd創建新的組:

# groupadd [组名]

使用gpasswd將用户添加到組:

# gpasswd -a [用户名] [组名]

更改用户所屬的組名,不變更GID

# groupmod -n newname oldname

刪除用户組:

# groupdel [组名]

將用户從組中移除:

# gpasswd -d [用户名] [组名]

如果用户已登錄,必須重新登錄使更改生效。

The grpck command can be used to verify the integrity of the system's group files.

警吿: Arch Linux defaults of the files are created as .pacnew files by new releases of the filesystem package. Unless Pacman outputs related messages for action, these .pacnew files can, and should, be disregarded/removed. New required default users and groups are added or re-added as needed by systemd-sysusers(8) or the package install script.

羣組列表[編輯 | 編輯原始碼]

This section explains the purpose of the essential groups from the filesystem package. There are many other groups, which will be created with correct GID when the relevant package is installed. See the main page for the software for details.

注意: A later removal of a package does not remove the automatically created user/group (UID/GID) again. This is intentional because any files created during its usage would otherwise be left orphaned as a potential security risk.

用户組[編輯 | 編輯原始碼]

影響文件 作用
adm 通常用來給予系統日誌讀權限,可以讀取 journal 文件。
ftp /srv/ftp/ 訪問 FTP 伺服器.
games /var/games 訪問一些遊戲。
log 訪問 syslog-ng 創建的 /var/log/ 日誌文件.
http /srv/http/ 訪問 HTTP 伺服器文件.
rfkill 不再使用! 控制無線設備的電源 (被 rfkill 使用).
sys Right to administer printers in CUPS.
systemd-journal /var/log/journal/* 以只讀方式訪問系統日誌,和 admwheel 不同 [1]. 不在此組中的用户僅能訪問自己生成的信息。
users 標準用户組.
uucp /dev/ttyS[0-9]+, /dev/tts/[0-9]+, /dev/ttyUSB[0-9]+, /dev/ttyACM[0-9]+, /dev/rfcomm[0-9]+ 串口和 USB 設備,例如貓、手柄 RS-232/串口。
wheel 管理組,可以訪問 journal 文件和 CUPS 打印服務,可以用户 sudo 和 su 命令權限管理(需要額外設置)。

系統組[編輯 | 編輯原始碼]

下列組系統使用,一般不被 Arch 用户使用:

影響文件 作用
avahi
clamav /var/lib/clamav/*, /var/log/clamav/* Clam AntiVirus 使用.
dbus /var/run/dbus/*
kmem /dev/port, /dev/mem, /dev/kmem
locate /usr/bin/locate, /var/lib/locate, /var/lib/mlocate, /var/lib/slocate SeeLocate.
lp /dev/lp[0-9]*, /dev/parport[0-9]* Access to parallel port devices (printers and others).
mail /usr/bin/mail
mpd /var/lib/mpd/*, /var/log/mpd/*, /var/run/mpd/*, 可選的音樂目錄 MPD 組.
nobody 無權限的組。
ntp /var/lib/ntp/* NTPd 組.
proc /proc/pid/ A group authorized to learn processes information otherwise prohibited by hidepid= mount option of the proc file system. The group must be explicitly set with the gid= mount option.
root /* 完全的系統管理和控制 (root, admin)
smmsp sendmail 羣組.
tty /dev/tty, /dev/vcc, /dev/vc, /dev/ptmx 訪問 /dev/ACMx
utmp /run/utmp, /var/log/btmp, /var/log/wtmp
vboxsf 虛擬系統的共享目錄 VirtualBox使用.

systemd 之前的羣組[編輯 | 編輯原始碼]

Before arch migrated to systemd, users had to be manually added to these groups in order to be able to access the corresponding devices. This way has been deprecated in favour of udev marking the devices with a uaccess tag[失效連結 2022-09-23 ⓘ] and logind assigning the permissions to users dynamically via ACLs according to which session is currently active. Note that the session must not be broken for this to work (see General troubleshooting#Session permissions to check it).

There are some notable exceptions which require adding a user to some of these groups: for example if you want to allow users to access the device even when they are not logged in. However, note that adding users to the groups can even cause some functionality to break (for example, the audio group will break fast user switching and allows applications to block software mixing).

如下組是 systemd 之前使用,目前已經沒有任何作用,使用後還可能對功能有影響:

作用
audio /dev/audio, /dev/snd/*, /dev/rtc0 直接訪問聲音硬件(ALSAOSS),JACK 也用來給予用户實時處理權限。
camera 訪問 Digital Cameras.
disk /dev/sda[1-9], /dev/sdb[1-9] 直接訪問不受 optical, floppystorage 組控制的塊設備. 除非有特殊需要, 否則不建議將一般用户添加至該組.
floppy /dev/fd[0-9] 訪問軟盤驅動器。
lp /etc/cups, /var/log/cups, /var/cache/cups, /var/spool/cups 訪問打印設備,管理打印任務。
network 改變網絡設置的權限,比如使用 NetworkManager 的權限.
optical /dev/sr[0-9], /dev/sg[0-9] 訪問光學設備,比如CD,DVD。
scanner /var/lock/sane 訪問掃描儀硬件。
storage 訪問可移動儲存器,例如 USB 硬盤、flash 存儲器、MP3 播放器等;用户可以通過 D-Bus 掛載設備。
sys 管理 CUPS 中的打印機.
video /dev/fb/0, /dev/misc/agpgart 訪問視頻捕獲和硬件加速設備。例如framebuffer (X 不屬於這個組也能使用).

不再使用的組[編輯 | 編輯原始碼]

Group Affected files Purpose
bin none Historical
daemon
lock Used for lockfile access. Required by e.g. gnokiiAUR.
mem
network Unused by default. Can be used e.g. for granting access to NetworkManager (see NetworkManager#Set up PolicyKit permissions).
power
uuidd
users The primary group for users when user private groups are not used (generally not recommended), e.g. when creating users with USERGROUPS_ENAB no in /etc/login.defs or the -N/--no-user-group option of useradd.