Linux學習記錄 檔案特殊許可權

2021-09-01 22:44:01 字數 4459 閱讀 7694

檔案除了讀寫(r),寫(w),執行(x) 許可權,還有些特殊許可權(s,t)

功能:

suid許可權僅對二進位制程式有效

執行者對於程式需要有x可執行的許可權

執行者將均有改程式所有者的許可權

本許可權只在執行程式過程中有效

舉例:

普通使用者也可以通過命令passwd修改自己的密碼。修改的密碼內容將會記錄/etc/shadow檔案中,但是普通使用者對這個檔案無任何許可權,那如何修改這個檔案呢?

以上步驟可以理解為這樣

普通使用者執行passwd命令修改密碼èèpasswd命令程式修改/etc/shadow檔案將密碼記錄其中

[root@localhost /]# ll /etc/shadow/usr/bin/passwd

-r-------- 1 root root  1352 02-14 10:36 /etc/shadow

-rwsr-xr-x 1 root root 23420 2010-08-11/usr/bin/passwd

這就是suid的功能,當普通使用者在執行passwd程式命令時,由於passwd具有suid許可權,同時普通使用者對於passwd命令具有x許可權,那麼在passwd執行過程中普通使用者將程式所有者(root)的許可權,因此/etc/shadow就可以被修改

sgid對於二進位制程式來說,功能和suid差不多

sgid許可權對二進位制程式有效

執行者對於程式需要有x可執行的許可權

執行者將均有改程式使用者組的許可權

本許可權只在執行程式過程中有效

sgid也可以針對目錄設定,功能如下

使用者在具有sgid許可權的目錄下建立的檔案或目錄其所屬的使用者組就是目錄所有的有戶組

說明,預設情況下使用者建立的檔案所屬的使用者組為使用者的有效使用者組

舉例

[root@localhost /]# mkdir -m 777 /tmp/newdir;ll -d /tmp/newdir

drwxrwxrwx 2 root root 4096 03-10 12:35 /tmp/newdir

[root@localhost /]# cd /tmp/newdir/

[root@localhost newdir]# touch rootfile

[root@localhost newdir]# ll rootfile

-rw-r--r-- 1 root root 0 03-10 12:35 rootfile

=>所屬使用者和所屬使用者組都是root

[root@localhost newdir]# su tkf

=>切換到普通使用者

[tkf@localhost newdir]$ touch tkffile

[tkf@localhost newdir]$ ll

-rw-r--r-- 1 root root 0 03-10 12:35 rootfile

-rw-rw-r-- 1 tkf tkf 0 03-10 12:36 tkffile

=>所屬使用者和所屬使用者組都是tkf

[root@localhost ~]# chmod g+s /tmp/newdir/

=>給newdir新增sgid許可權

[root@localhost ~]# ll -d /tmp/newdir/

drwxrwsrwx 2 root root 4096 03-10 12:36 /tmp/newdir/

[root@localhost ~]# su tkf

[tkf@localhost root]$ touch /tmp/newdir/sgidfile;ll /tmp/newdir/

-rw-r--r-- 1 root root 0 03-10 12:35 rootfile

-rw-rw-r-- 1 tkf root 0 03-10 12:40 sgidfile

-rw-rw-r-- 1 tkf tkf 0 03-10 12:36 tkffile

=> sgidfile檔案所屬使用者組發生變化,和目錄(newdir)的使用者組一樣

sbit只針對目錄有效,其主要功能是

當使用者擁有目錄的wx許可權時,使用者可以刪除(刪除,重新命名,移動)目錄下的任意檔案

當目錄擁有sbit許可權時,即使使用者擁有目錄的wx許可權,使用者只能刪除自己建立的檔案(可以修改不是自己建立的檔案)。root使用者都可以刪除

舉例

[root@localhost ~]# mkdir -m 1777 /tmp/bitdir

[root@localhost ~]# su tkf

[tkf@localhost root]$ ll -d /tmp/bitdir/

