Linux 管理員 - 使用者管理



在討論使用者管理時,我們需要理解三個重要的術語:

  • 使用者
  • 許可權

我們已經深入討論了應用於檔案和資料夾的許可權。在本章中,讓我們討論使用者和組。

CentOS 使用者

在 CentOS 中,有兩種型別的賬戶:

  • 系統賬戶 - 用於守護程序或其他軟體。

  • 互動式賬戶 - 通常分配給使用者以訪問系統資源。

這兩種使用者型別的主要區別在於:

  • 系統賬戶由守護程序用於訪問檔案和目錄。這些通常不允許透過 shell 或物理控制檯登入進行互動式登入。

  • 互動式賬戶由終端使用者用於透過 shell 或物理控制檯登入訪問計算資源。

有了對使用者的基本瞭解,現在讓我們為會計部門的 Bob Jones 建立一個新使用者。使用adduser命令新增新使用者。

以下是一些adduser常用開關:

開關 操作
-c 向用戶帳戶添加註釋
-m 如果不存在,則在預設位置建立使用者主目錄
-g 分配給使用者的預設組
-n 不為使用者建立私有組,通常是具有使用者名稱組
-M 不建立主目錄
-s 除了/bin/bash之外的預設 shell
-u 指定 UID(否則由系統分配)
-G 分配給使用者的其他組

建立新使用者時,請按如下方式使用-c、-m、-g、-n開關:

[root@localhost Downloads]# useradd -c "Bob Jones  Accounting Dept Manager" 
-m -g accounting -n bjones

現在讓我們看看我們的新使用者是否已建立:

[root@localhost Downloads]# id bjones 
(bjones) gid = 1001(accounting) groups = 1001(accounting)

[root@localhost Downloads]# grep bjones /etc/passwd 
bjones:x:1001:1001:Bob Jones  Accounting Dept Manager:/home/bjones:/bin/bash

[root@localhost Downloads]#

現在我們需要使用passwd命令啟用新帳戶:

[root@localhost Downloads]# passwd bjones 
Changing password for user bjones. 
New password:  
Retype new password:  
passwd: all authentication tokens updated successfully.

[root@localhost Downloads]#

使用者帳戶未啟用,不允許使用者登入系統。

停用使用者帳戶

有幾種方法可以停用系統上的帳戶。這些方法從手動編輯/etc/passwd檔案開始。甚至可以使用帶有-l開關的passwd命令。這兩種方法都存在一個很大的缺點:如果使用者具有ssh訪問許可權並使用 RSA 金鑰進行身份驗證,他們仍然可以使用此方法登入。

現在讓我們使用chage命令,將密碼過期日期更改為過去的日期。此外,最好在帳戶上記下我們停用它的原因。

[root@localhost Downloads]# chage -E 2005-10-01 bjones
 
[root@localhost Downloads]# usermod  -c "Disabled Account while Bob out of the country 
for five months" bjones

[root@localhost Downloads]# grep bjones /etc/passwd 
bjones:x:1001:1001:Disabled Account while Bob out of the country for four 
months:/home/bjones:/bin/bash

[root@localhost Downloads]#

管理組

在 Linux 中管理組使管理員可以方便地將使用者組合到容器中,並應用適用於所有組成員的許可權集。例如,會計部門中的所有使用者可能都需要訪問相同的檔案。因此,我們建立一個會計組,並新增會計使用者。

在大多數情況下,任何需要特殊許可權的操作都應在組中完成。這種方法通常比僅對一個使用者應用特殊許可權可以節省時間。例如,Sally 負責報表,並且只有 Sally 需要訪問某些檔案才能生成報表。但是,如果 Sally 有一天生病了,而 Bob 負責報表怎麼辦?或者報表的需要增長了怎麼辦?當建立一個組時,管理員只需執行一次。新增使用者將根據需要更改或擴充套件進行應用。

以下是一些用於管理組的常用命令:

  • chgrp
  • groupadd
  • groups
  • usermod

chgrp - 更改檔案或目錄的組所有權。

讓我們為會計組中的人員建立一個儲存檔案和建立檔案目錄的目錄。

[root@localhost Downloads]# mkdir /home/accounting

[root@localhost Downloads]# ls -ld /home/accounting
drwxr-xr-x. 2 root root 6 Jan 13 10:18 /home/accounting

[root@localhost Downloads]#

接下來,讓我們將組所有權賦予accounting組。

[root@localhost Downloads]# chgrp -v  accounting /home/accounting/ 
changed group of ‘/home/accounting/’ from root to accounting

[root@localhost Downloads]# ls -ld /home/accounting/ 
drwxr-xr-x. 2 root accounting 6 Jan 13 10:18 /home/accounting/

[root@localhost Downloads]#

現在,會計組中的每個人都對/home/accounting具有讀取執行許可權。他們也需要寫入許可權。

[root@localhost Downloads]# chmod g+w /home/accounting/

[root@localhost Downloads]# ls -ld /home/accounting/ 
drwxrwxr-x. 2 root accounting 6 Jan 13 10:18 /home/accounting/

[root@localhost Downloads]#

由於會計組可能會處理敏感文件,因此我們需要對其他世界應用一些限制性許可權。

[root@localhost Downloads]# chmod o-rx /home/accounting/

[root@localhost Downloads]# ls -ld /home/accounting/ 
drwxrwx---. 2 root accounting 6 Jan 13 10:18 /home/accounting/

[root@localhost Downloads]#

groupadd - 用於建立新組。

開關 操作
-g 指定組的 GID
-K 覆蓋/etc/login.defs中 GID 的規格
-o 允許覆蓋非唯一組 ID 禁止
-p 組密碼,允許使用者自行啟用

讓我們建立一個名為secret的新組。我們將向該組新增一個密碼,允許使用者使用已知密碼新增自己。

[root@localhost]# groupadd secret

[root@localhost]# gpasswd secret 
Changing the password for group secret 
New Password:  
Re-enter new password:

[root@localhost]# exit 
exit

[centos@localhost ~]$ newgrp secret 
Password:

[centos@localhost ~]$ groups 
secret wheel rdc

[centos@localhost ~]$

在實踐中,組密碼並不常用。輔助組足夠,並且在其他使用者之間共享密碼並不是一個很好的安全實踐。

groups命令用於顯示使用者屬於哪個組。在對當前使用者進行一些更改後,我們將使用它。

usermod用於更新帳戶屬性。

以下是usermod的常用開關。

開關 操作
-a 追加,將使用者新增到補充組,僅與-G選項一起使用
-c 註釋,更新使用者註釋值
-d 主目錄,更新使用者的主目錄
-G 組,新增或刪除輔助使用者組
-g 組,使用者的預設主組
[root@localhost]# groups centos 
centos : accounting secret

[root@localhost]#

[root@localhost]# usermod -a -G wheel centos

[root@localhost]# groups centos
centos : accounting wheel secret

[root@localhost]#
廣告

© . All rights reserved.