Linux创建用户和密码相关的命令useradd、groupadd、passwd和userdel应用
admin
2024-02-25 01:36:44
0

记录: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] GROUPOptions:-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] LOGINuseradd -Duseradd -D [options]Options:-b, --base-dir BASE_DIR       base directory for the home directory of thenew 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 newaccount-G, --groups GROUPS           list of supplementary groups of the newaccount-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 andfaillog 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 asthe 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...] -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] LOGINOptions:-f, --force                   force some actions that would fail otherwisee.g. removal of user still logged inor 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日

相关内容

热门资讯

韩国股市暴跌触发熔断机制 △韩国证券交易所(资料图) 韩国综合股价指数(KOSPI)7日盘中一度大跌超8%,触发市场熔断机制。...
爱柯迪跌3.11%,成交额1.... 来源:新浪证券-红岸工作室 7月7日,爱柯迪跌3.11%,成交额1.57亿元,换手率1.08%,总市...
高善文去世,享年55岁 记者据多方证实,国投证券前首席经济学家高善文因病去世,享年55岁。 高善文被公认为是中国资本市场上颇...
南昌黄金回收怎么选更安心 附近... 近年来随着黄金价格波动,不少南昌市民有处置闲置黄金的需求,但黄金回收市场鱼龙混杂,不少用户曾遭遇套路...
SpaceX股票几乎回到首日开... 来源:环球市场播报 SpaceX股价今日下跌逾5%,至152美元以下,距离该火箭制造商在6月12日...
景泽生物三闯港股IPO:尚未实... 财通社——7月3日,景泽生物医药(合肥)股份有限公司(简称“景泽生物”)再次向香港交易所递交主板上市...
亚马逊拟发行债券募资至少250... 来源:环球市场播报 核心要点 消息人士向记者戴维・费伯透露,亚马逊计划通过债券发行募资至少 25...
一季净利暴涨3020%!又一存... 作者 | 铅笔道 松格 编辑 | 铅笔道 黄小贵 2025年前四个月,宏芯宇的净利润只有1.23亿元...
原创 暴... 2026年夏天,饮料这行的人普遍难受。以往一到六七月,仓库就得连轴转,货车排队装水,老板数钱数到手抽...
活力中国调研行|探访李政道研究... 东方网记者程琦7月7日报道:初夏的张江科学城,草木葱茏,创新活力跃动。2026年“活力中国调研行”上...
超5家上市银行密集“自购”,释... 下半年伊始,A股上市银行再度传来股东、高管增持的消息。 常熟银行日前接连发布两份公告称,为提升持股比...
经济学家高善文去世,享年55岁 7月7日晚,据新经济学家智库消息,著名经济学家高善文去世,享年55岁。 公开资料显示,高善文出生于1...
连板背后的资本棋局:华芯晶电实... 图片来源:视觉中国 蓝鲸新闻7月7日讯(记者 徐晓春)因筹划控制权变更停牌五个交易日后,阿科力(60...
AI高端对话:智能体“凉了”?... 智东西 作者 | 杨京丽 编辑 | 心缘 智东西7月7日报道,7月2日,2026中国智能体大会在杭州...
著名经济学家高善文因病去世,享... 据上海证券报7月7日报道,记者据多方证实,国投证券前首席经济学家高善文因病去世,享年55岁。 高善...
医药股跨界并购半导体,周三停牌 7月7日晚间,五洲医疗(301234)发布《关于筹划发行股份及支付现金购买资产事项的停牌公告》。公告...
原创 上... 同一条马路两头,售楼处里排号验资的人挤到门口,中介橱窗上的挂牌广告却贴了一层又一层没人问。这就是20...
A股交易规则三大修订 投资必看 今天开始,A股市场的全新交易规则正式落地执行。A股交易新规涉及多项内容,和投资者密切相关,具体有哪些...
原创 金... 金价这两天走得有点拧巴。 7月6日现货黄金盘初一口气冲到4202.09美元/盎司,创了两周新高,谁料...
韩股两度熔断!赚疯的三星,吓崩... 三星一跃成为“全球最赚钱巨头”,韩国股市反倒崩了。 周二,亚太市场集体下挫,韩国股市两度上演暴跌熔断...