drwxrwxrwt 2 root root 4096 03-10 12:59 /tmp/bitdir/

=>tkf使用者擁有目錄的rwx許可權

[tkf@localhost root]$ touch /tmp/bitdir/tkffile ;ll /tmp/bitdir/

-rw-rw-r-- 1 tkf tkf 0 03-10 12:59 tkffile

=>在目錄下建立檔案

[tkf@localhost root]$ su usera

=>切換另乙個使用者

[usera@localhost root]$ ll -d /tmp/bitdir/

drwxrwxrwt 2 root root 4096 03-10 12:59 /tmp/bitdir/

=> usera使用者擁有目錄的rwx許可權

[usera@localhost root]$ cd /tmp/bitdir/

[usera@localhost bitdir]$ touch userfile;ll

-rw-rw-r-- 1 tkf tkf 0 03-10 12:59 tkffile

-rw-rw-r-- 1 usera usera 0 03-10 13:04 userfile

=>在目錄下建立檔案userfile

[usera@localhost bitdir]$ rm tkffile

rm:是否刪除有寫保護的 一般空檔案 「tkffile」? y

rm: 無法刪除 「tkffile」: 不允許的操作

=>由於目錄具有sbit許可權 雖然usera對目錄具有wx許可權,但是不能刪除非他建立的檔案

[root@localhost ~]# chmod o-t /tmp/bitdir/

=>將目錄sbit許可權去掉

[root@localhost ~]# su usera

[usera@localhost root]$ cd /tmp/bitdir/

[usera@localhost bitdir]$ ll

-rw-rw-r-- 1 tkf tkf 0 03-10 12:59 tkffile

-rw-rw-r-- 1 usera usera 0 03-10 13:04 userfile

[usera@localhost bitdir]$ rm tkffile

rm:是否刪除有寫保護的 一般空檔案 「tkffile」? y

[usera@localhost bitdir]$ ll

-rw-rw-r-- 1 usera usera 0 03-10 13:04 userfile

=>可以刪除不是自己建立的檔案

和設定和基本許可權(rwx)方法基本,可以通過數字設定也可以通過符號設定

suid:4

sgid:2

sbit:1

suid:u+s

sgid:g+s

sbit:o+t

舉例:

[root@localhost ~]# touch test

[root@localhost ~]# chmod 4755 test; ll test

-rwsr-xr-x 1 root root 0 03-10 13:14 test

[root@localhost ~]# chmod 6755 test; ll test

-rwsr-sr-x 1 root root 0 03-10 13:14 test

[root@localhost ~]# chmod 1755 test; ll test

-rwxr-xr-t 1 root root 0 03-10 13:14 test

[root@localhost ~]# chmod 7666 test; ll test

-rwsrwsrwt 1 root root 0 03-10 13:14 test

=>這裡s,t都是大寫是因為檔案本身不具有x許可權,而s,t許可權設定成功的

=>前提是檔案具有x許可權,因此大寫的st代表檔案無這些特殊許可權

Linux檔案特殊許可權

linux中檔案除了r w x這三個讀 寫 執行的許可權還有特殊許可權 s t suid許可權 當s這個標誌出現在檔案所有者的x許可權上時 如 rwsr xr x 則被稱為set uid,簡稱suid許可權。suid限制 1 suid許可權僅對二進位制程式 可執行程式 有效。2 執行者對於該程式需要...

Linux檔案特殊許可權

檔案特殊許可權set uid set gid sticky bit 1 set uid 該許可權針對二進位制可執行檔案,使檔案在執行階段具有檔案所有者的許可權。比如 passwd具有該許可權 chmod u s filename filename必須是二進位制檔案 2 set gid 改許可權針對目...

Linux檔案特殊許可權

suid sgid sbit 先用ls l命令看一下下面幾個檔案或目錄的資訊 rwsr xr x.1 root root 25980 2月 22 2012 usr bin passwd rwx s x.1 root slocate 35612 8月 24 2010 usr bin locate dr...