【SRE笔记 2022.8.24 linux用户组及用户命令】

文件属性 用户和用户组

用户 user

  • 用户的身份标识用UID标识,组标识用GID
  • Linux里有三类用户 User

1)超级用户 超管,

  • a)拥有最高权限 root UID=0;
  • b)实际生产中会禁用root通过ssh登录,通过登录普通用户再切到root。

2)普通用户

  • 普通权限 只对家目录和/tmp目录具有写的权限。
  • 管理员创建,centos6 UID:500-6000,centos7 UID:1000-60000。
  • 与超管之间的切换方式。
  • a)切换root, su - root
  • b)sudo - 命令

3)虚拟用户(傀儡用户)

  • 多数情况就存在,不能登陆。UID :1-499
  • 创建文件和进程启动都需要对应相关的用户和用户组。
  • 虚拟用户的存在就是为满足进程启动时对用户和用户组的要求(普通用户也行),

最小化原则:
1 安装软件最小化
2 登录安全最小化(普通用户)
3 进程启动权限最小化
4 权限最小化

用户配置文件

/etc/passwd

  • 主要配置文件,包含用户的属性。**
[root@aaa ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

用户名称 用户密码 uid gid 用户的说明 用户家目录 用户登录使用的shell

- /etc/shadow

  • 密码文件,存放密码及密码的属性。
[root@aaa ~]# tail -2  /etc/shadow
tcpdump:!!:19219::::::
ntp:!!:19222::::::

用户名 用户密码(加密字符串) 最近更改密码的时间 禁止修改密码的天数 用户必须更改口令的天数 警告更改密码的期限 不活动时间 失效时间 标志

/etc/group 组文件

  • 存放用户组及属性。
[root@aaa ~]# tail -2 /etc/group
ntp:x:38:
slocate:x:21:

组名称 组密码(通常无需设置) GID 用户组成员

/etc/gshadow

  • 组密码文件(不常用)**
[root@aaa ~]# tail -2 /etc/gshadow
ntp:!::
slocate:!::
  • 组名 用户组密码 用户组管理员用户 用户组成员
  • /etc/default/useradd 创建用户命令 useradd配置文件
  • /etc/skel 创建用户环境变量原始文件存放地
  • /ect/login.defs 创建用户系统配置,对应文件。

用户组 Group

  • 用户组是在创建用户时附带产生的。
  • 用户组和用户同名,且GID和UID相同。

用户相关命令

useradd 添加用户

  • 1)直接加用户名添加用户,新建后用id查看。
[root@aaa ~]# useradd aaa
[root@aaa ~]# id aaa
uid=1001(aaa) gid=1001(aaa) groups=1001(aaa)
  • 新加用户后,查看四个关联文件
[root@aaa ~]# tail -n 1 /etc/passwd /etc/shadow /etc/group /etc/gshadow
==> /etc/passwd <==
aaa:x:1001:1001::/home/aaa:/bin/bash

==> /etc/shadow <==
aaa:!!:19228:0:99999:7:::

==> /etc/group <==
aaa:x:1001:

==> /etc/gshadow <==
aaa:!::
  • 2)指定uid添加用户 -u
[root@aaa ~]# useradd -u 5000 bbb
[root@aaa ~]# id 5000
uid=5000(bbb) gid=5000(bbb) groups=5000(bbb)
  • 3)指定用户的登录shell -s
[root@aaa ~]# useradd -s /sbin/nologin ccc
[root@aaa ~]# id ccc
uid=5001(ccc) gid=5001(ccc) groups=5001(ccc)
  • 4)指定用户每次登陆时使用的家目录 -d
[root@aaa ~]# useradd -d /ddd ddd
[root@aaa ~]# tail -n 1 /etc/passwd
ddd:x:5002:5002::/ddd:/bin/bash
  • 5)不建立用户家目录 -M 配合 创建虚拟用户使用
  • 6)创建并制定用户家目录,如家目录不存在
  • 7)-G 指定多个用户组
  • 8)-g 指定用户组
