Linux创建用户和密码相关的命令useradd、groupadd、passwd和userdel应用

记录:351

场景:在CentOS 7.9操作系统上,使用useradd命令创建用户;使用groupadd命令创建用户组;使用passwd命令修改密码、锁定密码、设置密码有效时间等;使用userdel删除用户。

版本:

操作系统:CentOS 7.9

1.groupadd命令应用

(1)创建用户组

命令:groupadd hangzhou

解析:创建一个用户组,名称为hangzhou。

(2)强制创建用户组

命令:groupadd -f hangzhou

解析:创建一个用户组,名称为hangzhou;-f,强制创建。

2.useradd命令应用

(1)创建用户

命令:useradd hangzhou

解析:创建一个用户,hangzhou是用户名。

(2)创建用户

命令:useradd -g hangzhou hangzhou -d /home/hangzhou

解析:创建一个用户,-g hangzhou,指定用户组是hangzhou;第二个hangzhou是用户名;-d,指定创建用户的home目录。

(3)创建用户

命令:useradd -g hangzhou xihu -d /home/xihu

解析:创建一个用户,-g hangzhou,指定用户组是hangzhou;xihu是用户名;-d,指定创建用户的home目录。在hangzhou用户组下创建了两个用户。

(4)创建用户不创建home目录

命令:useradd zhejiang -M

解析:创建用户,没有home目录。

(5)创建系统用户

命令:useradd -r binjiang

解析:创建系统用户,没有home目录,系统用户序号在1000以内,普通用户从1000开始。

3.passwd命令应用

(1)修改密码

命令格式:passwd 用户名

命令:passwd hangzhou

解析:执行命令时,命令行提示New password:,然后输入密码就行。

(2)强制修改密码(-f选项)

命令:passwd -f hangzhou

解析:强制修改密码;执行命令时,命令行提示New password:,然后输入密码就行。

(3)从输入流修改密码

命令:passwd -stdin hangzhou

解析:这种方式修密码,能看到输入字符串,其它方式修改密码是看不到输入的字符串。

(4)删除密码

命令:passwd -d hangzhou

解析:删除指定用户的密码,只能是root用户操作。

(5)锁定密码

命令:passwd -l hangzhou

解析:锁定指定用户的密码,只能是root用户操作。

(6)解锁密码

命令:passwd -u hangzhou

解析:解锁指定用户的密码,只能是root用户操作。

(7)设置密码失效

命令:passwd -e hangzhou

解析:设置指定用户的密码,只能是root用户操作。

(8)查看密码状态

命令:passwd -S hangzhou

解析:查看指定用户的密码状态,只能是root用户操作。

默认状态:hangzhou PS 1970-01-01 0 99999 7 -1 (Password set, SHA512 crypt.)

解析:从1970-01-01开始,99999天。

(9)设置密码最大有效时间

命令:passwd -x 30 hangzhou

解析:设置密码最大有效时间,只能是root用户操作。

查看状态:passwd -S hangzhou

状态信息:hangzhou PS 2022-11-28 0 30 7 -1 (Password set, SHA512 crypt.)

(10)设置密码生效开始时间

命令:passwd -n 3 hangzhou

解析:设置密码生效开始时间,只能是root用户操作。

查看状态:passwd -S hangzhou

状态信息:hangzhou PS 2022-11-28 3 30 7 -1 (Password set, SHA512 crypt.)

解析:从设置时间开始3天后才有效。

(11)设置密码到期警告时间

命令:passwd -w 5 hangzhou

解析:设置密码到期前5天提示警告修改密码,只能是root用户操作。

查看状态:passwd -S hangzhou

状态信息:hangzhou PS 2022-11-28 3 30 5 -1 (Password set, SHA512 crypt.)

解析:密码到期前5天会开始提示修改密码。

(12)设置帐户被禁用时密码过期后的天数

命令:passwd -i 6 hangzhou

解析:设置密码生效开始时间,只能是root用户操作。

查看状态:passwd -S hangzhou

状态信息:hangzhou PS 2022-11-28 3 30 5 6 (Password set, SHA512 crypt.)

解析:帐户被禁用时密码过期后的天数。

(13)保留未过期的身份验证令牌

命令:passwd -k hangzhou

解析:保留未过期的身份验证令牌。

4.userdel命令

(1)删除用户

命令:userdel hangzhou

解析:删除用户。

(2)删除用户包括home目录

命令:userdel -r hangzhou

解析:删除用户,删除对应home目录。

5.查看用户和密码

(1)查看group文件

查看用户组:cat /etc/group | grep hangzhou

解析:使用cat和grep查看已经创建的用户组。

(2)查看passwd文件

查看用户和密码:cat /etc/passwd

查看用户和密码:cat /etc/passwd | grep xihu

解析:使用cat和grep查看已经创建的用户组。

6.命令帮助手册

(1)groupadd命令帮助手册

命令:groupadd --help

解析:查看groupadd支持的全部命令和选项,在实际工作中,查看这个手册应该是必备之选。

Usage: groupadd [options] GROUP

Options:
  -f, --force                   exit successfully if the group already exists,
                                and cancel -g if the GID is already used
  -g, --gid GID                 use GID for the new group
  -h, --help                    display this help message and exit
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -o, --non-unique              allow to create groups with duplicate
                                (non-unique) GID
  -p, --password PASSWORD       use this encrypted password for the new group
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       directory prefix

(2)useradd命令帮助手册

命令:useradd --help

解析:查看useradd支持的全部命令和选项,在实际工作中,查看这个手册应该是必备之选。

Usage: useradd [options] LOGIN
       useradd -D
       useradd -D [options]

Options:
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -G, --groups GROUPS           list of supplementary groups of the new
                                account
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                                the user
  -o, --non-unique              allow to create users with duplicate
                                (non-unique) UID
  -p, --password PASSWORD       encrypted password of the new account
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account
  -U, --user-group              create a group with the same name as the user
  -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping

(3)passwd命令帮助手册

命令:passwd --help

解析:查看passwd支持的全部命令和选项,在实际工作中,查看这个手册应该是必备之选。

Usage: passwd [OPTION...] <accountName>
  -k, --keep-tokens       keep non-expired authentication tokens
  -d, --delete            delete the password for the named account (root only)
  -l, --lock              lock the password for the named account (root only)
  -u, --unlock            unlock the password for the named account (root only)
  -e, --expire            expire the password for the named account (root only)
  -f, --force             force operation
  -x, --maximum=DAYS      maximum password lifetime (root only)
  -n, --minimum=DAYS      minimum password lifetime (root only)
  -w, --warning=DAYS      number of days warning users receives before password expiration (root only)
  -i, --inactive=DAYS     number of days after password expiration when an account becomes disabled (root only)
  -S, --status            report password status on the named account (root only)
  --stdin                 read new tokens from stdin (root only)

Help options:
  -?, --help              Show this help message
  --usage                 Display brief usage message

(4)userdel命令帮助手册

命令:userdel --help

解析:查看userdel支持的全部命令和选项,在实际工作中,查看这个手册应该是必备之选。

Usage: userdel [options] LOGIN

Options:
  -f, --force                   force some actions that would fail otherwise
                                e.g. removal of user still logged in
                                or files, even if not owned by the user
  -h, --help                    display this help message and exit
  -r, --remove                  remove home directory and mail spool
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -Z, --selinux-user            remove any SELinux user mapping for the user

以上,感谢。

2022年11月28日

  • 4
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值