root用户除外 root用户不受任何限制shell
文件:数据库
r(Read,读取):对文件而言,具备读取文件内容的权限;vim
w(Write,写入):对文件而言,具备新增、修改文件内容的权限;centos
x(eXecute,执行):对文件而言,具备执行文件的权限;安全
[skyuser1@VM_158_86_centos test]$ll
total 0
-r-xr-xr-x 1 skyuser1 skyuser1 0 May 23 17:25 a.txt
#没有w写入权限
[skyuser1@VM_158_86_centos test]$echo "aaa" > a.txt
-bash: a.txt: Permission denied
#没有r读取权限
[skyuser1@VM_158_86_centos test]$chmod 333 a.txt
[skyuser1@VM_158_86_centos test]$ll a.txt
--wx-wx-wx 1 skyuser1 skyuser1 0 May 23 17:27 a.txt
[skyuser1@VM_158_86_centos test]$cat a.txt
cat: a.txt: Permission denied
复制代码
目录:bash
r(Read,读取):具备浏览目录的权限。测试
w(Write,写入):具备删除、移动、修改目录内文件的权限。ui
x(eXecute,执行):该用户具备进入目录的权限。spa
exec:code
#根目录的目录权限
[skyuser1@VM_158_86_centos /]$ ll -d
dr-xr-xr-x. 20 root root 4096 May 23 11:39 .
#没有写入权限,因此对于普通用户而言不能建立任何文件
[skyuser1@VM_158_86_centos /]$ touch a.txt
touch: cannot touch ‘a.txt’: Permission denied
#不能删除文件
[skyuser1@VM_158_86_centos /]$ ll /a.txt
-rw-r--r-- 1 root root 24 Mar 12 17:34 /a.txt
[skyuser1@VM_158_86_centos /]$ rm -rf a.txt
rm: cannot remove ‘a.txt’: Permission denied
#不能移动文件
[skyuser1@VM_158_86_centos /]$ mv /a.txt /home
mv: cannot move ‘/a.txt’ to ‘/home/a.txt’: Permission denied
复制代码
root用户 无所不能
[root@VM_158_86_centos /]# touch b.txt
[root@VM_158_86_centos /]# ls /b.txt
/b.txt
复制代码
能够经过sh或bash执行.sh文件
可是不能经过./bill.sh执行文件
[skyuser1@VM_158_86_centos test]$ll bill.sh
-rw-rw-r-- 1 skyuser1 skyuser1 23 May 23 17:30 bill.sh
[skyuser1@VM_158_86_centos test]$sh bill.sh
123
[skyuser1@VM_158_86_centos test]$./bill.sh
-bash: ./bill.sh: Permission denied
复制代码
感受这个机制仍是有问题的,只要执行bash xxx.sh文件在根目录下的root .sh文件就能够执行了 那拿x权限来干啥呢?
[wuyuhong@VM_158_86_centos temp_bird]$ su - root
Password:
Last login: Thu May 23 17:54:36 CST 2019 on pts/0
Last failed login: Thu May 23 17:59:49 CST 2019 on pts/0
There was 1 failed login attempt since the last successful login.
welcome to my world.please smile
[root@VM_158_86_centos ~]# cd /
[root@VM_158_86_centos /]# vim test.sh
[root@VM_158_86_centos /]# bash test.sh
我是root建立的可执行文件
[root@VM_158_86_centos /]# su - skyuser1
Last login: Thu May 23 17:52:27 CST 2019 on pts/0
welcome to my world.please smile
[skyuser1@VM_158_86_centos /]$bash test.sh
我是root建立的可执行文件
[skyuser1@VM_158_86_centos /]$ll test.sh
-rw-r--r-- 1 root root 54 May 23 18:00 test.sh
复制代码
创建一个新的文件或目录时,该文件的默认权限会根据umask来设定
umask就是指定 目前用户在创建文件或目录时候的权限默认值
[root@VM_158_86_centos vbird]# umask
0022
[root@VM_158_86_centos vbird]# umask -S
u=rwx,g=rx,o=rx
复制代码
通常文件的创建不该该有执行权限,通常文件一般用于数据的记录,因此不须要x权限
-rw-rw-rw-
复制代码
目录的x权限决定用户是否能进入该目录,因此通常须要x权限
drwxrwxrwx
复制代码
umask的分数指的是该默认值须要减掉的权限
因此上面减下来:
-rw-r--r--
drwxr-xr-x
复制代码
出于安全考虑root的默认umask 022,普通用户的默认umask 002,保留同群组的写入权利
操做:
+ :增长一个特殊参数
- :移除一个特殊参数
= :设定一个特殊参数
复制代码
option:
a:这个文件只能新增数据,不能删除,修改数据,只有root能设定
i:不能删除,更名,设定连结,没法新增编辑数据,只有root能设定
复制代码
command:
lsattr [-adR] file Or Directory
-a:隐藏文件的属性显示出来
-d: 目录自己的隐藏属性
-R:连同子目录也一并列出来
复制代码
chattr:
chattr +aiS testFile
复制代码
rwt rws
[root@VM_158_86_centos ~]# ll -d /tmp
drwxrwxrwt. 7 root root 4096 5月 27 03:31 /tmp
[root@VM_158_86_centos ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 6月 10 2014 /usr/bin/passwd
复制代码
/etc/shadow:保存着帐户信息
[root@VM_158_86_centos ~]# ll /etc/shadow
---------- 1 root root 1133 May 23 17:55 /etc/shadow
复制代码
/etc/shadow这个文件只有root能够读写
但普通用户能经过/usr/bin/passwd命令执行写入或更改密码的操做,也就是能够经过passwd向文件/etc/shadow写入修改数据的缘由就是SUID
-rwsr-xr-x. 1 root root 27832 6月 10 2014 /usr/bin/passwd
复制代码
[wuyuhong@VM_158_86_centos ~]$ passwd
Changing password for user wuyuhong.
Changing password for wuyuhong.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
复制代码
SUID生效过程
wuyuhong对于/usr/bin/passwd具备x权限,表示wuyuhong能执行passwd
passwd的拥有者是root;
wuyuhong执行passwd的过程,会<暂时>得到root的权限;
/etc/shadow就能够被wuyuhong所执行的passwd所修改;prefect
复制代码
然而
[wuyuhong@VM_158_86_centos ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
[wuyuhong@VM_158_86_centos ~]$ ll /usr/bin/cat
-rwxr-xr-x 1 root root 54160 Oct 31 2018 /usr/bin/cat
复制代码
cat指令没有SUID属性,so 不能cat /etc/shadow
SUID只对二进制可执行文件有效
locate指令具备SGID属性
[root@VM_158_86_centos ~]# ll /usr/bin/locate
-rwx--s--x 1 root slocate 40520 Apr 11 2018 /usr/bin/locate
复制代码
locate的本质是去查询/var/lib/mlocate/mlocate.db数据库,从而获取结果
updatedb手动更新数据库,更新新增的文件、配置等
[root@VM_158_86_centos ~]# ll /var/lib/mlocate/mlocate.db
-rw-r----- 1 root slocate 3107582 May 27 16:13 /var/lib/mlocate/mlocate.db
复制代码
这里能够看出只有root能读写mlocate.db,slocate群组能读取mlocate.db
见证奇迹
[root@VM_158_86_centos ~]# ll /usr/bin/locate
-rwx--s--x 1 root slocate 40520 Apr 11 2018 /usr/bin/locate
[root@VM_158_86_centos ~]# ll /var/lib/mlocate/mlocate.db
-rw-r----- 1 root slocate 3107582 May 27 16:13 /var/lib/mlocate/mlocate.db
[root@VM_158_86_centos ~]# su - wuyuhong
Last login: Mon May 27 16:13:58 CST 2019 on pts/0
welcome to my world.please smile
[wuyuhong@VM_158_86_centos ~]$ locate /usr/bin/passwd
/usr/bin/passwd
复制代码
[root@VM_158_86_centos ~]# ll /usr/bin/locate
-rwx--s--x 1 root slocate 40520 Apr 11 2018 /usr/bin/locate
和SUID很类似,locate的other用户执行权限是x,具备执行权限
locate群组拥有者是slocate
执行locate的过程,会<暂时>得到slocate的权限;
so.slocate能够读取mlocate;prefect
复制代码
SGID对二进制程序有用;
程序执行者对于该程序来讲,需具有x的权限;
执行者在执行的过程当中会得到该程序群组的支持
复制代码
SGID不只对二进制可执行文件有效,对目录依然有效
用户若对于此目录具备r与x权限,该用户可以进入此目录;
用户在此目录下的有效群组(effective group)将变成该目录的群组;
用途:若用户在此目录下具备w权限,则使用者所创建的新文件,该新文件的群组与此目录的群组相同
复制代码
模拟测试:
[root@VM_158_86_centos ~]# groupadd project
[root@VM_158_86_centos ~]# useradd -G project pro_user1
[root@VM_158_86_centos ~]# useradd -G project pro_user2
[root@VM_158_86_centos ~]# id pro_user1
uid=1006(pro_user1) gid=1011(pro_user1) groups=1011(pro_user1),1010(project)
[root@VM_158_86_centos ~]# id pro_user2
uid=1007(pro_user2) gid=1012(pro_user2) groups=1012(pro_user2),1010(project)
[root@VM_158_86_centos ~]# mkdir /srv/prohome
[root@VM_158_86_centos ~]# ll -d /srv/prohome/
drwxr-xr-x 2 root root 4096 May 27 16:51 /srv/prohome/
[root@VM_158_86_centos ~]# chgrp project /srv/prohome/
[root@VM_158_86_centos ~]# chmod 770 /srv/prohome/
[root@VM_158_86_centos ~]# ll -d /srv/prohome/
drwxrwx--- 2 root project 4096 May 27 16:51 /srv/prohome/
[root@VM_158_86_centos ~]# su - pro_user1
welcome to my world.please smile
[pro_user1@VM_158_86_centos ~]$ cd /srv/prohome/
[pro_user1@VM_158_86_centos prohome]$ touch abc.txt
[pro_user1@VM_158_86_centos prohome]$ exit
logout
[root@VM_158_86_centos ~]# su - pro_user2
welcome to my world.please smile
[pro_user2@VM_158_86_centos ~]$ cd /srv/prohome/
[pro_user2@VM_158_86_centos prohome]$ echo "hello" > abc.txt
-bash: abc.txt: Permission denied
[pro_user2@VM_158_86_centos prohome]$ ll abc.txt
-rw-rw-r-- 1 pro_user1 pro_user1 0 May 27 17:14 abc.txt
复制代码
abc.txt文件属于pro_user1的用户,同时也属于pro_user1
所以 pro_user2没有写入权限 只有r只读权限
这时就须要赋予/srv/prohome/的SGID权限 让pro_user1和pro_user2建立的文件为project群组文件
[pro_user1@VM_158_86_centos ~]$ cd /srv/prohome/
[pro_user1@VM_158_86_centos prohome]$ touch hello
[pro_user1@VM_158_86_centos prohome]$ ll hello
-rw-rw-r-- 1 pro_user1 project 0 May 27 22:37 hello
su: Authentication failure
[pro_user1@VM_158_86_centos prohome]$ su - root
Password:
Last login: Mon May 27 22:28:36 CST 2019 from 183.220.26.92 on pts/0
welcome to my world.please smile
[root@VM_158_86_centos ~]# su - pro_user2
Last login: Mon May 27 17:14:36 CST 2019 on pts/0
Last failed login: Mon May 27 22:38:49 CST 2019 on pts/0
There was 1 failed login attempt since the last successful login.
welcome to my world.please smile
[pro_user2@VM_158_86_centos ~]$ cd /srv/prohome/
[pro_user2@VM_158_86_centos prohome]$ echo "123" > hello
[pro_user2@VM_158_86_centos prohome]$ cat hello
123
复制代码
添加SGID后建立的文件就为同组的主组project 同组的成员就能够访问和修改文件了
只对目录有效
当用户对于此目录具备w,x权限,即具备写入权限时;
当用户在该目录下创建文件或目录时,仅有本身和root才有权利操做该文件
复制代码
意思就是只有本身能操做该目录下本身建立的文件,不能操做其余人建立的文件,其余人(同组,或other具备权限)有执行权限,也不能操做个人文件,我有其余人(同组,或other具备权限)的执行权限,也不能操做其余人的文件
4为SUID
2为GID
1为SBIT
chmod 4755 XXX;
chmod 7755 XXX;
chmod u=rwxs,go=x XXX;
chmod u+s g+s o+t XXX
chmod 7666 test;ls -l test;
-rwSrwSrwT
666表示没有执行权限 因此这里设置7,因此为SST,SST表示无效状态
复制代码
传统的Linux权限只能针对一个用户(建立者),一个群组以及非此群组的其余人设定权限
假如我须要对该文件设定3个不一样用户,两个不一样群组的不一样权限此时应该怎么办?
传统的权限控制是没法作到的,只有用ACL这个更细致的权限控制能够解决,
而传统的权限控制控制了一个大的范围,ACL再细分小的范围,真是太完美啦
复制代码
那 ACL 主要能够针对哪些方面来控制权限呢?他主要能够针对几个项目:
使用者 (user):能够针对使用者来配置权限;
群组 (group):针对群组为对象来配置其权限;
默认属性 (mask):还能够针对在该目录下在建立新文件/目录时,规范新数据的默认权限;
复制代码