linux基础权限管理篇二

一、特殊权限:suid sgid

1.suid(set uid 设置用户ID)限定:只能设置在二进制可执行程序上面。对目录无效

功能:程序运行时的权限从执行者变成程序所有者的权限

sgid:既可以给二进制可执行程序设置也可以对目录设置

功能:在设置了sgid权限的目录下建立文件时,新创建的文件的所有组会继承上级目录的所属组

设置suid

chmod u+s  二进制可执行文件或chmod u=4 二进制可执行文件

设置sgid

chmod g+s 二进制可执行文件或chmod g=2 二进制可执行文件

suid属性一般用在可执行的文件上,当用户执行该文件时,会临时拥有该执行文件的所有者权限。

使用“ls -l”或者”ll“命令浏览文件时,如果可执行文件所有者权限的第三位是一个小写的“s”,就表明该执行文件拥有suid属性

[root@andy ~]# which cat
/bin/cat
[root@andy ~]# chmod u+s /bin/cat
[root@andy ~]# ls -l /bin/cat
-rwsr-xr-x. 1 root root 54080 11月 6 2016 /bin/cat
[root@andy ~]# su - test
上一次登录:三 11月 27 22:00:51 CST 2019pts/0 上
[test@andy ~]$ cat /etc/shadow
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::
games:*:17110:0:99999:7:::
ftp:*:17110:0:99999:7:::
nobody:*:17110:0:99999:7:::
systemd-bus-proxy:!!:18079::::::
systemd-network:!!:18079::::::
dbus:!!:18079::::::
polkitd:!!:18079::::::
tss:!!:18079::::::
postfix:!!:18079::::::
sshd:!!:18079::::::
chrony:!!:18079::::::
mysql:!!:18117::::::
apache:!!:18118::::::
zabbix:!!:18118::::::
dhcpd:!!:18123::::::
geoclue:!!:18123::::::
andy:!!:18218::::::
andy1:$6$Xs6FGgpm$gOXgBQLVRVGGc6DbbWPxqU/ZK775VbGxkZD0ip3ziYHEtYCRaYXh1hjDKsn7dP4JpIRZwHxwBAHYPyMbRdIJQ.:18219::::::
root:$6$UL3tJO.I$L7IO.DLLH/SQj7dZ.c5ljFJKzQaRjUf9tPBDZnFBL46EhDhFANXsh7h93v6g0f4/pZknX9S7paLLCptQwp.d20:18219::::::
test:$6$DRHD2FZ5$ky30s5SlL8dup7.vn7Uhj7/4Q5PAOyKltZPp488OAWn2OoEQz.0OYg.4mJ/ajWqJNpJnNMMhnabpYG2aeG9M1.:18227:0:99999:7:::
[test@andy ~]$

去除suid权限

chmod u-s 可执行二进制文件 

[root@andy ~]# ls -l /bin/cat
-rwsr-xr-x. 1 root root 54080 11月 6 2016 /bin/cat
[root@andy ~]# chmod u-s /bin/cat
[root@andy ~]# ls -l /bin/cat
-rwxr-xr-x. 1 root root 54080 11月 6 2016 /bin/cat
[root@andy ~]#

sgid既可以给二进制可执行文件设置,也可以给目录设置

功能:在设置了sgid权限的目录下建立文件时,新创建的文件的所属组会继承上级目录的权限

chmod g+s 可执行二进制文件  当其他用户执行二进制命令时,可以拥有执行执行该命令用户,所属组的权限

