AIX 用戶和組管理
原文:https://www.ibm.com/developerworks/cn/aix/library/au-aixuseradmin/
管理 IBM AIX 中的用戶和組是管理員的重要責任之一。可以以許多種方式管理用戶和組:可以通過 AIX System Management Interface Tool (SMIT) 管理它們,也可以手工編輯配置文件或使用命令行命令。本文討論 AIX 上與用戶和組相關的配置文件,以及用來幫助管理用戶和組的命令行工具。
先了解文件,然后了解命令
在學習 AIX 中用來創建、修改和維護用戶和組的命令之前,一定要了解在幕后發生的情況。例如,應該了解相關文件及其作用。
下面是影響用戶本身的一些文件:
- /etc/passwd
- /etc/security/.profile
- /etc/security/limits
- /etc/security/passwd
- /etc/security/user
- /usr/lib/security/mkuser.default
/etc/passwd
/etc/passwd 文件包含用戶的基本信息,它可能是 UNIX® 和 Linux® 用戶最熟知的用戶管理文件。清單 1 給出 /etc/passwd 文件的示例。
清單 1. /etc/passwd 文件示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
root:!:0:0::/:/usr/bin/ksh
daemon:!:1:1::/etc:
bin:!:2:2::/bin:
sys:!:3:3::/usr/sys:
adm:!:4:4::/var/adm:
uucp:!:5:5::/usr/lib/uucp:
guest:!:100:100::/home/guest:
nobody:!:4294967294:4294967294::/:
lpd:!:9:4294967294::/:
lp:*:11:11::/var/spool/lp:/bin/false
invscout:*:6:12::/var/adm/invscout:/usr/bin/ksh
snapp:*:200:13:snapp login user:/usr/sbin/snapp:/usr/sbin/snappd
ipsec:*:201:1::/etc/ipsec:/usr/bin/ksh
nuucp:*:7:5:uucp login user:/var/spool/uucppublic:/usr/sbin/uucp/uucico
pconsole:*:8:0::/var/adm/pconsole:/usr/bin/ksh
esaadmin:*:10:0::/var/esa:/usr/bin/ksh
sshd:*:206:201::/var/empty:/usr/bin/ksh
atc:!:8000:400:Adam Cormany,Sr UNIX Admin:/home/atc:/bin/ksh
amdc:!:8001:401:AMDC:/home/amdc:/bin/ksh
pac:!:8002:400:PAC,Jr UNIX Admin:/home/pac:/bin/ksh
atc2:!:8003:402:ATCv2:/home/atc2:/bin/ksh
|
可以看到這個文件使用冒號 (:
) 作為分隔符,每個條目按以下格式包含 7 個字段(為了便於閱讀,在分隔符前后添加了空格):
1
|
Username : Password Flag : UID : GID : GECOS : Home : Shell/Command
|
下面逐一解釋這些字段:
- Username。這是與用戶賬戶相關聯的登錄名/用戶名。
- Password Flag。這個字段因 UNIX 和 Linux 的風格而異。在 AIX 上,第二個字段可以包含兩個字符之一:
!
或*
。如果顯示!
,那么已經為此用戶設置了密碼。如果還沒有設置密碼,就會出現*
。密碼本身存儲在 /etc/security/passwd 中。 - UID。User Identifier (UID) 是用戶的數字標識符。
- GID。Group Identifier (GID) 與 UID 相似,但是它與組相關聯。GID 在 /etc/group 中定義。
- GECOS。General Electric Comprehensive Operating System (GECOS) 信息存儲在第五個字段中。這里存儲用戶的姓名、電話號碼和其他一般個人信息。
- Home。這是用戶的主目錄。
- Shell/Command。通常情況下,最后一個字段包含在用戶登錄時啟動的 shell。管理員也可以通過修改這個字段執行其他命令而不是 shell(例如 /bin/false),從而限制訪問。
/etc/security/.profile
/etc/security/.profile 文件可以節省寶貴的時間和減少麻煩。在使用 mkuser
命令創建用戶時,執行 /usr/lib/security/mkuser.sys 腳本。這個腳本創建用戶的目錄,設置正確的權限,“創建” 用戶的 .profile。mkuser.sys 腳本實際上是把 /etc/security/.profile 文件復制到新用戶的主目錄中。
如果您正在構建新系統,或者一個新部門有 100 名員工需要在系統上建立賬戶,那么一定要先修改 /etc/security/.profile 文件,然后再開始創建用戶賬戶。如果已經創建了賬戶,然后意識到需要對某個變量或其他設置做簡單的修改,就不得不手工修改每個用戶的 profile。可以使用腳本簡化這個過程,但是如果提前修改了 /etc/security/.profile,會簡單得多。
清單 2 給出一個 /etc/security/.profile 文件示例。
清單 2. /etc/security/.profile 文件示例
1
2
3
4
5
6
7
|
PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:$HOME/bin:/usr/bin/X11:/sbin:.
export PATH
if [ -s "$MAIL" ] # This is at Shell startup. In normal
then echo "$MAILMSG" # operation, the Shell checks
fi # periodically.
|
/etc/security/limits
/etc/security/limits 文件包含所有 ulimit
,即用戶的系統資源限制。表 1 列出 /etc/security/limits 文件中的字段及其用途。
表 1. /etc/security/limits 中的字段
軟限制和硬限制的區別是什么?在最大值(硬限制)范圍內,用戶或應用程序可以動態地修改軟限制。硬限制 就是參數可以設置的最大值。如果把參數設置為數字值太困難(例如,如果開發人員不知道程序將使用的內存量或它需要打開的文件數量),那么可以把參數設置為 -1,這表示無限制。
但是,不必為每個用戶設置所有 ulimit
。/etc/security/limits 文件包含一個 default
部分,它為每個用戶定義一組標准值,如果用戶沒有設置定制的值,就會使用這些值。如果 default
部分不存在,系統會設置預先確定的限制。
IBM 的默認值如下:
1
2
3
4
5
6
7
8
9
|
* Attribute Value
* ========== ============
* fsize_hard set to fsize
* cpu_hard set to cpu
* core_hard -1
* data_hard -1
* stack_hard 8388608
* rss_hard -1
* nofiles_hard -1
|
清單 3 給出一個 /etc/security/limits 文件示例。
清單 3. /etc/security/limits 文件示例
1
2
3
4
5
6
7
8
9
10
11
12
|
default:
fsize = 4194303
core = 16384
cpu = -1
data = 262144
rss = 65536
stack = 65536
pac:
fsize = 131072
fsize_hard = 262144
core = 262144
|
假設用戶 “pac” 是一位初級 UNIX 管理員,他的軟限制值 fsize
由 default
部分的 4,194,303 降低到 131,072;但是,允許他在需要時把這個值增加到 262,144。另外,pac 經常把自己的程序弄壞。因此,把他的 core ulimit
增加到 262,144。
/etc/security/passwd
/etc/security/passwd 文件包含 AIX 用戶的密碼信息。在這個文件中,每個用戶有三個字段:
- password。加密的密碼。
注意:如果這個字段只包含星號 (
*
),那么賬戶被鎖定,直到設置密碼為止。 - lastupdate。最后一次更新密碼的時間(系統紀元以來的秒數)。
- flags。對修改用戶密碼的限制。可以設置三個標志:
- ADMIN。如果設置,那么只有根用戶可以修改用戶的密碼。
- ADMCHG。如果設置,那么在用戶下一次登錄或執行
su
時提示修改密碼。 - NOCHECK。如果設置,那么忽略 /etc/security/user 中的任何其他限制。
清單 4 提供一個 /etc/security/password 文件示例。
清單 4. /etc/security/password 文件示例
1
2
3
4
|
amdc:
password = oBQaUkPkUryCY
lastupdate = 1243972006
flags = ADMCHG
|
在這個示例中,用戶 “amdc” 的密碼是在 2009 年 6 月 2 日星期二 15:46:46 EDT 設置的。當用戶下一次登錄或執行 su
時,會提示修改密碼。
您可能想知道如何把系統紀元以來的秒數轉換為可讀性更好的表示法,為此我編寫了一個 Perl 腳本。這個腳本的核心如下:
1
2
|
# perl -e 'use POSIX; print strftime("%c\n", localtime(1243972006));'
Tue Jun 2 15:46:46 EDT 2009
|
/etc/security/user
現在,要接觸到 AIX 用戶管理的核心了。除了 /etc/passwd 中的基本信息之外,/etc/security/user 文件包含最重要的用戶設置。表 2 說明一些參數。
表 2. /etc/security/user 文件中的參數
參數的完整列表請查看 AIX 系統上的 /etc/security/user,或訪問 AIX Information Center。與 /etc/security/limits 一樣,如果沒有為賬戶指定值,就使用 default
部分設置所有字段。
/usr/lib/security/mkuser.default
/usr/lib/security/mkuser.default 文件包含在通過 mkuser
創建新的 AIX 用戶時使用的值。清單 5 提供這個文件的示例。
清單 5. /usr/lib/security/mkuser.default 文件示例
1
2
3
4
5
6
7
8
9
10
11
|
user:
pgrp = staff
groups = staff
shell = /usr/bin/ksh
home = /home/$USER
admin:
pgrp = system
groups = system
shell = /usr/bin/ksh
home = /home/$USER
|
在這個文件中可以定義許多參數。完整的列表請參見 man chuser
或訪問 IBM Systems Information Center。
基本命令
既然您已經熟悉了命令背后的文件,現在就來看看命令本身。學習如何創建用戶以及在創建用戶之后修改用戶。
mkuser
要了解的第一個命令是 mkuser
。如果沒有 mkuser
,其他命令都沒什么用。使用這個命令創建 AIX 用戶並設置初始值。下面是在創建用戶時要記住的幾條簡單規則:
- 用戶名不能以下面的字符開頭:
- 連字符或減號 (
-
) - 加號 (
+
) - At 符號 (
@
) - 波浪號 (
~
)
- 連字符或減號 (
- 用戶名不能是 ALL 或 default,因為這些名稱是為操作系統保留的。
- 用戶名不能包含:
- 冒號 (
:
) - 引號 — 單引號或雙引號 (
'
或"
) - 鎊符或數字符 (
#
) - 逗號 (
,
) - 等號 (
=
) - 斜杠 — 反斜杠或前向斜杠 (
\
或/
) - 問號 (
?
) - 反引號或勾號 (
`
) - 空白(空格或制表符)
- 換行符
- 冒號 (
- 在 AIX 5.2 和更早的版本上,用戶名的長度必須小於等於 8 個字符。從 AIX 5.3 開始,最大字符數增加到了 255。
為了在 AIX 5.3 和更高版本上檢查這個設置,可以使用 getconf
:
1
2
|
# getconf LOGIN_NAME_MAX
9
|
或 lsattr
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# lsattr -El sys0
SW_dist_intr false Enable SW distribution of interrupts True
autorestart true Automatically REBOOT OS after a crash True
boottype disk N/A False
capacity_inc 1.00 Processor capacity increment False
capped true Partition is capped False
conslogin enable System Console Login False
cpuguard enable CPU Guard True
dedicated true Partition is dedicated False
enhanced_RBAC true Enhanced RBAC Mode True
ent_capacity 1.00 Entitled processor capacity False
frequency 2656000000 System Bus Frequency False
fullcore true Enable full CORE dump True
fwversion IBM,EL340_075 Firmware version and revision levels False
id_to_partition 0X80000CE988400001 Partition ID False
id_to_system 0X80000CE988400000 System ID False
iostat false Continuously maintain DISK I/O history True
keylock normal State of system keylock at boot time False
log_pg_dealloc true Log predictive memory page deallocation events True
max_capacity 1.00 Maximum potential processor capacity False
max_logname 9 Maximum login name length at boot time True
maxbuf 20 Maximum number of pages in block I/O BUFFER CACHE True
maxmbuf 0 Maximum Kbytes of real memory allowed for MBUFS True
maxpout 0 HIGH water mark for pending write I/Os per file True
maxuproc 800 Maximum number of PROCESSES allowed per user True
min_capacity 1.00 Minimum potential processor capacity False
minpout 0 LOW water mark for pending write I/Os per file True
modelname IBM,8203-E4A Machine name False
ncargs 256 ARG/ENV list size in 4K byte blocks True
nfs4_acl_compat secure NFS4 ACL Compatibility Mode True
pre430core false Use pre-430 style CORE dump True
pre520tune disable Pre-520 tuning compatibility mode True
realmem 3784704 Amount of usable physical memory in Kbytes False
rtasversion 1 Open Firmware RTAS version False
sed_config select Stack Execution Disable (SED) Mode True
systemid IBM,021082744 Hardware system identifier False
variable_weight 0 Variable processor capacity weight False
|
要修改這個設置,只需使用 chdev
調整 v_max_logname
參數(在 lsattr
輸出中顯示為 max_logname
),把它設置為所需的最大字符數加一,加一是為了容納終止字符。例如,如果希望用戶名的長度是 128 個字符,那么應該把 v_max_logname
設置為 129:
1
2
|
# chdev -l sys0 -a max_logname=129
sys0 changed
|
請注意,這一修改直到重新引導操作系統之后才會生效。重新引導服務器之后,可以檢查修改是否已經生效了:
1
2
|
# getconf LOGIN_NAME_MAX
128
|
但是請記住,如果環境包含 AIX 5.3 之前的 IBM RS/6000® 服務器,或者操作系統無法處理超過 8 個字符的用戶名,而且依靠 NIS 或其他身份驗證機制,那么應該繼續使用 8 個字符的用戶名。
要想用默認設置創建用戶並分配下一個可用的 UID,只需執行 mkuser
加上用戶名:
1
2
3
4
5
|
# mkuser xander
# finger xander
Login name: xander
Directory: /home/xander Shell: /usr/bin/ksh
No Plan.
|
很容易,不是嗎?現在試一下更有意思的操作。通過添加在 chuser
手冊頁 (man chuser
) 上找到的一些值,可以包含用戶的 GECOS 信息並把用戶的 core ulimit
改為 524,288,見 清單 6。
清單 6. 修改用戶的 core ulimit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# mkuser core=524288 gecos="Xander Cormany,317.555.1234" xander
# finger xander
Login name: xander In real life: Xander Cormany
Site Info: 317.555.1234
Directory: /home/xander Shell: /usr/bin/ksh
No Plan.
# su - xander "-c ulimit -a"
time(seconds) unlimited
file(blocks) unlimited
data(kbytes) unlimited
stack(kbytes) 4194304
memory(kbytes) unlimited
coredump(blocks) 524288
nofiles(descriptors) unlimited
threads(per process) unlimited
processes(per user) unlimited
|
與 /etc/passwd 中的其他字段一樣,GECOS 的值不應該包含冒號 (:
)。添加冒號就會影響對字段位置的解釋,所有期望值會向右移。例如,如果用戶試圖在 /etc/passwd 的 GECOS 字段中設置 Xander:Cormany 值,Xander 實際上會在正確的字段中,而 Cormany 成為右邊字段(即主目錄)的值。另外,GECOS 字段不能以 !#
結尾。
大多數管理員其實並不使用這樣的命令行,但是了解 SMIT(man smit
或 man smitty
)等實用程序在幕后做的工作是很重要的。如果您願意使用 SMIT,過程很簡單。下面是使用 SMIT 創建具有相同屬性的同一用戶的示例。在用戶創建屏幕上直接輸入 SMIT
,然后使用快速路徑 mkuser
:
1
|
# smitty mkuser
|
圖 1 顯示 SMIT 實用程序。
圖 1. smitty mkuser 過程
填寫用戶名、GECOS 字段和 core ulimit
之后,按 Enter 創建用戶。當 SMIT 返回命令成功完成的消息時,按 F10 或 Esc + 0退出程序。可以使用 清單 7 中的代碼檢查用戶。
清單 7. 檢查 SMIT 是否成功地創建了用戶
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# finger xander
Login name: xander In real life: Xander Cormany
Site Info: 317.555.1234
Directory: /home/xander Shell: /usr/bin/ksh
No Plan.
# su - xander "-c ulimit -a"
time(seconds) unlimited
file(blocks) unlimited
data(kbytes) unlimited
stack(kbytes) 4194304
memory(kbytes) unlimited
coredump(blocks) 524288
nofiles(descriptors) unlimited
threads(per process) unlimited
processes(per user) unlimited
|
chuser
現在到了比較難的部分。但是等等:Xander 的經理 Ann 剛才來告訴您 Xander 的 core ulimit
應該是 1,048,576(有人忘記乘以 2 了)。沒問題:只需用 chuser
修改 ulimit
。
chuser
命令的語法與 mkuser
非常相似,使用相同的屬性。清單 8 提供 chuser
命令的示例。
清單 8. chuser 命令
1
2
3
4
5
6
7
8
9
10
11
|
# chuser core=1048576 xander
# su - xander "-c ulimit -a"
time(seconds) unlimited
file(blocks) unlimited
data(kbytes) unlimited
stack(kbytes) 4194304
memory(kbytes) unlimited
coredump(blocks) 1048576
nofiles(descriptors) unlimited
threads(per process) unlimited
processes(per user) unlimited
|
可以使用快速路徑在 SMIT 中方便地訪問這些命令。smitty chuser
會把您直接帶到用戶修改屏幕。
chsh
有時候希望改變 shell。AIX 中的默認 shell 是 Korn shell,即 ksh。改變 shell 的方法是執行 chsh
並提供用戶名,然后選擇所需的 shell,見 清單 9。
清單 9. 改變用戶的 shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# finger xander
Login name: xander In real life: Xander Cormany
Site Info: 317.555.1234
Directory: /home/xander Shell: /usr/bin/ksh
No Plan.
# chsh xander
Current available shells:
/bin/sh
/bin/bsh
/bin/csh
/bin/ksh
/bin/tsh
/bin/ksh93
/usr/bin/sh
/usr/bin/bsh
/usr/bin/csh
/usr/bin/ksh
/usr/bin/tsh
/usr/bin/ksh93
/usr/bin/rksh
/usr/bin/rksh93
/usr/sbin/uucp/uucico
/usr/sbin/sliplogin
/usr/sbin/snappd
xander's current login shell:
/usr/bin/ksh
Change (yes) or (no)? > yes
To?>/usr/bin/csh
# finger xander
Login name: xander In real life: Xander Cormany
Site Info: 317.555.1234
Directory: /home/xander Shell: /usr/bin/csh
No Plan.
|
chfn
在為 Xander 創建 AIX 用戶時,管理員在 GECOS 信息中的姓名中出現了輸入錯誤。要糾正這個錯誤,應該使用 chfn
命令。這個命令與 chsh
很相似,它顯示當前值,詢問用戶是否希望修改它,然后把值改為輸入的值。清單 10 提供一個示例。
清單 10. chfn 命令示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# finger xander
Login name: xander In real life: Zander Cormany
Site Info: 317.555.1234
Directory: /home/xander Shell: /usr/bin/ksh
No Plan.
# chfn xander
xander's current gecos:
"Zander Cormany,317.555.1234"
Change (yes) or (no)? > yes
To?>Xander Cormany,317.555.1234
# finger xander
Login name: xander In real life: Xander Cormany
Site Info: 317.555.1234
Directory: /home/xander Shell: /usr/bin/ksh
No Plan.
|
糾正 GECOS 信息看起來可能意義不大,但是這對系統上的其他管理員和用戶有幫助。例如,如果您試圖找到 Xander 的賬戶,但是沒有記住他的用戶名,就可以在 GECOS 信息中搜索他的姓名。搜索他的姓(已經在 GECOS 字段中正確地輸入了),可以快速地顯示他的用戶名。finger
命令在 /etc/passwd 中用戶名和 GECOS 信息的第一個字段中的真實姓名中搜索字符串:
1
2
3
4
5
6
7
8
9
|
# finger cormany
Login name: atc In real life: Adam Cormany
Directory: /home/cormany Shell: /bin/ksh
No Plan.
Login name: xander In real life: Xander Cormany
Site Info: 317.555.1234
Directory: /home/xander Shell: /usr/bin/ksh
No Plan.
|
lsuser
從各個用戶文件收集某一用戶的所有信息看起來可能很繁瑣。好在 AIX 提供了一個簡便的命令,可以一下子收集所有數據。lsuser
命令從各個管理文件收集指定用戶的所有屬性。這對於許多任務非常有幫助,比如對比用戶、生成要備份的所有用戶的完整列表和解決某一賬戶的問題。
可以使用 -f
開關查看一個用戶的屬性,它按特定的結構顯示所有信息。清單 11 提供輸出的示例。
清單 11. lsuser -f 的輸出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# lsuser -f xander
xander:
id=214
pgrp=staff
groups=staff
home=/home/xander
shell=/usr/bin/ksh
gecos=Xander Cormany,317.555.1234
login=true
su=true
rlogin=true
daemon=true
admin=false
sugroups=ALL
admgroups=
tpath=nosak
ttys=ALL
expires=0
auth1=SYSTEM
auth2=NONE
umask=22
registry=files
SYSTEM=compat
logintimes=
loginretries=0
pwdwarntime=0
account_locked=false
minage=0
maxage=0
maxexpired=-1
minalpha=0
minother=0
mindiff=0
maxrepeats=8
minlen=0
histexpire=0
histsize=0
pwdchecks=
dictionlist=
default_roles=
fsize=-1
cpu=-1
data=-1
stack=-1
core=1048576
rss=-1
nofiles=-1
roles=
|
如果要對比用戶,只需把開關由 -f
改為 -c
並通過逗號分隔的參數添加要對比的用戶。清單 12 提供輸出示例。
清單 12. lsuser -c 的輸出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# lsuser -c xander,atc
#name:id:pgrp:groups:home:shell:gecos:login:su:rlogin:daemon:admin:
sugroups:tpath:ttys:expires:auth1:auth2:umask:registry:SYSTEM:loginretries:
pwdwarntime:account_locked:minage:maxage:maxexpired:minalpha:minother:
mindiff:maxrepeats:minlen:histexpire:histsize:fsize:cpu:data:stack:core:rss:nofiles
xander:214:staff:staff:/home/xander:/usr/bin/ksh:Xander Cormany,317.555.1234:
true:true:true:true:false:ALL:nosak:ALL:0:SYSTEM:NONE:22:files:compat:0:0:false:
0:0:-1:0:0:0:8:0:0:0:-1:-1:-1:-1:1048576:-1:-1
#name:id:pgrp:groups:home:shell:gecos:login:su:rlogin:daemon:admin:sugroups:
tpath:ttys:expires:auth1:auth2:umask:registry:SYSTEM:loginretries:pwdwarntime:
account_locked:minage:maxage:maxexpired:minalpha:minother:mindiff:maxrepeats:
minlen:histexpire:histsize:fsize:cpu:data:stack:core:rss:nofiles:time_last_login:
time_last_unsuccessful_login:tty_last_login:tty_last_unsuccessful_login:host_last_login:
host_last_unsuccessful_login:unsuccessful_login_count
cormany:215:staff:staff,support:/home/cormany:/bin/ksh:Adam Cormany:true:true:
true:true:false:ALL:nosak:ALL:0:SYSTEM:NONE:22:NIS:compat:0:0:false:0:0:
-1:0:0:0:8:0:0:0:-1:-1:-1:-1:-1:-1:-1:1250854405:1250522447:/dev/pts/3:/dev/pts/13:
10.20.30.40:10.20.30.41:0
|
這輸出大量信息,看起來很亂。但是,如果把這些數據導入電子表格,就一目了然了。在腳本中使用這些數據管理用戶時,這種包含分隔符的格式也有幫助。
如果只想尋找幾個字段(例如用戶的 shell 和主目錄),那么可以在 lsuser
命令中使用 -a
開關。清單 13 提供這個命令的示例,使用來自 chuser
手冊頁的字段。
清單 13. 運行 lsuser -c –a
1
2
3
4
5
|
# lsuser -c -a shell home xander,cormany
#name:shell:home
xander:/usr/bin/ksh:/home/xander
#name:shell:home
cormany:/bin/ksh:/home/cormany
|
passwd
許多人認為 passwd
命令只用於修改用戶的密碼。盡管 passwd
確實執行這種重要的功能,但是它還有其他特性。
passwd
最重要的特性確實是修改用戶的密碼。如果符合 /etc/security/user 和 /etc/security/passwd 配置文件中設置的規則,標准用戶就可以修改自己的密碼;如果作為根用戶登錄,還可以修改其他用戶的密碼,見 清單 14。
清單 14. 使用 passwd 修改用戶的密碼
1
2
3
4
5
6
7
8
9
10
11
12
|
# lsuser -c -a password xander
#name:password
xander:*
# passwd xander
Changing password for "xander"
xander's New password:
Enter the new password again:
# lsuser -c -a password xander
#name:password
xander:!
|
passwd
命令還可以像 chfn
一樣修改用戶的 GECOS 信息,或者像 chsh
一樣修改用戶登錄時執行的 shell/命令。清單 15 提供一個示例。
清單 15. 使用 passwd 修改用戶的信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# passwd -f xander
xander's current gecos:
"Xander Cormany,317.555.1234"
Change (yes) or (no)? > yes
To?>Xander Cormany,317.555.7890
# passwd -s xander
Current available shells:
/bin/sh
/bin/bsh
/bin/csh
/bin/ksh
/bin/tsh
/bin/ksh93
/usr/bin/sh
/usr/bin/bsh
/usr/bin/csh
/usr/bin/ksh
/usr/bin/tsh
/usr/bin/ksh93
/usr/bin/rksh
/usr/bin/rksh93
/usr/sbin/uucp/uucico
/usr/sbin/sliplogin
/usr/sbin/snappd
xander's current login shell:
/usr/bin/ksh
Change (yes) or (no)? > yes
To?>/usr/bin/bsh
# finger xander
Login name: xander In real life: Xander Cormany
Site Info: 317.555.7890
Directory: /home/xander Shell: /usr/bin/bsh
No Plan.
|
pwdadm
AIX 中的 pwdadm
命令可以修改密碼。另外,pwdadm
可以顯示(不包括加密的密碼)或更新 /etc/security/passwd 中的用戶標志。仍然以 Xander 的賬戶做實驗,首先修改他的密碼,然后查看當前的密碼屬性。因為修改了密碼,將設置 ADMCHG
標志。把這個標志改為 ADMIN
並限制賬戶,以后只允許管理員更新密碼。清單 16 給出執行此任務的代碼。
清單 16. 修改用戶的密碼並限制特權
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# pwdadm xander
Changing password for "xander"
xander's New password:
Enter the new password again:
# pwdadm -q xander
xander:
lastupdate = 1250858719
flags = ADMCHG
# pwdadm -f ADMIN xander
# pwdadm -q xander
xander:
lastupdate = 1250858719
flags = ADMIN
|
rmuser
現在要從系統中刪除用戶;必須刪除 Xander 的賬戶。這需要使用 rmuser
。
要想刪除用戶,只需執行 rmuser
並作為參數指定用戶的賬戶名。這樣做會從系統中刪除用戶,但是用戶的密碼信息仍然保留在 /etc/security/passwd 文件中:
1
|
# rmuser xander
|
可以使用 -p
開關完全刪除用戶的密碼信息:
1
|
# rmuser –p xander
|
注意,rmuser
並不刪除用戶的主目錄。用戶的主目錄中可能有應該保留的重要數據,由您負責在確認安全時刪除主目錄。
組
您已經熟悉了一些用戶修改命令;現在,我們來討論組。與用戶管理一樣,一定要了解命令在幕后修改的配置文件。
/etc/group
/etc/group 文件包含組的基本信息。清單 17 提供 /etc/group 文件的示例。
清單 17. /etc/group 文件示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
system:!:0:root,pconsole,esaadmin
staff:!:1:ipsec,esaadmin,sshd,xander
bin:!:2:root,bin
sys:!:3:root,bin,sys
adm:!:4:bin,adm
uucp:!:5:uucp,nuucp
mail:!:6:
security:!:7:root
cron:!:8:root
printq:!:9:lp
audit:!:10:root
ecs:!:28:
nobody:!:4294967294:nobody,lpd
perf:!:20:
shutdown:!:21:
lp:!:11:root,lp
invscout:!:12:invscout
snapp:!:13:snapp
ipsec:!:200:
pconsole:!:14:pconsole
sshd:!:201:sshd
|
與 /etc/passwd 文件一樣,這個文件使用冒號作為分隔符,每個條目按以下格式包含 4 個字段(為了便於閱讀,在分隔符前后添加了空格):
1
|
Group Name : Password Flag : GID : User(s)
|
下面逐一解釋這些字段:
- Group Name。與組相關聯的組名。
- Password Flag。AIX 中不使用這個字段。AIX 使用 /etc/security/group 文件存儲組管理員信息。
- GID。與組相關聯的 GID。
- User(s)。屬於組成員的用戶的列表。
注意:這個字段以逗號作為分隔符。
/etc/security/group
/etc/security/group 文件與 /etc/security/user 很相似:它包含指定的組的擴展屬性。表 3 說明這個配置文件中的兩個參數。
表 3. /etc/security/group 參數
更多屬性請參見 /etc/security/group 的手冊頁 (man group
) 或訪問 http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.files/doc/aixfiles/group.htm。
與 /etc/security 中的其他配置文件一樣,這個文件划分為小節,組名作為標識符。這個文件的特性之一是允許把組的管理員權限分配給標准用戶。組的管理員可以根據需要在組中添加或刪除成員。清單 18 提供 /etc/security/group 文件的示例。在這個示例中,jradmin 組的 admin
屬性設置為 False,標准用戶 pac 和 xander 定義為組的管理員。
清單 18. /etc/security/group 文件示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
system:
admin = true
staff:
admin = false
bin:
admin = true
sys:
admin = true
jradmin:
admin = false
adms = pac,xander
|
幾個命令
您已經了解了命令背后的文件。現在來看看命令本身。學習如何創建組以及在創建組之后修改組。
mkgroup
在 AIX 中創建組非常簡單明了。對創建用戶的限制也適用於組:
- 組名不能以下面的字符開頭:
- 連字符或減號 (
-
) - 加號 (
+
) - At 符號 (
@
) - 波浪號 (
~
)
- 連字符或減號 (
- 組名不能是 ALL 或 default,因為這些名稱是為操作系統保留的。
- 組名不能包含:
- 冒號 (
:
) - 引號 — 單引號或雙引號 (
'
或"
) - 鎊符或數字符 (
#
) - 逗號 (
,
) - 等號 (
=
) - 斜杠 — 反斜杠或前向斜杠 (
\
或/
) - 問號 (
?
) - 反引號或勾號 (
`
) - 空白(空格或制表符)
- 換行符
- 冒號 (
- 在 AIX 5.2 和更早的版本上,組名的長度必須小於等於 8 個字符。從 AIX 5.3 開始,最大字符數增加到了 255。
由同一個參數 v_max_logname
控制用戶名和組名的長度。要想查看或修改這個值,請參見本文前面 mkuser 小節中對查看和修改用戶名長度的說明。
要想創建組,只需執行 mkgroup
命令並指定組名,見 清單 19。
清單 19. 用 mkgroup 創建組
1
2
3
4
5
6
7
8
|
# mkgroup atctest
# grep atctest /etc/group
atctest:!:202:
# grep -p atctest /etc/security/group
atctest:
admin = false
|
添加 -a
可以創建管理員組,見 清單 20。
清單 20. 創建管理員組
1
2
3
4
5
6
7
8
|
# mkgroup -a atcadmin
# grep atcadmin /etc/group
atcadmin:!:15:
# grep -p atcadmin /etc/security/group
atcadmin:
admin = true
|
要想創建組並指定 Xander 作為組的管理員,應該在命令行上指定 adms=xander
,這會在 /etc/security/group 文件中添加 adms
屬性,見 清單 21。
清單 21. 指定一個用戶作為組管理員
1
2
3
4
5
6
7
8
9
|
# mkgroup adms=xander xangroup
# grep xangroup /etc/group
xangroup:!:203:
# grep -p xangroup /etc/security/group
xangroup:
admin = false
adms = xander
|
與 mkuser
一樣,mkgroup
采用與 chgroup
相同的屬性。完整的屬性列表請參見 chgroup
的手冊頁 (man chgroup
)。
chgroup
chgroup
命令與 chuser
相似,它的手冊頁包含可以修改的所有組屬性。清單 22 中的示例把 xangroup
組的 GID 由 203 改為 204,還添加了幾個用戶。
清單 22. 修改組的 GID 並添加用戶
1
2
3
4
5
6
7
|
# grep xangroup /etc/group
xangroup:!:203:
# chgroup id=204 users=xander,atc,amdc xangroup
# grep xangroup /etc/group
xangroup:!:204:xander,atc,amdc
|
chgrpmem
也可以使用 chgrpmem
修改組的成員。可以使用 chgrpmem
命令列出、添加和刪除組中的用戶,以及改變組管理員。
例如,xangroup 組包含成員 xander 和 atc,xander 是組管理員。從組中刪除 atc,見 清單 23。
清單 23. 從組中刪除用戶
1
2
3
4
5
6
7
8
9
10
11
|
# chgrpmem xangroup
xangroup:
members = xander,atc
adms = xander
# chgrpmem -m - atc xangroup
# chgrpmem xangroup
xangroup:
members = xander
adms = xander
|
假設這是錯的,不應該刪除 atc 用戶。相反,應該把 atc 用戶變成組管理員,刪除 xander 的管理權限。清單 24 給出糾正錯誤的代碼。
清單 24. 添加已經刪除的用戶並改變組管理員
1
2
3
4
5
6
7
8
9
10
|
# chgrpmem -m + atc xangroup
# chgrpmem -a + atc xangroup
# chgrpmem -a - xander xangroup
# chgrpmem xangroup
xangroup:
members = xander,atc
adms = atc
|
lsgroup
lsuser
對於用戶管理很有用,有相似的組管理命令嗎?有,這就是 lsgroup
。lsgroup
采用 AIX 的標准命令和選項格式,采用與 lsuser
相同的結構。
清單 25 中的示例使用 清單 11、清單 12 和 清單 13 中在 lsuser
中使用過的開關。
清單 25. lsgroup 命令的輸出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# lsgroup xangroup
xangroup id=204 admin=false users=xander,cormany adms=cormany registry=files
# lsgroup -f xangroup
xangroup:
id=204
admin=false
users=xander,cormany
adms=cormany
registry=files
# lsgroup -c xangroup,atcadmin
#name:id:admin:users:adms:registry
xangroup:204:false:xander,cormany:cormany:files
#name:id:admin:registry
atcadmin:15:true:files
# lsgroup -c -a id xangroup,atcadmin
#name:id
xangroup:204
#name:id
atcadmin:15
|
rmgroup
前面創建了組。現在,要清理 AIX 系統。要想從系統中刪除組,只需執行 rmgroup
並指定組名:
1
|
# rmgroup atctest
|
以這個組作為主組的所有用戶必須把主組改為另一個組,然后 rmgroup
命令才允許刪除這個組。
結束語
閱讀完本文之后,現在您應該已經熟悉了 AIX 中用於用戶和組維護的配置文件。還應該更好地了解了命令行工具,知道如何創建、修改和刪除用戶和組。IBM 大大簡化了這些維護任務,讓您能夠集中精力處理更復雜的問題。