Linux基础命令[20]-useradd

本文详细介绍了Linux中的useradd命令,包括其基本用法、参数说明(如指定家目录、用户组、密码有效期等)、示例以及如何创建系统用户和指定UID。重点讲解了如何通过选项定制新用户的设置。
摘要由CSDN通过智能技术生成

1. useradd 命令说明

useradd:用于创建用户账号。基本信息如下:

NAME
       useradd - create a new user or update default new user information

SYNOPSIS
       useradd [options] LOGIN

       useradd -D

       useradd -D [options]

DESCRIPTION
       When invoked without the -D option, the useradd command creates a new user account using the values specified on the
       command line plus the default values from the system. Depending on command line options, the useradd command will update
       system files and may also create the new user´s home directory and copy initial files.

       By default, a group will also be created for the new user (see -g, -N, -U, and USERGROUPS_ENAB).

OPTIONS
       The options which apply to the useradd command are:

       -b, --base-dir BASE_DIR
           The default base directory for the system if -d HOME_DIR is not specified.  BASE_DIR is concatenated with the account
           name to define the home directory. The BASE_DIR must exist otherwise the home directory cannot be created.           If this option is not specified, useradd will use the base directory specified by the HOME variable in
           /etc/default/useradd, or /home by default.

       -c, --comment COMMENT
           Any text string. It is generally a short description of the login, and is currently used as the field for the user´s
           full name.

       -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.

       -D, --defaults
           See below, the subsection "Changing the default values".

       -e, --expiredate EXPIRE_DATE
           The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD.

           If not specified, useradd will use the default expiry date specified by the EXPIRE variable in /etc/default/useradd, or
           an empty string (no expiry) by default.

       -f, --inactive INACTIVE
           The number of days after a password expires until the account is permanently disabled. A value of 0 disables the
           account as soon as the password has expired, and a value of -1 disables the feature.

           If not specified, useradd will use the default inactivity period specified by the INACTIVE variable in
           /etc/default/useradd, or -1 by default.

部分参数如下:

选项作用
-b新帐户主目录的基本目录
-c新帐户的GECOS字段
-d新帐户的主目录
-D打印或更改默认用户添加配置
-e新帐户的到期日期
-f新帐户的密码失效期
-g新帐户的主组的名称或ID
-G新帐户的附属组列表
-m创建用户的主目录
-M不要创建用户的主目录
-N不要创建与用户同名的组
-p新帐户的加密密码
-r创建一个系统帐户
-u新帐户的用户ID
-U创建与用户同名的组

2. useradd 命令语法

useradd [选项] 用户

3. useradd 命令示例

3.1 不加参数

执行 useradd 命令需要管理员用户,普通用户是没有权限的,不加参数时,useradd 会直接创建新用户、新用户家目录及新用户组。

useradd tuser

在这里插入图片描述

3.2 -d(指定家目录)

-d:可以为用户指定家目录,不使用默认创建的路径。

注意:

  • 需要指定的那个目录不需要提前创建
  • 指定路径需为绝对路径
  • 切换到一个用户后,cd 可以切换到该用户的家目录
useradd -d 绝对路径 用户
[root@bogon ~]# cd /home
[root@bogon home]# ls
root2  test  tuser
[root@bogon home]# mkdir thome
[root@bogon home]# useradd -d t
test/  thome/ tuser/ 
[root@bogon home]# useradd -d thome/ tuser2
useradd: invalid home directory 'thome/'
[root@bogon home]# useradd -d /home/thome/ tuser2
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
[root@bogon home]# useradd -d /home/thome2/ tuser2
useradd: user 'tuser2' already exists
[root@bogon home]# useradd -d /home/thome3/ tuser3
[root@bogon home]# ls
root2  test  thome  thome3  tuser
[root@bogon home]# su tuser3
[tuser3@bogon home]$ cd
[tuser3@bogon thome3]$ pwd
/home/thome3
[tuser3@bogon thome3]$ 

在这里插入图片描述

3.3 -g(指定用户组)

-g:为新建用户指定用户组,若用户组不存在会有提示。

useradd -g 用户组 用户
[root@bogon thome3]# id tuser
uid=1002(tuser) gid=1002(tuser) groups=1002(tuser)
[root@bogon thome3]# useradd -g tuser tuser3
useradd: user 'tuser3' already exists
[root@bogon thome3]# useradd -g tuser tuser4
[root@bogon thome3]# id tuser
uid=1002(tuser) gid=1002(tuser) groups=1002(tuser)
[root@bogon thome3]# id tuser4
uid=1008(tuser4) gid=1002(tuser) groups=1002(tuser)
[root@bogon thome3]# useradd -g tuser5 tuser4
useradd: group 'tuser5' does not exist
[root@bogon thome3]# 

在这里插入图片描述

3.4 -G(指定附属组)

-G:为新用户指定附属组

useradd -G 用户组 用户
[root@bogon thome3]# useradd -G tuser tuser5
[root@bogon thome3]# id tuser5
uid=1009(tuser5) gid=1009(tuser5) groups=1009(tuser5),1002(tuser)
[root@bogon thome3]# su tuser5
[tuser5@bogon thome3]$ cd
[tuser5@bogon ~]$ pwd
/home/tuser5
[tuser5@bogon ~]$ 

