一、文章大綱
Linux基礎(chǔ)權(quán)限
ACL訪問控制
特殊權(quán)限SUID、SGID、STICKY和特殊屬性
二、Linux基礎(chǔ)權(quán)限
在Linux下,我們可以使用ll或ls -l命令方便查看一個(gè)文件的權(quán)限設(shè)置信息,如下圖,我們將查看信息分為三個(gè)部分。

第一部分:-rw-------.
表示所有者、所屬組和其他人對(duì)文件的訪問權(quán)限,也可以分為3個(gè)部分:

第1部分:表示文件類型,有以下類型
”-” 表示普通文件
”d” 表示目錄
"l” 表示鏈接文件
"p" 表示管理文件
"b" 表示塊設(shè)備文件
"c" 表示字符設(shè)備文件
"s" 表示套接字文件
第2部分:總共有9位,前面3位表示所有者對(duì)文件的權(quán)限,中間3位表示所屬組對(duì)文件的權(quán)限,最后3位表示其他用戶對(duì)文件的權(quán)限,使用rwx表示。

除使用rwx表示外,另外一個(gè)直觀的表示方式是使用數(shù)字,如:
查看文件a的權(quán)限為:rwxrwxrwx,對(duì)應(yīng)數(shù)字表示為777,表明用戶自己、所屬組、其他人對(duì)該文件具有讀取、寫入(修改)和執(zhí)行權(quán)限。
查看目錄b的權(quán)限為:r-xr--r--,對(duì)應(yīng)數(shù)字表示為644,表明用戶自己對(duì)該目錄具有讀取權(quán)限,所屬組、其他人雖然對(duì)目錄有讀取權(quán)限但因?yàn)闆]有執(zhí)行權(quán)限無法進(jìn)入目錄實(shí)際上無權(quán)限。
#切換到tmp目錄,創(chuàng)建test.file文件
[root@centos7 ~]# cd /tmp/
[root@centos7 tmp]# echo abcd > test.file
#查看文件權(quán)限,為644
[root@centos7 tmp]# ll test.file
-rw-r--r--. 1 root root 5 May 30 01:40 test.file
#新增xiaozeng用戶
[root@centos7 tmp]# useradd xiaozeng
#切換用戶,因?yàn)槲募?duì)其他用戶有讀取權(quán)限,所以新用戶可以讀取但是沒辦法修改
[root@centos7 tmp]# su xiaozeng
[xiaozeng@centos7 tmp]$ cat test.file
abcd
#因?yàn)闆]有寫入權(quán)限,無法對(duì)文件進(jìn)行
[xiaozeng@centos7 tmp]$ echo 1234 >> test.file
bash: test.file: Permission denied
#xiaozeng相對(duì)于root屬于其他用戶,取消其他用戶對(duì)test.file文件的讀取權(quán)限
[root@centos7 tmp]# chmod o-r test.file
[root@centos7 tmp]# ll test.file
-rw-r-----. 1 root root 5 May 30 01:40 test.file
[xiaozeng@centos7 tmp]$ cat test.file
cat: test.file: Permission denied
#也可以使用數(shù)字形式修改,比如給其他用戶增加讀取和寫入權(quán)限
[root@centos7 tmp]# chmod 646 test.file
[root@centos7 tmp]# ll test.file
-rw-r--rw-. 1 root root 5 May 30 01:40 test.file
[xiaozeng@centos7 tmp]$ cat test.file
abcd
[xiaozeng@centos7 tmp]$ echo 1234 >> test.file
[xiaozeng@centos7 tmp]$ cat test.file
abcd
1234
第3部分:
表示facl權(quán)限,后面會(huì)詳細(xì)介紹。
第二部分: 1
表示硬鏈接引用次數(shù)。
第三部分: root root
表示文件的所屬用戶和所屬組。
#查看test.file文件權(quán)限
[root@centos7 tmp]# ll test.file
-rw-r--r--. 1 root root 5 May 30 01:51 test.file
#去掉other用戶讀取權(quán)限,因?yàn)檫@里用戶和用戶組為root,xiaozeng用戶相對(duì)文件來說是其他用戶
[root@centos7 tmp]# chmod o-r test.file
[root@centos7 tmp]# ll test.file
-rw-r-----. 1 root root 5 May 30 01:51 test.file
#切換xiaozeng用戶,查看文件,提示沒有權(quán)限
[root@centos7 tmp]# su xiaozeng
[xiaozeng@centos7 tmp]$ cat test.file
cat: test.file: Permission denied
#將文件的屬組修改為xiaozeng
[root@centos7 tmp]# chown root:xiaozeng test.file
[root@centos7 tmp]# ll test.file
-rw-r-----. 1 root xiaozeng 5 May 30 01:51 test.file
#切換xiaozeng用戶,可以查看文件,因?yàn)閷?duì)就屬組的權(quán)限是可讀
[root@centos7 tmp]# su xiaozeng
[xiaozeng@centos7 tmp]$ cat test.file
abcd
三、ACL訪問控制
上述基礎(chǔ)權(quán)限控制還是太粗,我們?cè)囅脒@樣一個(gè)場(chǎng)景:文件test.file屬于用戶root,屬組為xiaozeng,對(duì)應(yīng)權(quán)限為640,現(xiàn)在有新用戶zhangsan也需要查看test.file文件,但是zhangsan用戶相對(duì)文件屬于other組,沒有查看權(quán)限,在不改變屬組的關(guān)系情況下該怎么辦?這就要用到ACL訪問控制了。
#添加用戶zhangsan
[root@centos7 tmp]# useradd zhangsan
#切換用戶zhangsan
[root@centos7 tmp]# su zhangsan
#沒有權(quán)限查看文件
[zhangsan@centos7 tmp]$ cat test.file
cat: test.file: Permission denied
[zhangsan@centos7 tmp]$ ls -al test.file
-rw-r-----. 1 root xiaozeng 5 May 30 01:51 test.file
#查看文件acl權(quán)限
[root@centos7 tmp]# getfacl test.file
# file: test.file
# owner: root
# group: xiaozeng
user::rw-
group::r--
other::---
#設(shè)置acl權(quán)限,用戶zhangsan用戶對(duì)文件test.file讀取權(quán)限。
[root@centos7 tmp]# setfacl -m u:zhangsan:r-- test.file
[root@centos7 tmp]# getfacl test.file
# file: test.file
# owner: root
# group: xiaozeng
user::rw-
user:zhangsan:r--
group::r--
mask::r--
other::---
#切換用戶,可查看文件
[root@centos7 tmp]#
[root@centos7 tmp]# su zhangsan
[zhangsan@centos7 tmp]$ cat test.file
abcd
#修改內(nèi)容,失敗
[zhangsan@centos7 tmp]$ echo zhangsan >> test.file
bash: test.file: Permission denied
#設(shè)置zhangsan用戶對(duì)文件讀寫權(quán)限
[root@centos7 tmp]# setfacl -m u:zhangsan:rw- test.file
[root@centos7 tmp]# getfacl test.file
# file: test.file
# owner: root
# group: xiaozeng
user::rw-
user:zhangsan:rw-
group::r--
mask::rw-
other::---
#切換用戶,用戶可以寫入文件
[root@centos7 tmp]# su zhangsan
[zhangsan@centos7 tmp]$ echo zhangsan >> test.file
[zhangsan@centos7 tmp]$ cat test.file
abcd
zhangsan
#查看文件,多一個(gè)+號(hào),表示文件設(shè)置acl權(quán)限
[root@centos7 tmp]# ll test.file
-rw-r-x---+ 1 root xiaozeng 14 May 30 04:04 test.file
#清除acl權(quán)限
[root@centos7 tmp]# setfacl -b test.file
[root@centos7 tmp]# getfacl test.file
# file: test.file
# owner: root
# group: xiaozeng
user::rw-
group::r--
other::---
#新建文件夾zs
[root@centos7 tmp]# mkdir zs
[root@centos7 tmp]# ls -ld zs/
drwxr-xr-x. 2 root root 6 May 30 04:13 zs/
#設(shè)置目錄權(quán)限,去掉其他用戶讀寫權(quán)限
[root@centos7 tmp]# chmod o-rx zs -R
[root@centos7 tmp]# ls -ld zs/
drwxr-x---. 2 root root 6 May 30 04:13 zs/
[root@centos7 tmp]#
#切換zhangsan用戶,測(cè)試對(duì)目錄訪問權(quán)限
[root@centos7 tmp]# su zhangsan
[zhangsan@centos7 tmp]$ cd zs/
bash: cd: zs/: Permission denied
#設(shè)置zhangsan用戶組對(duì)目錄讀寫訪問權(quán)限
[root@centos7 tmp]# setfacl -m g:zhangsan:rwx zs
[root@centos7 tmp]# getfacl zs
# file: zs
# owner: root
# group: root
user::rwx
group::r-x
group:zhangsan:rwx
mask::rwx
other::---
#切換zhangsan用戶
[root@centos7 tmp]# su zhangsan
[zhangsan@centos7 tmp]$ cd zs/
#可以正常創(chuàng)建文件
[zhangsan@centos7 zs]$ touch abc
#查看創(chuàng)建文件權(quán)限,發(fā)現(xiàn)創(chuàng)建文件并沒有fac權(quán)限
[zhangsan@centos7 zs]$ ls abc -l
-rw-rw-r--. 1 zhangsan zhangsan 0 May 30 04:17 abc
#可以加上d參數(shù),這樣該目錄下文件會(huì)繼承目錄acl權(quán)限
[root@centos7 tmp]# setfacl -m d:g:zhangsan:rwx zs
[root@centos7 tmp]# getfacl zs
# file: zs
# owner: root
# group: root
user::rwx
group::r-x
group:zhangsan:rwx
mask::rwx
other::---
default:user::rwx
default:group::r-x
default:group:zhangsan:rwx
default:mask::rwx
default:other::---
#切換用戶,創(chuàng)建文件
[root@centos7 tmp]# su zhangsan
[zhangsan@centos7 tmp]$ cd zs
[zhangsan@centos7 zs]$ touch bcd
[zhangsan@centos7 zs]$ ls -alth
total 0
drwxrwx---+ 2 root root 28 May 30 04:20 .
-rw-rw----+ 1 zhangsan zhangsan 0 May 30 04:20 bcd
-rw-rw-r--. 1 zhangsan zhangsan 0 May 30 04:17 abc
drwxrwxrwt. 9 root root 171 May 30 04:13 ..
#使用k參數(shù)取消默認(rèn)權(quán)限設(shè)置
[root@centos7 tmp]# setfacl -k zs
[root@centos7 tmp]# getfacl zs
# file: zs
# owner: root
# group: root
user::rwx
group::r-x
group:zhangsan:rwx
mask::rwx
other::---
#使用b參數(shù),取消acl權(quán)限設(shè)置
[root@centos7 tmp]# setfacl -b zs
[root@centos7 tmp]# getfacl zs
# file: zs
# owner: root
# group: root
user::rwx
group::r-x
other::---
四、特殊權(quán)限SUID、SGID、STICKY和特殊屬性
SUID
作用:讓普通用戶臨時(shí)擁有該文件的屬主的執(zhí)行權(quán)限,suid權(quán)限只能應(yīng)用在二進(jìn)制可執(zhí)行文件(命令)上,而且suid權(quán)限只能設(shè)置在屬主位置上。
增加權(quán)限 u+s,移除權(quán)限u-s,對(duì)應(yīng)數(shù)字為4。
#拷貝一份cat命令,命名為suid_cat
[root@centos7 tmp]# cp /usr/bin/cat suid_cat
[root@centos7 tmp]# ll suid_cat
-rwxr-xr-x. 1 root root 54160 May 30 04:50 suid_cat
#切換xiaozeng用戶
[root@centos7 tmp]# su xiaozeng
#查看shadow文件權(quán)限,權(quán)限為000也就意味著只有root可以查看(root是超級(jí)管理員)
[xiaozeng@centos7 tmp]$ ll /etc/shadow
----------. 1 root root 771 May 30 03:37 /etc/shadow
#使用拷貝的cat命令查看shadow文件,提示無權(quán)限,因?yàn)槲募荒躵oot查看
[xiaozeng@centos7 tmp]$ ./suid_cat /etc/shadow
./suid_cat: /etc/shadow: Permission denied
#切換root,給文件設(shè)置suid權(quán)限
[root@centos7 tmp]# chmod u+s suid_cat
#查看文件,發(fā)現(xiàn)文件屬主由rxw變?yōu)閞ws,說明文件有suid權(quán)限
[root@centos7 tmp]# ll suid_cat
-rwsr-xr-x. 1 root root 54160 May 30 04:50 suid_cat
#切換xiaozeng用戶
[root@centos7 tmp]# su xiaozeng
#可以成功查看shadow文件內(nèi)容。因?yàn)榫哂衧uid權(quán)限的文件會(huì)臨時(shí)擁有屬主的權(quán)限,也就是root權(quán)限
[xiaozeng@centos7 tmp]$ ./suid_cat /etc/shadow
root:$6$BRi2HZ9h$nae7m9Ai0IYzQL5UT8H0REwHHEkzYemOFY6o85zdDblHaPUd2Bz2yFhT8bKvXpl1d41U1pdGFp.S.eLGWqOvF.:19503:0:99999:7:::
bin:*:17834:0:99999:7:::
daemon:*:17834:0:99999:7:::
adm:*:17834:0:99999:7:::
lp:*:17834:0:99999:7:::
sync:*:17834:0:99999:7:::
shutdown:*:17834:0:99999:7:::
halt:*:17834:0:99999:7:::
mail:*:17834:0:99999:7:::
operator:*:17834:0:99999:7:::
games:*:17834:0:99999:7:::
ftp:*:17834:0:99999:7:::
nobody:*:17834:0:99999:7:::
systemd-network:!!:19499::::::
dbus:!!:19499::::::
polkitd:!!:19499::::::
sshd:!!:19499::::::
postfix:!!:19499::::::
test:$6$2UQjp2Fc$KvajSnGNhDRhA9//QpgqiUtMD0LvT6zoP8ohhveaVYrVlyM3dERo2NHlkYBOH33yZRSHUnb1N4EPcuB/F4qGr1:19499:0:99999:7:::
abc:!!:19499:0:99999:7:::
xiaozeng:!!:19506:0:99999:7:::
zhangsan:!!:19506:0:99999:7:::
#查看文件權(quán)限
[root@centos7 tmp]# ll suid_cat
-rwsr-xr-x. 1 root root 54160 May 30 04:50 suid_cat
#可以使用八進(jìn)制0,表示不設(shè)置,也就是取消suid權(quán)限
[root@centos7 tmp]# chmod 0744 suid_cat
[root@centos7 tmp]# ll suid_cat
-rwxr--r--. 1 root root 54160 May 30 04:50 suid_cat
#使用八進(jìn)制4,表示設(shè)置文件suid權(quán)限
[root@centos7 tmp]# chmod 4744 suid_cat
[root@centos7 tmp]# ll suid_cat
-rwsr--r--. 1 root root 54160 May 30 04:50 suid_cat
SGID
作用:sgid權(quán)限一般應(yīng)用在目錄上,當(dāng)一個(gè)目錄擁有sgid權(quán)限時(shí),任何用戶在該目錄下創(chuàng)建的文件的屬組都會(huì)****繼承****該目錄的屬組。
sgid權(quán)限也使用s表示,增加權(quán)限g+s,移除權(quán)限g-s,對(duì)應(yīng)數(shù)字為2。
#新建目錄
[root@centos7 tmp]# mkdir sgid_dir
[root@centos7 tmp]# ls -ld sgid_dir/
drwxr-xr-x. 2 root root 6 May 30 04:59 sgid_dir/
#給目錄設(shè)置757權(quán)限,即其他用戶可訪問可以讀寫
[root@centos7 tmp]# chmod 757 sgid_dir/
[root@centos7 tmp]# ls -ld sgid_dir/
drwxr-xrwx. 2 root root 6 May 30 04:59 sgid_dir/
#切換xiaozeng用戶,并創(chuàng)建文件
[root@centos7 tmp]# su xiaozeng
[xiaozeng@centos7 tmp]$ cd sgid_dir/
[xiaozeng@centos7 sgid_dir]$ touch a
#查看文件,文件屬于xiaozeng用戶和xiaozeng組
[xiaozeng@centos7 sgid_dir]$ ll
total 0
-rw-rw-r--. 1 xiaozeng xiaozeng 0 May 30 05:00 a
#給目錄設(shè)置sgid權(quán)限
[root@centos7 tmp]# chmod g+s sgid_dir/
#查看目錄,屬組權(quán)限由r-x變?yōu)閞-s
[root@centos7 tmp]# ls -ld sgid_dir/
drwxr-srwx. 2 root root 24 May 30 05:01 sgid_dir/
#切換用戶xiaoeng,并創(chuàng)建文件b
[root@centos7 tmp]# su xiaozeng
[xiaozeng@centos7 tmp]$ cd sgid_dir/
[xiaozeng@centos7 sgid_dir]$ touch b
#查看文件b屬組,由于目錄屬組為root,所以文件自動(dòng)繼承其屬組
[xiaozeng@centos7 sgid_dir]$ ll
total 0
-rw-rw-r--. 1 xiaozeng xiaozeng 0 May 30 05:00 a
-rw-rw-r--. 1 xiaozeng root 0 May 30 05:01 b
#
[root@centos7 tmp]# ls -ld sgid_dir/
drwxr-srwx. 2 root root 24 May 30 05:01 sgid_dir/
[root@centos7 tmp]# chmod 0757 sgid_dir/
[root@centos7 tmp]# ls -ld sgid_dir/
drwxr-srwx. 2 root root 24 May 30 05:01 sgid_dir/
STICKY
作用:sticky權(quán)限一般針對(duì)目錄來設(shè)置,作用是只允該目錄下的文件的創(chuàng)建者刪除自己的創(chuàng)建的文件,不允許其他人刪除文件。(root用戶除外,因?yàn)閞oot用戶是超級(jí)管理員),而且sticky權(quán)限只能設(shè)置在other位置上。
增加權(quán)限o+t,移除權(quán)限o-t,對(duì)應(yīng)數(shù)字為1。
#新建目錄sticky_dir
[xiaozeng@centos7 tmp]$ mkdir sticky_dir
#賦予目錄777權(quán)限,即任何人都可以讀寫
[xiaozeng@centos7 tmp]$ chmod 777 sticky_dir/
[xiaozeng@centos7 tmp]$ ls -althd sticky_dir/
drwxrwxrwx. 2 xiaozeng xiaozeng 6 May 30 05:49 sticky_dir/
#切換用戶xiaozeng,并創(chuàng)建多個(gè)文件
[xiaozeng@centos7 tmp]$ cd sticky_dir/
[xiaozeng@centos7 sticky_dir]$ touch aaa
[xiaozeng@centos7 sticky_dir]$ touch bbb
#賦予文件aaa和bbb777權(quán)限
[xiaozeng@centos7 sticky_dir]$ chmod 777 aaa bbb
[xiaozeng@centos7 sticky_dir]$ ll
total 0
-rwxrwxrwx. 1 xiaozeng xiaozeng 0 May 30 05:50 aaa
-rwxrwxrwx. 1 xiaozeng xiaozeng 0 May 30 05:50 bbb
#切換zhangsan用戶
[root@centos7 tmp]# su zhangsan
[zhangsan@centos7 tmp]$ cd sticky_dir/
#因?yàn)槲募?77權(quán)限,所以用戶zhangsan可以對(duì)文件讀寫
[zhangsan@centos7 sticky_dir]$ echo 1234 >> aaa
[zhangsan@centos7 sticky_dir]$ echo 567 > aaa
[zhangsan@centos7 sticky_dir]$ cat aaa
567
#也可以刪除文件
[zhangsan@centos7 sticky_dir]$ rm aaa
[zhangsan@centos7 sticky_dir]$ exit
exit
#切回xiaozeng用戶
[root@centos7 tmp]# su xiaozeng
#對(duì)目錄設(shè)置STICKY權(quán)限
[xiaozeng@centos7 tmp]$ chmod o+t sticky_dir/
#查看目錄權(quán)限,其他用戶權(quán)限由rwx變?yōu)閞wt
[xiaozeng@centos7 tmp]$ ls -ld sticky_dir/
drwxrwxrwt. 2 xiaozeng xiaozeng 17 May 30 05:51 sticky_dir/
#切換回sticky_dir目錄
[xiaozeng@centos7 tmp]$ cd sticky_dir/
[xiaozeng@centos7 sticky_dir]$ ll
total 0
-rwxrwxrwx. 1 xiaozeng xiaozeng 0 May 30 05:50 bbb
#創(chuàng)建文件ccc,并設(shè)置777權(quán)限
[xiaozeng@centos7 sticky_dir]$ touch ccc
[xiaozeng@centos7 sticky_dir]$ chmod 777 ccc
[xiaozeng@centos7 sticky_dir]$ ll
total 0
-rwxrwxrwx. 1 xiaozeng xiaozeng 0 May 30 05:50 bbb
-rwxrwxrwx. 1 xiaozeng xiaozeng 0 May 30 05:53 ccc
#切換回zhangsan用戶
[root@centos7 tmp]# su zhangsan
[zhangsan@centos7 tmp]$ cd sticky_dir/
#可以對(duì)文件正常寫入
[zhangsan@centos7 sticky_dir]$ echo 1324 >> bbb
[zhangsan@centos7 sticky_dir]$ echo 6666 > bbb
[zhangsan@centos7 sticky_dir]$ cat bbb
6666
[zhangsan@centos7 sticky_dir]$ echo 1234 >> ccc
[zhangsan@centos7 sticky_dir]$ echo 666 > ccc
#雖然文件權(quán)限為777,但是目錄設(shè)置STICKY,所以其他用戶沒辦法刪除文件
[zhangsan@centos7 sticky_dir]$ rm bbb
rm: cannot remove ‘bbb’: Operation not permitted
[zhangsan@centos7 sticky_dir]$ rm ccc
rm: cannot remove ‘ccc’: Operation not permitted
[root@centos7 tmp]# su xiaozeng
[xiaozeng@centos7 tmp]$ ll
total 0
drwxrwxrwt. 2 xiaozeng xiaozeng 28 May 30 05:53 sticky_dir
[xiaozeng@centos7 tmp]$ chmod 0777 sticky_dir/
[xiaozeng@centos7 tmp]$ ll
total 0
drwxrwxrwx. 2 xiaozeng xiaozeng 28 May 30 05:53 sticky_dir
[xiaozeng@centos7 tmp]$ chmod 1777 sticky_dir/
[xiaozeng@centos7 tmp]$ ll
total 0
drwxrwxrwt. 2 xiaozeng xiaozeng 28 May 30 05:53 sticky_dir
特殊屬性
另外還有兩種特殊的屬性
i屬性
添加i屬性,文件不能寫,不能刪除,root用戶也受限
#創(chuàng)建文件test
[root@centos7 tmp]# echo "hell world" > test
[root@centos7 tmp]# cat test
hell world
#給文件添加i屬性
[root@centos7 tmp]# chattr +i test
#使用ls查看不到i屬性,需要使用lsattr
[root@centos7 tmp]# ll test
-rw-r--r--. 1 root root 11 May 30 10:04 test
#使用lsattr查看
[root@centos7 tmp]# lsattr test
----i----------- test
#設(shè)置i屬性后,文件不能添加
[root@centos7 tmp]# echo add >> test
-bash: test: Permission denied
#也無法刪除
[root@centos7 tmp]# rm test
rm: remove regular file ‘test’? y
rm: cannot remove ‘test’: Operation not permitted
#使用-i取消i屬性
[root@centos7 tmp]# chattr -i test
#查看文件屬性
[root@centos7 tmp]# lsattr test
---------------- test
#取消屬性后,可以刪除文件
[root@centos7 tmp]# rm test
rm: remove regular file ‘test’? y
[root@centos7 tmp]#
a屬性
添加a屬性,文件只能追加,不能刪除,root用戶也受限
#創(chuàng)建文件test
[root@centos7 tmp]# echo hello > test
#添加a屬性
[root@centos7 tmp]# chattr +a test
#無法重寫文件
[root@centos7 tmp]# echo world > test
-bash: test: Operation not permitted
#可以追加
[root@centos7 tmp]# echo world >> test
#可以查看
[root@centos7 tmp]# cat test
hello
world
#無法刪除,root也不行
[root@centos7 tmp]# rm test
rm: remove regular file ‘test’? y
rm: cannot remove ‘test’: Operation not permitted
#查看文件屬性
[root@centos7 tmp]# lsattr test
-----a---------- test
#去掉文件a屬性
[root@centos7 tmp]# chattr -a test
[root@centos7 tmp]# lsattr test
---------------- test
#去掉屬性后可以刪除文件
[root@centos7 tmp]# rm test
rm: remove regular file ‘test’? y
[root@centos7 tmp]#