Linux基础命令[20]-useradd

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 用户

就够用了。

相关推荐

  1. Linux20linux常用命令

    2024-03-23 09:18:04       60 阅读
  2. Linux基础命令使用】

    2024-03-23 09:18:04       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-23 09:18:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-23 09:18:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-23 09:18:04       82 阅读
  4. Python语言-面向对象

    2024-03-23 09:18:04       91 阅读

热门阅读

  1. C#如何从上次窗体关闭的位置打开窗体

    2024-03-23 09:18:04       40 阅读
  2. RK3588 rknpu2及rknn-toolkit2使用说明

    2024-03-23 09:18:04       97 阅读
  3. python 创建word并添加文字和统计图

    2024-03-23 09:18:04       39 阅读
  4. c# 结构体(Struct)与枚举(Enum)与记录(Record)

    2024-03-23 09:18:04       41 阅读
  5. AI学习笔记

    2024-03-23 09:18:04       34 阅读
  6. Ubuntu 下conda创建环境失败报错相关问题

    2024-03-23 09:18:04       41 阅读
  7. 简易C语言词法分析程序

    2024-03-23 09:18:04       39 阅读
  8. html5&css&js代码 039 元素尺寸

    2024-03-23 09:18:04       36 阅读
  9. IOS面试题编程机制 11-15

    2024-03-23 09:18:04       36 阅读
  10. 【链表】Leetcode 23. 合并 K 个升序链表【困难】

    2024-03-23 09:18:04       41 阅读
  11. 6-190 先序输出叶节点

    2024-03-23 09:18:04       41 阅读
  12. 【Leetcode】代码随想录D13|栈与队列3.0

    2024-03-23 09:18:04       44 阅读