[root@andy ~]# ls -l /bin/cat
-rwxr-xr-x. 1 root root 54080 11月 6 2016 /bin/cat
[root@andy ~]# chmod g+s /bin/cat
[root@andy ~]# ls -l /bin/cat
-rwxr-sr-x. 1 root root 54080 11月 6 2016 /bin/cat
[root@andy ~]# ls -l /etc/passwd
-rw-r----- 1 root root 1463 11月 28 02:29 /etc/passwd
[root@andy ~]# id test
uid=1000(test) gid=1002(test) 组=1002(test)
[root@andy ~]# su - test
上一次登录:四 11月 28 02:59:39 CST 2019pts/0 上
/usr/bin/id: cannot find name for user ID 1000
/usr/bin/id: cannot find name for user ID 1000
[I have no name!@andy ~]$ 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:99:99:Nobody:/:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
zabbix:x:996:994:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
geoclue:x:995:993:User for geoclue:/var/lib/geoclue:/sbin/nologin
andy:x:994:991::/home/andy:/sbin/nologin
andy1:x:993:990::/home/andy1:/sbin/nologin
test:x:1000:1002::/home/test:/bin/bash
andy2:x:992:989::/home/andy2:/bin/bash
[I have no name!@andy ~]$ su - root
密码:
上一次登录:四 11月 28 03:01:35 CST 2019pts/0 上
[root@andy ~]# chmod g-s /bin/cat
[root@andy ~]# ls -l /bin/cat
-rwxr-xr-x. 1 root root 54080 11月 6 2016 /bin/cat
[root@andy ~]# su - test
上一次登录:四 11月 28 03:05:27 CST 2019pts/0 上
/usr/bin/id: cannot find name for user ID 1000
/usr/bin/id: cannot find name for user ID 1000
[I have no name!@andy ~]$ cat /etc/passwd
cat: /etc/passwd: 权限不够
[I have no name!@andy ~]$

chmod g+s 目录  继承所属组的权限

[root@andy ~]# ls -ld
dr-xr-x---. 4 root root 225 11月 28 03:13 .
[root@andy ~]# ls -ld test
drwxr-xr-x 3 root root 19 11月 28 03:16 test
[root@andy ~]# chown test:G1 test
[root@andy ~]# ls -ld test
drwxr-xr-x 3 test G1 19 11月 28 03:16 test
[root@andy ~]# cd test
[root@andy test]# mkdir test1
[root@andy test]# ls -ld test1
drwxr-xr-x 2 root root 6 11月 28 03:21 test1
[root@andy test]# cd
[root@andy ~]# chmod g+s test
[root@andy ~]# ls -ld test
drwxr-sr-x 4 test G1 32 11月 28 03:21 test
[root@andy ~]# cd test
[root@andy test]# mkdir test3
[root@andy test]# ls -ld test3
drwxr-sr-x 2 root G1 6 11月 28 03:24 test3
[root@andy test]#

二、 sbit权限

对于设置sbit权限的文件,用户只能删除自己创建的文件,不能删除其他用户创建的文件

对目录/tmp添加sbit权限,删除文件的时候显示拒绝

 root用户设置sbit

[root@andy ~]# chmod o+t /tmp

 

切换到andy1用户创建andy1文件

 

[root@andy ~]# su - andy1

 [andy1@andy ~]$ cd /tmp

[andy1@andy tmp]$ touch andy1

切换到andy2用户

[andy1@andy tmp]$ su - andy2

[andy2@andy ~]$ cd /tmp

[andy2@andy tmp]$ rm -rf andy1
rm: 无法删除"andy1": 不允许的操作

切换到root取消suit权限再次切换到andy2就可以删除了

[andy2@andy tmp]$ su - root
密码:
上一次登录:三 12月 4 13:43:03 CST 2019pts/1 上
[root@andy ~]# chmod o-t /tmp
[root@andy ~]# su - andy2
上一次登录:三 12月 4 15:18:15 CST 2019pts/1 上
[andy2@andy ~]$ rm -rf /tmp/andy1
[andy2@andy ~]$

三、文件扩展权限ACL

扩展ACL:access control list

场景:设置用户andy3对文件a.txt拥有rwx权限,andy不属于a.txt的所属主和组,andy是other,同时还要保证除了andy这个用户,other中其他的用户,没有rwx权限。

1.查看andy3属主属组,创建并查看a.txt属主属组

