命令介绍
命令:useradd 添加用户
用法:useradd -D [options] useradd 用户
命令选项
[root@fp-21 ~]# useradd --help
-c, --comment COMMENT # 添加备注信息
-d, --home-dir HOME_DIR # 指定用户的家目录
-D, --defaults # 打印或更改用户的配置
-e, --expiredate EXPIRE_DATE # 用户的有效期限
-f, --inactive INACTIVE # 过期缓存天数(过期还能使用多少天)
-g, --gid GROUP # 指定用户所属的群组
-G, --groups GROUPS # 指定用户所属的附加群组
-h, --help # 帮助文档
-l, --no-log-init # 不将用户添加到lastlog和faillog数据库
-m, --create-home # 自动创建家目录
-M, --no-create-home # 不自动创建家目录
-N, --no-user-group # 不创建用户组
-p, --password PASSWORD # 新用户的密码
-r, --system # 建立系统账号
-s, --shell SHELL # 指定用户登陆后使用的shell
-u, --uid UID # 指定用户的UID
-U, --user-group # 创建与用户同名的组
命令实例
# 添加备注信息
[root@fp-21 ~]# useradd -c "This is first user" test01
[root@fp-21 ~]# tail -1 /etc/passwd
test01:x:1001:1001:This is first user:/home/test01:/bin/bash
# 指定用户的家目录
[root@fp-21 ~]# useradd -d /opt/test test02
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
# 指定用户所属的群组
[root@fp-21 ~]# useradd -g test01 test04
[root@fp-21 ~]# tail -1 /etc/passwd
test04:x:1004:1001::/home/test04:/bin/bash
# 不创建用户组
[root@fp-21 ~]# useradd -N test07
[root@fp-21 ~]# tail -2 /etc/passwd
test07:x:1007:100::/home/test07:/bin/bash
# 指定用户的UID
[root@fp-21 ~]# useradd -u 666 test10
[root@fp-21 ~]# cat /etc/passwd |grep 666
test10:x:666:1010::/home/test10:/bin/bash