Linux系统之高级用户组和权限管理
【摘要】 Linux系统之高级用户组和权限管理
@TOC
一、用户的密码策略设置
1.用户的密码文件
[root@tianyi ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
unbound:x:997:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
libstoragemgmt:x:996:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
setroubleshoot:x:995:992::/var/lib/setroubleshoot:/sbin/nologin
clevis:x:994:990:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/sbin/nologin
cockpit-ws:x:993:989:User for cockpit-ws:/:/sbin/nologin
sssd:x:992:988:User for sssd:/:/sbin/nologin
insights:x:991:987:Red Hat Insights:/var/lib/insights:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
pesign:x:990:986:Group for the pesign signing daemon:/var/run/pesign:/sbin/nologin
chrony:x:989:985::/var/lib/chrony:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
admin:x:1000:1000::/home/admin:/bin/bash
nginx:x:988:984:Nginx web server:/var/lib/nginx:/sbin/nologin
mysql:x:27:27:MySQL Server:/var/lib/mysql:/sbin/nologin
zabbix:x:987:983:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
~
2.用户的密码期限配置
①查看用户密码期限
[root@tianyi ~]#
[root@tianyi ~]# chage -l user1
Last password change : Nov 25, 2025 #最后一次密码变更时间
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@tianyi ~]#
②修改密码期限
[root@tianyi ~]# chage -m 0 -M 90 -W7 -I 14 user1
[root@tianyi ~]# chage -l user1
Last password change : Nov 25, 2025
Password expires : Feb 23, 2026
Password inactive : Mar 09, 2026
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 90
Number of days of warning before password expires : 7
③强制用户下一次修改密码
[root@tianyi ~]# chage -d 0 user1
④用户到期时间设置
[root@tianyi ~]# chage -E 2021-10-01 user1
[root@tianyi ~]# chage -l user1
Last password change : password must be changed
Password expires : password must be changed
Password inactive : password must be changed
Account expires : Oct 01, 2021
Minimum number of days between password change : 0
Maximum number of days between password change : 90
Number of days of warning before password expires : 7
3.查看当前用户存在
[root@tianyi ~]# id user1
uid=1001(user1) gid=1001(user1) groups=1001(user1)
[root@tianyi ~]#
二、监控系统用户登录
1.查看当前登录用户
[root@tianyi ~]# w
11:54:10 up 89 days, 20:42, 1 user, load average: 0.04, 0.05, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 183.92.102.127 11:14 0.00s 0.06s 0.00s w
2.用户登录和重启记录
[root@node1 ~]# last |head
root pts/0 192.168.200.1 Fri Sep 17 22:32 still logged in
reboot system boot 4.18.0-80.el8.x8 Fri Sep 17 22:29 still running
root pts/0 192.168.200.1 Mon Sep 13 20:28 - crash (4+02:00)
reboot system boot 4.18.0-80.el8.x8 Mon Sep 13 20:22 still running
root pts/2 192.168.200.1 Wed Sep 1 20:19 - 01:04 (04:45)
root pts/1 192.168.200.1 Mon Aug 30 23:11 - 22:59 (1+23:47)
root pts/0 192.168.200.1 Mon Aug 30 20:04 - 22:59 (2+02:54)
reboot system boot 4.18.0-80.el8.x8 Mon Aug 30 20:03 still running
root pts/0 192.168.200.1 Mon Aug 30 19:59 - 20:00 (00:00)
root pts/1 192.168.200.1 Mon Aug 30 16:36 - 19:59 (03:23)
[root@node1 ~]#
3.用户登录失败记录
[root@node1 ~]# lastb
btmp begins Mon Sep 13 20:23:03 2021
[root@node1 ~]#
4.登录日志
[root@node1 ~]# lastlog
Username Port From Latest
root pts/0 192.168.200.1 Fri Sep 17 22:32:06 +0800 2021
bin **Never logged in**
daemon **Never logged in**
adm **Never logged in**
lp **Never logged in**
sync **Never logged in**
shutdown **Never logged in**
halt **Never logged in**
mail **Never logged in**
operator **Never logged in**
games **Never logged in**
ftp **Never logged in**
nobody **Never logged in**
dbus **Never logged in**
systemd-coredump **Never logged in**
systemd-resolve **Never logged in**
tss **Never logged in**
polkitd **Never logged in**
geoclue **Never logged in**
rtkit **Never logged in**
pulse **Never logged in**
三、用户的提权
[root@node1 ~]# vim /etc/sudoers.d/user1
user1 ALL= (ALL) NOPASSWD: /usr/bin/passwd, /usr/bin/mkdir
[user1@node1 ~]$ sudo mkdir file01
[user1@node1 ~]$ ls -l file01
total 0
[user1@node1 ~]$ ls -ld file01
drwxr-xr-x 2 root root 6 Sep 17 22:49 file01
[user1@node1 ~]$ mkdir file02
[user1@node1 ~]$ ls -ld file02
drwxrwxr-x 2 user1 user1 6 Sep 17 22:49 file02
[user1@node1 ~]$
四、ACL权限配置
1.查看文件的ACL权限
访问控制列表,可以实现更细致的访问控制
[root@node1 ~]# getfacl file
# file: file
# owner: root
# group: root
user::rw-
group::r--
other::r--
2.给指定用户设置ACL权限
[root@node1 ~]# setfacl -m u:user1:rx- file
[root@node1 ~]# getfacl file
# file: file
# owner: root
# group: root
user::rw-
user:user1:r-x
group::r--
mask::r-x
other::r--
3.给文件拥有人设置ACL权限
[root@node1 ~]# setfacl -m u::rwx file
[root@node1 ~]# getfacl file
# file: file
# owner: root
# group: root
user::rwx
user:user1:r-x
group::r--
mask::r-x
other::r--
4.给指定用户组设置权限
①新建用户组及添加组内用户
[root@node1 ~]# groupadd HR
[root@node1 ~]# groupmems -g HR -a user1
[root@node1 ~]# groupmems -g HR -l
user1
②设置文件的用户组权限
[root@node1 ~]# setfacl -m g:HR:rwx file
[root@node1 ~]# getfacl file
# file: file
# owner: root
# group: root
user::rwx
user:user1:r-x
group::r--
group:HR:rwx
mask::rwx
other::r--
[root@node1 ~]#
5.给文件拥有组设置权限
[root@node1 ~]# setfacl -m g::rx- file
[root@node1 ~]# getfacl file
# file: file
# owner: root
# group: root
user::rwx
user:user1:r-x
group::r-x
group:HR:rwx
mask::rwx
other::r--
[root@node1 ~]#
6.给文件其他人设置权限
[root@node1 ~]# setfacl -m o::rw- file
[root@node1 ~]# getfacl file
# file: file
# owner: root
# group: root
user::rwx
user:user1:r-x
group::r-x
group:HR:rwx
mask::rwx
other::rw-
7.删除文件高级权限
①删除指定用户权限
[root@node1 ~]# getfacl file
# file: file
# owner: root
# group: root
user::rwx
user:user1:r-x
group::r-x
group:HR:rwx
mask::rwx
other::rw-
[root@node1 ~]# setfacl -x u:user1 file
[root@node1 ~]# getfacl file
# file: file
# owner: root
# group: root
user::rwx
group::r-x
group:HR:rwx
mask::rwx
other::rw-
[root@node1 ~]#
②删除指定组的ACL权限
[root@node1 ~]# getfacl file
# file: file
# owner: root
# group: root
user::rwx
group::r-x
group:HR:rwx
mask::rwx
other::rw-
[root@node1 ~]# setfacl -x g:HR file
[root@node1 ~]# getfacl file
# file: file
# owner: root
# group: root
user::rwx
group::r-x
mask::r-x
other::rw-
[root@node1 ~]#
③递归修改ACL权限
[root@node1 ~]# getfacl ./dir01/
# file: dir01/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
[root@node1 ~]# getfacl ./dir01/test.txt
# file: dir01/test.txt
# owner: root
# group: root
user::rw-
group::r--
other::r--
[root@node1 ~]# setfacl -R -m u:user1:rw- ./dir01/
[root@node1 ~]# getfacl ./dir01/
# file: dir01/
# owner: root
# group: root
user::rwx
user:user1:rw-
group::r-x
mask::rwx
other::r-x
[root@node1 ~]# getfacl ./dir01/test.txt
# file: dir01/test.txt
# owner: root
# group: root
user::rw-
user:user1:rw-
group::r--
mask::rw-
other::r--
[root@node1 ~]#
④清除所有ACL权限
setfacl -b
五、文件与目录的特殊权限
1.进程与文件的属主与属组
前提:进程有属主和属组;文件有属主和属组;
(1) 任何一个可执行程序文件能不能启动为进程:取决于发起者对程序文件是否拥有执行权限;
(2) 启动为进程之后,其进程的属主为发起者;进程的属组为发起者所属的组;
(3) 进程访问文件时的权限,取决于进程的发起者:
(4) 进程的发起者,同文件的属主:则应用文件属主权限;
(5) 进程的发起者,属于文件的属组;则应用文件属组权限;
(6) 应用文件“其它”权限;
2.suid高级权限
ps:/usr/bin/passwd root
1.当一个文件有suid的权限时,那么其他用户来执行该文件时,临时获得文件拥有人的权限
2.suid的权限只能用在二进制的文件上,一般是针对命令
3.只能设置在文件上,设置目录上无意义
chmod u+s file
[root@node1 ~]# getfacl /usr/bin/passwd
getfacl: Removing leading '/' from absolute path names
# file: usr/bin/passwd
# owner: root
# group: root
# flags: s--
user::rwx
group::r-x
other::r-x
3.sgid高级权限
1.当一个文件有sgid的权限时,那么其他用户来执行该文件时,临时获得文件拥有组的权限。
2.当一个目录有sgid的权限时,那么任何人在该目录下创建文件(包括目录)时,该文件的拥有组继承目录的组。
chmod g+s directory
[root@node1 ~]# chmod g+s dir01/
[root@node1 ~]# getfacl dir01/
# file: dir01/
# owner: root
# group: root
# flags: -s-
user::rwx
user:user1:rw-
group::r-x
mask::rwx
other::r-x
[root@node1 ~]# touch dir01/list.txt
[root@node1 ~]# getfacl dir01/list.txt
# file: dir01/list.txt
# owner: root
# group: root
user::rw-
group::r--
other::r--
4.sticky高级权限
1.用在目录上,当一个目录有stciky的权限时,那么其他用户只能删除拥有人是自己的文件。
chmod o+t directory
六、默认权限与反掩码
1.root用户文件的默认权限
默认权限 umask 022
root
文件 644
目录 755
2.普通用户文件的默认权限
普通用户 002
文件 664
目录 775
3.umask配置文件
[root@node1 ~]# cat /etc/bashrc |grep umask
# By default, we want umask to get set. This sets it for non-login shell.
umask 002
umask 022
[root@node1 ~]#
4.修改umask
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
~
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
~
5.umask和mask的权限计算
[root@node1 ~]# mkdir dir02/
[root@node1 ~]# ls -ld dir02/
drwxr-xr-x 2 root root 6 Sep 18 12:30 dir02/
[root@node1 ~]# touch file02
[root@node1 ~]# ll file02
-rw-r--r-- 1 root root 0 Sep 18 12:30 file02
[root@node1 ~]#
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)