Linux命令详解用法_useradd [OPTION] USERNAME

本文详细介绍了Linux系统下useradd命令的使用方法,包括各种选项参数的含义及实际应用示例,如创建用户、指定UID、组ID、家目录、默认shell等,适合系统管理员和Linux用户深入理解用户管理。

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/

▷更多用户管理相关命令:

修改用户信息
usermod [OPTION] USERNAME
删除用户
userdel [OPTION] USERNAME
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十三y

你的鼓励就是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值