在这里插入图片描述

3.5 -p(加密密码)

-p:为用户添加加密密码,不是明文密码。

cat /etc/shadow

可以获得各用户加密后的密码

root:$5$nW3ljdHa.SDBXFoA$89vRDJqumo41HdzmxC1ySOWZAZfjaK1YFucJkiinPmA::0:99999:7:::
bin:*:18353:0:99999:7:::
daemon:*:18353:0:99999:7:::
adm:*:18353:0:99999:7:::
lp:*:18353:0:99999:7:::
sync:*:18353:0:99999:7:::
shutdown:*:18353:0:99999:7:::
halt:*:18353:0:99999:7:::
mail:*:18353:0:99999:7:::
operator:*:18353:0:99999:7:::
games:*:18353:0:99999:7:::
ftp:*:18353:0:99999:7:::
nobody:*:18353:0:99999:7:::
systemd-network:!!:19803::::::
dbus:!!:19803::::::
polkitd:!!:19803::::::
libstoragemgmt:!!:19803::::::
colord:!!:19803::::::
rpc:!!:19803:0:99999:7:::
gluster:!!:19803::::::
saslauth:!!:19803::::::
abrt:!!:19803::::::
rtkit:!!:19803::::::
radvd:!!:19803::::::
chrony:!!:19803::::::
qemu:!!:19803::::::
ntp:!!:19803::::::
unbound:!!:19803::::::
tss:!!:19803::::::
sssd:!!:19803::::::
usbmuxd:!!:19803::::::
geoclue:!!:19803::::::
setroubleshoot:!!:19803::::::
pulse:!!:19803::::::
saned:!!:19803::::::
gdm:!!:19803::::::
rpcuser:!!:19803::::::
nfsnobody:!!:19803::::::
gnome-initial-setup:!!:19803::::::
sshd:!!:19803::::::
avahi:!!:19803::::::
postfix:!!:19803::::::
tcpdump:!!:19803::::::
root2:$5$nW3ljdHa.SDBXFoA$89vRDJqumo41HdzmxC1ySOWZAZfjaK1YFucJkiinPmA:19803:0:99999:7:::
test:!!:19804:0:99999:7:::
tuser:!!:19804:0:99999:7:::
tuesr:!!:19804:0:99999:7:::
tuesr2:!!:19804:0:99999:7:::
tuesr3:!!:19804:0:99999:7:::
tuser2:!!:19804:0:99999:7:::
tuser3:!!:19804:0:99999:7:::
tuser4:!!:19804:0:99999:7:::
tuser5:!!:19804:0:99999:7:::
tuser6:!!:19804:0:99999:7::18993:
tuser7:123:19804:0:99999:7::18993:
tuser8:1234:19804:0:99999:7::20006:
tuser9:.SDBXFoA9vRDJqumo41HdzmxC1ySOWZAZfjaK1YFucJkiinPmA:19804:0:99999:7:::
tuser10:$5$nW3ljdHa.SDBXFoA$89vRDJqumo41HdzmxC1ySOWZAZfjaK1YFucJkiinPmA:19804:0:99999:7:::

root 用户的明文密码是:123456,加密后成了 5 5 5nW3ljdHa.SDBXFoA$89vRDJqumo41HdzmxC1ySOWZAZfjaK1YFucJkiinPmA

如果使用命令:

useradd -p '$5$nW3ljdHa.SDBXFoA$89vRDJqumo41HdzmxC1ySOWZAZfjaK1YFucJkiinPmA' tuser11

tuser11 的密码就是 123456 了。

从这来看,这个参数很不好用,可以直接使用

passwd 用户

为用户设置密码。

3.6 -e(指定有效期)

测试的时候需要设置密码,就使用了 -p 参数一起。

useradd -p '$5$nW3ljdHa.SDBXFoA$89vRDJqumo41HdzmxC1ySOWZAZfjaK1YFucJkiinPmA' -e 2022-10-10 tuser11

tuser11 的密码就设置为了 123456,但是登录时会提示该用户过期了。

在这里插入图片描述

3.7 -r(系统用户)

-r:创建系统用户,UID 小于 1000,且没有家目录。系统用户一般给程序使用。

useradd -r tuser13
[root@bogon ~]# useradd -r tuser13
[root@bogon ~]# id tuser13
uid=987(tuser13) gid=981(tuser13) groups=981(tuser13)
[root@bogon ~]# cd /home/tuser13
bash: cd: /home/tuser13: No such file or directory
[root@bogon ~]# 

在这里插入图片描述

3.8 -u(指定 UID)

默认情况普通用户的 UID 是自增1的。也可以使用 -u 指定用户 UID

useradd -u 2000 tuser14
[root@bogon ~]# useradd -u 2000 tuser14
[root@bogon ~]# id tuser14
uid=2000(tuser14) gid=2000(tuser14) groups=2000(tuser14)
[root@bogon ~]# 

在这里插入图片描述

4. 总结

useradd 用于创建用户。没有特殊要求的话,

useradd 用户

就够用了。

  • 14
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值