[root@aaa ~]# useradd eee -g  root
[root@aaa ~]# id eee
uid=5003(eee) gid=0(root) groups=0(root)
  • 9)添加用户设置过期时间 -e 企业中给非运维人员创建制定账户定期收回
[root@aaa ~]# useradd -e "2022/08/25"  fff

useradd的三个配置文件

  • /etc/default/useradd 创建用户命令 useradd配置文件。
  • 查看该文件
[root@aaa ~]# cat /etc/default/useradd 
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1   #停权
EXPIRE=     #过期时间
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

该文件是useradd + 用户 使用时,创建的用户默认带的参数,如修改过期时间参数后,再次创建的用户自动待参数。

  • /etc/skel 创建用户环境变量原始文件存放地
    每个用户家目录都会默认有.bash_logout(退出的环境变量),.bash_profile,.bashrc,创建用户的同事,从/etc/skel 里复制到用户家目录下的
[root@aaa ~]# ll -a /home/bbb/
total 12
drwx------. 2 bbb  root  62 Aug 24 22:59 .
drwxr-xr-x. 6 root root  53 Aug 27 00:51 ..
-rw-r--r--. 1 bbb  root  18 Nov 25  2021 .bash_logout
-rw-r--r--. 1 bbb  root 193 Nov 25  2021 .bash_profile
-rw-r--r--. 1 bbb  root 231 Nov 25  2021 .bashrc
[root@aaa ~]# id bbb
uid=8000(bbb) gid=0(root) groups=0(root)
[root@aaa ~]# ls /etc/skel/ -a
.  ..  .bash_logout  .bash_profile  .bashrc
  • /ect/login.defs 创建用户系统配置,对应文件。
root@aaa /]# cat /etc/login.defs 
  • 部分内容如下。
# Password aging controls:      # 密码限制
#
#	PASS_MAX_DAYS	Maximum number of days a password may be used.
#	PASS_MIN_DAYS	Minimum number of days allowed between password changes.
#	PASS_MIN_LEN	Minimum acceptable password length.
#	PASS_WARN_AGE	Number of days warning given before a password expires.
#
PASS_MAX_DAYS	99999
PASS_MIN_DAYS	0
PASS_MIN_LEN	5
PASS_WARN_AGE	7
#####################
# Min/max values for automatic uid selection in useradd   #UID限制
#
UID_MIN                  1000
UID_MAX                 60000
# System accounts
SYS_UID_MIN               201
SYS_UID_MAX               999
########################
# Min/max values for automatic gid selection in groupadd    
#
GID_MIN                  1000
GID_MAX                 60000
# System accounts
SYS_GID_MIN               201
SYS_GID_MAX               999

等等。。。。

userdel 删除用户

  • -r 删除家目录
[root@aaa ~]# userdel -r eee
  • 企业中人员离职,直接删除其用户,家目录保留。或者不让登录,或者在/etc/passwd中注释掉。

usermod 修改用户

用法和useradd类似,命令也类似

  • 1)修改UID
[root@aaa ~]# id bbb
uid=5000(bbb) gid=5000(bbb) groups=5000(bbb)
[root@aaa ~]# usermod -u 8000 bbb
[root@aaa ~]# id bbb
uid=8000(bbb) gid=5000(bbb) groups=5000(bbb)
  • 2)修改所属组
[root@aaa ~]# usermod -g root bbb
[root@aaa ~]# id bbb
uid=8000(bbb) gid=0(root) groups=0(root)
  • 3)修改过期时间 -e
[root@aaa ~]# chage -l ccc
Last password change					: Aug 24, 2022
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@aaa ~]# usermod -e "2022/08/29"  ccc
[root@aaa ~]# chage -l ccc
Last password change					: Aug 24, 2022
Password expires					: never
Password inactive					: never
Account expires						: Aug 29, 2022   # 用户过期时间
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
  • 4)修改用户shell解释器
