useradd [OPTION] USERNAME用户创建
[OPTION]
-u “指定UID”
[ root@centos7 ~] $useradd -u 8888 youname
[ root@centos7 ~] $id youname
uid= 8888( youname) gid= 8888( youname) groups= 8888( youname)
-o “配合-u 选项,不检查UID的唯一性”
` ID冲突,使用-o可以创建相同ID号的用户`
[ root@centos7 ~] $useradd -u 8888 youname
[ root@centos7 ~] $id youname
uid= 8888( youname) gid= 8888( youname) groups= 8888( youname)
[ root@centos7 ~] $useradd -u 8888 younameagain
useradd: UID 8888 is not unique
[ root@centos7 ~] $useradd -ou 8888 younameagain
[ root@centos7 ~] $id younameagain
uid= 8888( youname) gid= 8888( youname) groups= 8888( youname)
-g GID “指明用户所属基本组,可为组名,也可以GID”
` 指定组需要组存在`
[ root@centos7 ~] $useradd -g 8888 yourname
useradd: group '8888' does not exist
[ root@centos7 ~] $group
bash: group: command not found.. .
[ root@centos7 ~] $useradd -g 100 yourname
[ root@centos7 ~] $id yourname
uid= 1001( yourname) gid= 100( users) groups= 100( users)
-c COMMENT “用户的注释信息”
[ root@centos7 ~] $useradd -c "your COMMENT" yourname
[ root@centos7 ~] $cat /etc/passwd | grep yourname
yourname:x:1002:1002:your COMMENT:/home/yourname1:/bin/bash
-d HOME_DIR “以指定的路径(不存在)为家目录”
` 指定用户的家目录,并自动创建用户家目录`
[ root@centos7 ~] $useradd -d /data/wang wang
[ root@centos7 ~] $id wang
uid= 1003( wang) gid= 1003( wang) groups= 1003( wang)
[ root@centos7 ~] $cat /etc/passwd | grep wang
wang:x:1003:1003::/data/wang:/bin/bash
[ root@centos7 ~] $ls /data/
wang
-s SHELL “指明用户的默认shell程序,可用列表在/etc/shells文件中”
[ root@centos7 ~] $user -s /bin/nologin wang
bash: user: command not found.. .
[ root@centos7 ~] $cat /etc/passwd | grep wang
wang:x:1003:1003::/home/wang:/bin/nologin
-G GROUP1[,GROUP2,…] “为用户指明附加组,组须事先存在”
[ root@centos7 ~] $useradd -G 1000,1002 lee
[ root@centos7 ~] $id lee
uid= 1004( lee) gid= 1004( lee) groups= 1004( lee) ,1000( chen) ,1002( yourname1)
-N “不创建私用组做主组,使用users组做主组”
[ root@centos7 ~] $useradd -N yourname
[ root@centos7 ~] $id yourname
uid= 1001( yourname) gid= 100( users) groups= 100( users)
-r “创建系统用户 CentOS 6: ID<500,CentOS 7: ID<1000”
` CentOS7版本`
[ root@centos7 ~] $useradd -r wang
[ root@centos7 ~] $id wang
uid= 988( wang) gid= 982( wang) groups= 982( wang)
-M “不创建家目录,用于非系统用户”
[ root@centos7 ~] $useradd -M you
[ root@centos7 ~] $cat /etc/passwd | grep you
yourname:x:1001:1001::/home/yourname:/bin/bash
you:x:1002:1002::/home/you:/bin/bash
[ root@centos7 ~] $ls /home
-D “显示或更改默认设置”
默认值设定目录:/etc/default/useradd
useradd -D
显示默认设置 useradd –D [OPTION]
更改默认设置
[OPTION]与上述相同
[ root@centos7 ~] $useradd -D
GROUP= 100
HOME= /home
INACTIVE= -1
EXPIRE=
SHELL= /bin/bash
SKEL= /etc/skel
CREATE_MAIL_SPOOL= yes
[ root@centos7 ~] $useradd -D -s /bin/nologin
[ root@centos7 ~] $useradd -D
GROUP= 100
HOME= /home
INACTIVE= -1
EXPIRE=
SHELL= /bin/nologin
SKEL= /etc/skel
CREATE_MAIL_SPOOL= yes
[ root@centos7 ~] $useradd -D -b /root
[ root@centos7 ~] $useradd -D
GROUP= 100
HOME= /root
INACTIVE= -1
EXPIRE=
SHELL= /bin/bash
SKEL= /etc/skel
CREATE_MAIL_SPOOL= yes
▼应用示例
` 创建用户ID为1888,组ID为100,描述为“The tesxt user",家目录指定在/home/textdir,指定shell为nologin`
[ root@centos7 ~] $useradd -u 1888 -g 100 -c "The text user" -d /home/textdir -s /bin/nologin/ test_user
[ root@centos7 ~] $cat /etc/passwd | grep test
test_user:x:1888:100:The text user:/home/textdir:/bin/nologin/
▷更多用户管理相关命令: