3.1 用户配置文件和密码配置文件
用户名:密码(已废弃):UID:GID:注释:用户家目录:用户的shell(/sbin/nologin:禁止登录)
#
head -2 /etc/passwd; tail -2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
aming:x:1000:1000::/home/aming:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
# man shadow
struct spwd {
char *sp_namp; /* user login name */
char *sp_pwdp; /* encrypted password */
long int sp_lstchg; /* last password change */
long int sp_min; /* days until change allowed. */
long int sp_max; /* days before change required */
long int sp_warn; /* days warning for expiration */
long int sp_inact; /* days before account inactive */
long int sp_expire; /* date when account expires */
long int sp_flag; /* reserved for future use */
}
每个字段的含义是:
· sp_namp - 指向以 null 结束的用户名的指针
· sp_pwdp - 指向 null 结束的密码的指针
· sp_lstchg - 最近更改密码的日期(日期计算方法是从1970年1月1日开始的天数)
· sp_min - days before which password may not be changed ##下次可以改密码的天数,默认为0,表示不受限制
· sp_max - days after which password must be changed ##密码失效时间,默认为99999
· sp_warn - days before password is to expire that user is warned of pending password expiration ##密码过期提醒时间,默认为7天
· sp_inact - days after password expires that account is considered inactive and disabled ##密码过期n天后禁用账户
· sp_expire - days since Jan 1, 1970 when account will be disabled ##账户禁用倒计时
· sp_flag - reserved for future use
#
head -2 /etc/shadow; tail -2 /etc/shadow
root:$6$MLVDDpUb$SX6Zn3j4brcXC0GKGrduxRg5eGb90EfNhzG3adB6C.F5TEJRy4A/ZRBR7vs48tKVRYrO5TcP3n1VwSsE.pJOb.:17517:0:99999:7:::
bin:*:17110:0:99999:7:::
aming:!!:17521:0:99999:7:::
user1:!!:17521:0:99999:7:::
3.2 用户组管理
/etc/group
man gshadow
# ll /etc/passwd*
-rw-r--r--. 1 root root 928 12月 21 08:52 /etc/passwd
-rw-r--r--. 1 root root 887 12月 21 08:52 /etc/passwd-
# wc -l !$
wc -l /etc/passwd*
21 /etc/passwd
20 /etc/passwd-
41 总用量
# diff /etc/passwd /etc/passwd-
21d20
< user1:x:1001:1001::/home/user1:/bin/bash
groupadd - create a new group
# groupadd grp1
# tail -1 /etc/group
grp1:x:1002:
-g, --gid GID
The numerical value of the group's ID.
This value must be unique, unless the -o option is used.
# groupadd -g 2002 grp2
# tail -2 /etc/group
grp1:x:1002:
grp2:x:2002:
-o, --non-unique
This option permits to add a group with a non-unique GID.
在 /etc/login.defs 中有如下配置变量,可以用来更改此工具的行为:
GID_MAX (number), GID_MIN (number)
useradd,groupadd 或 newusers 创建的常规组的组 ID 的范围。
GID_MIN 和 GID_MAX 的默认值分别是 1000 和 60000。
groupdel - delete a group
CAVEATS(警告)
您不能移除现有用户的主组。在移除此组之前,必须先移除此用户。
您需要手动检查所有文件系统,以确保没有遗留的属于此组的文件。
# groupdel grp2
# tail -2 /etc/group
slocate:x:21:
grp1:x:1002:
# groupdel user1
groupdel:不能移除用户“user1”的主组
3.3 用户管理
useradd - 创建一个新用户或更新默认新用户信息
# useradd user2
# tail -3 /etc/passwd
aming:x:1000:1000::/home/aming:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1003::/home/user2:/bin/bash
-u, --uidUID
用户 ID 的数字值。此值必须为唯一的,除非使用了 -o 选项。
-g, --gidGROUP
用户初始登陆组的组名或号码。组名必须已经存在。组号码必须指代已经存在的组。
# useradd -u 1004 -g grp1 user3
# tail -3 /etc/passwd
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1003::/home/user2:/bin/bash
user3:x:1004:1002::/home/user3:/bin/bash
# tail -3 /etc/group
grp1:x:1002:
user2:x:1003:
user5:x:1007:
-d, --home-dir HOME_DIR
The new user will be created using HOME_DIR as the value for the user's login directory.
The default is to append the LOGIN name to BASE_DIR and use that as the login directory name.
-s, --shellSHELL
用户的登录 shell 名。默认为留空,让系统根据 /etc/default/useradd 中的 SHELL 变量选择默认的登录shell,默认为空字符串
# useradd -u 1006 -g grp1 -d /home/user444 -s /sbin/nologin user4
# tail -2 /etc/passwd
user3:x:1004:1002::/home/user3:/bin/bash
user4:x:1006:1002::/home/user444:/sbin/nologin
-M, --no-create-home
Do not create the user's home directory, even if the system wide setting from /etc/login.defs (CREATE_HOME) is set to yes.
# useradd -M user5
# tail -2 /etc/passwd
user4:x:1006:1002::/home/user444:/sbin/nologin
user5:x:1007:1007::/home/user5:/bin/bash
# ls /home
aming user1 user2 user3 user444
# tail -5 /etc/passwd
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1003::/home/user2:/bin/bash
user3:x:1004:1002::/home/user3:/bin/bash
user4:x:1006:1002::/home/user444:/sbin/nologin
user5:x:
1007:
1007::/home/user5:/bin/bash
# groupadd -g 1010 grp3
# useradd -g grp3 user6
# tail -5 /etc/passwd
user2:x:1002:1003::/home/user2:/bin/bash
user3:x:1004:1002::/home/user3:/bin/bash
user4:x:1006:1002::/home/user444:/sbin/nologin
user5:x:
1007:1007::/home/user5:/bin/bash
user6:x:
1008:
1010::/home/user6:/bin/bash
# useradd user7
# tail -5 /etc/passwd
user3:x:1004:1002::/home/user3:/bin/bash
user4:x:1006:1002::/home/user444:/sbin/nologin
user5:x:
1007:1007::/home/user5:/bin/bash
user6:x:
1008:
1010::/home/user6:/bin/bash
user7:x:
1009:
1009::/home/user7:/bin/bash
#
adduser user8
# tail -5 /etc/passwd
user4:x:1006:1002::/home/user444:/sbin/nologin
user5:x:1007:1007::/home/user5:/bin/bash
user6:x:
1008:
1010::/home/user6:/bin/bash
user7:x:
1009:
1009::/home/user7:/bin/bash
user8:x:
1010:
1011::/home/user8:/bin/bash
# tail -5 /etc/group
user2:x:1003:
user5:x:1007:
grp3:x:1010:
user7:x:1009:
user8:x:1011:
-G, --groupsGROUP1[,GROUP2,...[,GROUPN]]]
用户还属于的附加组列表。
每个组都用逗号隔开,没有中间的空格。这里的组受到了 -g
选项给定的组同样的限制。默认上,用户只属于初始组。
userdel - 删除用户账户和相关文件
# userdel user8
[root@aminglinux-01 ~]# tail -2 /etc/passwd
user6:x:1008:1010::/home/user6:/bin/bash
user7:x:1009:1009::/home/user7:/bin/bash
# ls /home/
aming user1 user2 user3 user444 user6 user7 user8
-r, --remove
用户主目录中的文件将随用户主目录和用户邮箱一起删除。
在其它文件系统中的文件必须手动搜索并删除
# userdel -r user7
# ls /home/
aming user1 user2 user3 user444 user6