[root@aaa ~]# grep ccc /etc/passwd
ccc:x:5001:5001::/home/ccc:/sbin/nologin
[root@aaa ~]# usermod -s /bin/bash ccc
[root@aaa ~]# grep ccc /etc/passwd
ccc:x:5001:5001::/home/ccc:/bin/bash

cp备份命令快捷用法

[root@aaa ~]# cp file1{,.ori}
[root@aaa ~]# ll
total 4
-rw-r--r--. 1 root root 191 Aug 23 02:21 data.tar.gz
-rw-r--r--. 1 root root   0 Aug 27 01:07 file1
-rw-r--r--. 1 root root   0 Aug 27 01:08 file1.ori

遇到的问题1

如果切换至该用户出现报错 -bash-4.1$ ,是缺少用户环境变量所致,如下,需要将/etc/skel/下的三个文件,bash_logout(退出的环境变量),.bash_profile,.bash_profile,拷贝到当前目录。且,该三个文件的用户主和组都为该用户,即用户家目录没有这三个文件会有如下显示。

[root@aaa ~]# id aaa
uid=1001(aaa) gid=1001(aaa) groups=1001(aaa)
[root@aaa ~]# usermod -d /fx aaa
[root@aaa ~]# grep aaa /etc/passwd
aaa:x:1001:1001::/fx:/bin/bash
[root@aaa ~]# su - aaa
Last login: Sat Aug 27 01:21:39 CST 2022 on pts/0
su: warning: cannot change directory to /fx: No such file or directory
-bash-4.2$    

遇到的问题2

root@aaa ~]# userdel -r fff
userdel: user fff is currently used by process 2554
[root@aaa ~]# ps aux | grep 2554
fff        2554  0.0  0.1 116340  2924 pts/0    S    Aug26   0:00 -bash
root       2813  0.0  0.0 112812   980 pts/0    S+   00:43   0:00 grep --color=auto 2554
[root@aaa ~]# kill 2554
[root@aaa ~]# ps aux | grep 2554
fff        2554  0.0  0.1 116340  2924 pts/0    S    Aug26   0:00 -bash
root       2815  0.0  0.0 112812   980 pts/0    R+   00:43   0:00 grep --color=auto 2554
[root@aaa ~]# userdel -r eee
[root@aaa ~]# usedel -r fff
-bash: usedel: command not found
[root@aaa ~]# userdel -r fff
userdel: user fff is currently used by process 2554
[root@aaa ~]# su - fff
Last login: Sat Aug 27 00:00:02 CST 2022 on pts/0
Last failed login: Sat Aug 27 00:00:51 CST 2022 from aaa on ssh:notty
There was 1 failed login attempt since the last successful login.
[fff@aaa ~]$ exit
logout
[root@aaa ~]# userdel -r fff
userdel: user fff is currently used by process 2554
[root@aaa ~]# userdel -r fff
userdel: user fff is currently used by process 2554
[root@aaa ~]# su - fff
Last login: Sat Aug 27 00:48:57 CST 2022 on pts/0
[fff@aaa ~]$ logout
[root@aaa ~]# userdel -r fff
userdel: user fff is currently used by process 2554
[root@aaa ~]# quit
-bash: quit: command not found
[root@aaa ~]# exit
logout
[fff@aaa ~]$ exit
logout
You have new mail in /var/spool/mail/root
[root@aaa ~]# userdel -r fff
userdel: user fff is currently used by process 2554
[root@aaa ~]# exit
logout
[fff@aaa ~]$ exit
logout
You have new mail in /var/spool/mail/root
[root@aaa ~]# exit
logout
You have new mail in /var/spool/mail/root
[ddd@aaa root]$ userdel -r fff
userdel: Permission denied.
userdel: cannot lock /etc/passwd; try again later.
[ddd@aaa root]$ exit
exit
You have new mail in /var/spool/mail/root
[root@aaa ~]# userdel -r fff
[root@aaa ~]# id fff
id: fff: no such user
  • 定时fff用户处于登录状态,需要多次logout登出后即可。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学习使我清醒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值