linux特殊權限使用(suid、sgid、sbit)
一.linux特殊權限介紹
特殊權限
在linux系統(tǒng)中,除了r讀w寫x執(zhí)行權限外,還存在其他的權限;下圖中在權限位置上有不同的字母
二.suid
1.介紹suid之前需要了解一些問題
普通用戶是否可以修改密碼
可以修改自己密碼
/etc/shadow文件的作用是什么
存儲用戶密碼的文件
/etc/shadow文件的權限是什么
普通用戶是否可以修改/etc/shadow文件
不可以修改,沒有任何權限
為什么普通用戶可以修改密碼
- 使用了passwd命令
- passwd在x權限位上有s權限
- 在使用有suid權限的文件或者命令時,會以該文件的屬主身份去執(zhí)行該命令
2.suid作用詳解
概念:命令文件的x權限位變?yōu)閟,那么其他用戶執(zhí)行命令文件時,就會以該命令文件的屬主用戶去執(zhí)行
- 如果屬主權限位上有x,則會顯示小s
- 如果屬主權限位上沒有x,則會顯示大S
實例:
設置suid,普通用戶可以正常修改自己的密碼,取消suid,普通賬戶無法修改自己的密碼
[root@localhost ~]# useradd tdm #創(chuàng)建用戶tdm [root@localhost ~]# passwd tdm #設置密碼 Changing password for user tdm. New password: BAD PASSWORD: The password is shorter than 7 characters Retype new password: passwd: all authentication tokens updated successfully. #設置密碼成功 [root@localhost ~]# su - tdm #切換用戶tdm Last login: Mon Jun 5 11:21:29 CST 2023 on pts/0 [tdm@localhost ~]$ passwd #修改密碼 Changing password for user tdm. Changing password for tdm. (current) UNIX password: New password: Retype new password: passwd: all authentication tokens updated successfully. #修改密碼成功 [tdm@localhost ~]$ exit #退出登錄 logout [root@localhost ~]# chmod u-s /bin/passwd #修改passwd權限,取消suid權限 [root@localhost ~]# ll /bin/passwd #查看命令文件權限 -rwxr-xr-x. 1 root root 27832 Jun 10 2014 /bin/passwd #s變成了x [root@localhost ~]# su - tdm #再次切換tdm賬戶 Last login: Mon Jun 5 11:24:54 CST 2023 on pts/0 [tdm@localhost ~]$ passwd #修改密碼 Changing password for user tdm. Changing password for tdm. (current) UNIX password: New password: Retype new password: passwd: Authentication token manipulation error #密碼修改失敗
問題:如果普通用戶在使用passwd命令時,是以root的身份去執(zhí)行的,普通用戶是否能修改其他賬戶的密碼
無法修改,只有root在使用passwd命令時后面可以接用戶名稱,其他賬號不可以。
[tdm@localhost ~]$ passwd test #tdm想修改test的密碼 passwd: Only root can specify a user name. #只用root賬戶后面才能接用戶名稱
3.授權、撤銷suid權限
- 授權格式:
chmod u+s 文件名稱
- 撤銷格式:
chmod u-s 文件名稱
[root@localhost ~]# touch test.txt #創(chuàng)建文件 [root@localhost ~]# ll test.txt #查看文件屬性 -rw-r--r--. 1 root root 0 Jun 5 11:37 test.txt [root@localhost ~]# chmod u+s test.txt #授權suid屬性 [root@localhost ~]# ll test.txt -rwSr--r--. 1 root root 0 Jun 5 11:37 test.txt #文件具有suid屬性 [root@localhost ~]# chmod u-s test.txt #文件取消suid屬性 [root@localhost ~]# ll test.txt -rw-r--r--. 1 root root 0 Jun 5 11:37 test.txt #文件取消suid屬性 [root@localhost ~]# chmod 4644 test.txt #授權suid屬性 [root@localhost ~]# ll test.txt -rwSr--r--. 1 root root 0 Jun 5 11:37 test.txt #文件具有suid屬性 [root@localhost ~]# chmod 0644 test.txt #文件取消suid屬性 [root@localhost ~]# ll test.txt -rw-r--r--. 1 root root 0 Jun 5 11:37 test.txt #文件取消suid屬性
三.sgid
1.sgid作用詳解
概念:一般情況下是設置給目錄使用的,主要目的是讓彼得用戶無法刪除其他用戶所創(chuàng)建的文件或目錄
- 如果屬主權限位上有x,則會顯示小s
- 如果屬主權限位上沒有x,則會顯示大S
實例:
給目錄設置了sgid權限,在該目錄下創(chuàng)建的文件或者目錄的屬組都與該目錄一致
[root@localhost opt]# mkdir abc #創(chuàng)建目錄abc [root@localhost opt]# ll total 0 drwxr-xr-x. 2 root root 6 Jun 5 13:18 abc [root@localhost opt]# chmod 777 abc #授予abc權限 [root@localhost opt]# ll total 0 drwxrwxrwx. 2 root root 6 Jun 5 13:18 abc [root@localhost opt]# su - tdm #切換用戶 Last login: Mon Jun 5 13:15:58 CST 2023 on pts/0 [tdm@localhost ~]$ cd /opt/abc [tdm@localhost abc]$ touch test.txt #創(chuàng)建test文件 [tdm@localhost abc]$ ll total 0 -rw-rw-r--. 1 tdm tdm 0 Jun 5 13:18 test.txt #查看文件的屬主屬組,都是tdm [root@localhost opt]# exit #退出tdm賬戶 logout [root@localhost opt]# chmod g+s abc/ #給目錄abc授予sgid權限 [root@localhost opt]# ll total 0 drwxrwsrwx. 2 root root 22 Jun 5 13:18 abc #目錄具有sgid權限 [root@localhost opt]# su tdm [tdm@localhost opt]$ ll total 0 drwxrwsrwx. 2 root root 22 Jun 5 13:18 abc [tdm@localhost opt]$ cd abc [tdm@localhost abc]$ touch test1.txt #創(chuàng)建test1.txt文件 [tdm@localhost abc]$ ll total 0 -rw-rw-r--. 1 tdm root 0 Jun 5 13:21 test1.txt #該文件的屬組跟隨目錄的屬組是root -rw-rw-r--. 1 tdm tdm 0 Jun 5 13:18 test.txt
2.授權、撤銷sgid權限
- 授權格式:
chmod g+s 文件名稱
- 撤銷格式:
chmod g-s 文件名稱
四.sbit
1.sticky bit 詳解
- 如果屬主權限位上有x,則會顯示小t
- 如果屬主權限位上沒有x,則會顯示大T
只作用在目錄上,當一個目錄沒有設置sticky bit權限時,并且該目錄對所有的用戶都有讀寫執(zhí)行權限時,普通用戶在該目錄下創(chuàng)建的文件或目錄都會被其他用戶刪除
[root@localhost opt]# ll total 0 drwxrwxrwx. 2 root root 6 Jun 5 13:30 abc #當前目錄對所有用戶都有讀寫執(zhí)行權限 [root@localhost opt]# su test #切換至test賬戶 [test@localhost opt]$ ll total 0 drwxrwsrwx. 2 root root 39 Jun 5 13:21 abc [test@localhost opt]$ cd abc [test@localhost abc]$ ll total 0 -rw-rw-r--. 1 tdm root 0 Jun 5 13:21 test1.txt -rw-rw-r--. 1 tdm tdm 0 Jun 5 13:18 test.txt [test@localhost abc]$ rm -f test1.txt #刪除test1文件 [test@localhost abc]$ rm -f test.txt #刪除test文件 [test@localhost abc]$ ll #目錄為空,文件已被刪除 total 0
當一個目錄設置了sticky bit權限時,普通用戶在該目錄下所創(chuàng)建的文件或目錄,只能被該文件或目錄的屬主用戶或者root刪除,其他用戶無法刪除彼得用戶所創(chuàng)建的文件或者目錄
drwxrwxrwx. 2 root root 6 Jun 5 13:30 abc [root@localhost opt]# chmod o+t abc #給目錄授予sbit權限 [root@localhost opt]# ll total 0 drwxrwxrwt. 2 root root 6 Jun 5 13:30 abc [root@localhost opt]# su test #切換test賬戶 [test@localhost opt]$ cd abc [test@localhost abc]$ touch test.txt #創(chuàng)建文件 [test@localhost abc]$ ll total 0 -rw-rw-r--. 1 test test 0 Jun 5 13:47 test.txt [test@localhost abc]$ exit exit [root@localhost opt]# su tdm #切換tdm賬戶 [tdm@localhost opt]$ cd abc [tdm@localhost abc]$ ll total 0 -rw-rw-r--. 1 test test 0 Jun 5 13:47 test.txt [tdm@localhost abc]$ rm -f test.txt #tdm賬戶刪除test文件 rm: cannot remove ‘test.txt': Operation not permitted #操作不允許
2.授權、撤銷sbit權限
- 授權格式:
chmod o+t 文件名稱
- 撤銷格式:
chmod o-t 文件名稱
五.總結
SUID
: user - 占據(jù)屬主的執(zhí)行權限位s
: 屬主擁有 x 權限S
:屬主沒有 x 權限SGID
: group - 占據(jù) group 的執(zhí)行權限位s
: group 擁有 x 權限S
:group 沒有 x 權限Sticky
: other - 占據(jù) ohter 的執(zhí)行權限位t
: other 擁有 x 權限T
:other 沒有 x 權限
類別 | suid | sgid | sbit |
---|---|---|---|
字符表示 | S | S | T |
出現(xiàn)位置 | 用戶權限位x | 用戶組權限位x | 其他用戶權限位x |
基本權限位 | s | s | t |
數(shù)字表示法 | 4 | 2 | 1 |
八進制表示法 | 4000 | 2000 | 1000 |
生效對象 | 用戶位 | 用戶組 | 其他用戶 |
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
查看遠程 Linux 系統(tǒng)中某個端口是否開啟的三種方法
這篇文章主要介紹了查看遠程 Linux 系統(tǒng)中某個端口是否開啟的三種方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04apache服務器一個ip(如:127.0.0.1)和多個域名(虛擬主機)的綁定
今天在學習PHP時,用的是apache服務器有這樣的一個需求:一個ip(如:127.0.0.1)和多個域名(虛擬主機)綁定,以下是我的解決方案,需要的朋友可以參考下2015-01-01詳解CentOS 7 網(wǎng)卡命名修改為eth0格式
這篇文章主要介紹了詳解CentOS 7 網(wǎng)卡命名修改為eth0格式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03