linux下添加用户组和用户
1,linux下文件权限管理,添加用户组并添加用户
添加组:groupadd 组名
groupadd deploy
cat /etc/group | grep deploy
如图:
删除组:groupdel 组名
groupdel deploy
cat /etc/group | grep deploy
查询组:cat /etc/group 或者使用管道来精确查询 cat /etc/group | grep dev
如图:
/etc 目录是专门用来保存 系统配置信息 的目录
group 是保存组信息的文件
2,添加用户
添加用户:useradd -m -g 组 新建用户名 注意:-m 自动建立用户家目录; -g 指定用户所在的组,否则会建立一个和用户名同名的组
useradd -m -g deploy test
deploy 为用户组,test1为用户名
查询用户:在/etc/passwd文件下存储的是用户的list
cat /etc/passwd
如图:
只查找deploy用户
id deploy
如图:
3,修改和创建密码 passwd 用户名 如果不加用户名则默认修改当前登录者的密码
[root@Server-n93yom ~] passwd test1
Changing password for user test1.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@Server-n93yom ~]#
设置好密码后,使用此账号和密码登录
➜ ~ ssh test1@192.168.85.163
test1@192.168.85.163's password:
Last failed login: Mon Jul 22 17:00:05 CST 2019 from 192.168.1.53 on ssh:notty
There were 3 failed login attempts since the last successful login.
[test1@Server-n93yom ~]$
设置用户不能修改密码
[root@Server-n93yom ~] passwd -l test1 //在root下,禁止test1用户修改密码的权限
Locking password for user test1. //锁住test1不能修改密码
passwd: Success
[root@Server-n93yom ~] su test1 //切换用户
[test1@Server-n93yom root]$ passwd //修改密码
Changing password for user test1.
Changing password for test1.
(current) UNIX password:
passwd: Authentication token manipulation error //没用权限修改密码
[test1@Server-n93yom root]$
清除密码
[root@Server-n93yom ~] passwd -d test1 //删除test1的密码
Removing password for user test1.
passwd: Success
[root@Server-n93yom ~] passwd -S test1 //查看test1的密码
test1 NP 2019-07-22 0 99999 7 -1 (Empty password.) //密码为空
[root@Server-n93yom ~]#
passwd 帮助命令
[root@Server-n93yom ~] passwd --help
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
[root@Server-n93yom ~]#
4,设置密码失效时间
可以编辑/etc/login.defs来设定几个参数,以后设置口令默认就按照参数设定为准:
PASS_MAX_DAYS 99999
PASS_MIN_DAYS 0
PASS_MIN_LEN 5
PASS_WARN_AGE 7
当然在/etc/default/useradd可以找到如下2个参数进行设置:
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes
通过修改配置文件,能对之后新建用户起作用,而目前系统已经存在的用户,则直接用chage来配置。
chage [选项] 用户名
chage命令是用来修改帐号和密码的有效期限。
-m:密码可更改的最小天数。为零时代表任何时候都可以更改密码。
-M:密码保持有效的最大天数。
-w:用户密码到期前,提前收到警告信息的天数。
-E:帐号到期的日期。过了这天,此帐号将不可用。
-d:上一次更改的日期。
-i:停滞时期。如果一个密码已过期这些天,那么此帐号将不可用。
-l:例出当前的设置。由非特权用户来确定他们的密码或帐号何时过期。
chage -l root 查root账号的信息
[root@Server-n93yom ~] chage -l root
Last password change : Jul 22, 2019
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
[root@Server-n93yom ~]#
chage -M 60 test 设置密码过期时间为60天
chage -I 5 test 设置密码失效时间为5天
以test1账号为例,再查一次信息账号信息
[root@Server-n93yom ~] chage -l test1
Last password change : Jul 22, 2019
Password expires : Sep 20, 2019
Password inactive : Sep 25, 2019
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 60
Number of days of warning before password expires : 7
从上述命令可以看到,在密码过期后5天,密码自动失效,这个用户将无法登陆系统了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~