[root@andy ~]# id andy3
uid=1003(andy3) gid=1003(andy3) 组=1003(andy3)
[root@andy ~]# rm -rf a.txt
[root@andy ~]# id andy3
uid=1003(andy3) gid=1003(andy3) 组=1003(andy3)
[root@andy ~]# touch a.txt
[root@andy ~]# ls -lh a.txt
-rw-r--r-- 1 root root 0 12月 4 15:45 a.txt
[root@andy ~]#

2.设置a.txt扩展权限,查看a.txt扩展权限

[root@andy ~]# setfacl -m u:andy3:rwx a.txt
[root@andy ~]# getfacl a.txt
# file: a.txt
# owner: root
# group: root
user::rw-
user:andy3:rwx
group::r--
mask::rwx
other::r--

[root@andy ~]#

3.修改权限-R一定要在-m(设置)前边表示目录下所有文件

[root@andy ~]# setfacl -R -m u:andy3:rw- /root/
[root@andy ~]# getfacl a.txt
# file: a.txt
# owner: root
# group: root
user::rw-
user:andy3:rw-

4.去掉单个权限

[root@andy ~]# setfacl -x u:andy3 a.txt
[root@andy ~]# getfacl a.txt
# file: a.txt
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--

[root@andy ~]#

5.去掉所有acl权限(假如a.txt对多个用户开放acl权限)

[root@andy ~]# setfacl -b /root/a.txt

四、sudo实战

1.sudo概念

sudo是linux下常用的允许普通用户使用超级用户权限的工具,允许系统管理员让普通用户执行一些或者全部的root命令。减少了root登陆管理时间,同样提高了安全性

2.特点

1)sudo能限制用户只在某台主机上运行某些命令

2)sudo提供了丰富的日志,详细地记录了每个用户干了什么。它能够将日志传到中心主机或者日志服务器上

3)sudo使用时间戳文件--日志 来执行类似检票系统,当用户调用sudo并且输入他的密码时,用户获得一张存活期为5min的票

4)sudo的配置文件是/etc/sudoers,属性必须为440,它允许系统管理员集中的管理用户的使用权限和使用的主机,一般使用visudo编辑配置文件

3.实战sudo

1)创建用户andy并创建密码

[root@andy ~]# useradd andy
[root@andy ~]# echo "andy" | passwd --stdin andy
更改用户 andy 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@andy ~]#

2)修改配置文件为andy添加useradd权限

[root@andy ~]# visudo

##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
andy ALL=(ALL) /usr/sbin/useradd
## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

## Allows people in group wheel to run all commands

3)验证andy的useradd权限

[root@andy ~]# su - andy
[andy@andy ~]$ useradd test1
-bash: /usr/sbin/useradd: 权限不够
[andy@andy ~]$ sudo /usr/sbin/useradd test1

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

[sudo] password for andy: (输入andy密码)
[andy@andy ~]$ 
[andy@andy ~]$ sudo /usr/sbin/useradd test2
[andy@andy ~]$

4)配置日志

1.创建日志文件

[root@andy ~]# touch /var/log/sudo.log
[root@andy ~]#

2.配置rsyslog

[root@andy ~]# vim /etc/rsyslog.conf

#sudo log
local2.debug /var/log/sudo.log
"/etc/rsyslog.conf" 92L, 3272C 已写入
[root@andy ~]#

3.配置visudo

[root@andy ~]# visudo

Defaults logfile=/var/log/sudo.log
"/etc/sudoers.tmp" 112L, 3983C written
[root@andy ~]#

4.测试,查看日志

[root@andy ~]# cat /var/log/sudo.log
Dec 12 16:42:47 : andy : TTY=pts/0 ; PWD=/home/andy ; USER=root ;
COMMAND=/usr/sbin/useradd test5
[root@andy ~]#

  

猜你喜欢

转载自www.cnblogs.com/yzandy/p/11946683.html