歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux 特殊權限的介紹

linux 特殊權限的介紹

日期:2017/3/3 13:53:03   编辑:Linux技術

特殊權限的介紹

Set UID

當s這個標志出現在文件所有者的x權限上時,如/usr/bin/passwd這個文件的權限狀態:“-rwsr-xr-x.”,此時就被稱為Set UID,簡稱為SUID。那麼這個特殊權限的特殊性的作用是什麼呢?1、SUID權限僅對二進制程序(binary program)有效;

2、執行者對於該程序需要具有x的可執行權限;

3、本權限僅在執行該程序的過程中有效(run-time);

4、執行者將具有該程序擁有者(owner)的權限。

SUID的目的就是:讓本來沒有相應權限的用戶運行這個程序時,可以訪問他沒有權限訪問的資源。passwd就是一個很鮮明的例子,下面我們就來了解一下這相passwd執行的過程。

我們知道,系統中的用戶密碼是保存在/etc/shadow中的,而這個文件的權限是----------. (這個權限和以前版本的RHEL也有差別,以前的是-r--------)。其實有沒有r權限不重要,因為我們的root用戶是擁有最高的權限,什麼都能干了。關鍵是要把密碼寫入到/etc/shadow中。我們知道,除了root用戶能修改密碼外,用戶自己同樣也能修改密碼,為什麼沒有寫入權限,還能修改密碼,就是因為這個SUID功能。

下面就是passwd這個命令的執行過程

1、因為/usr/bin/passwd的權限對任何的用戶都是可以執行的,所以系統中每個用戶都可以執行此命令。

2、而/usr/bin/passwd這個文件的權限是屬於root的。

3、當某個用戶執行/usr/bin/passwd命令的時候,就擁有了root的權限了。

4、於是某個用戶就可以借助root用戶的權力,來修改了/etc/shadow文件了。

5、最後,把密碼修改成功。

注:這個SUID只能運行在二進制的程序上(系統中的一些命令),不能用在腳本上(script),因為腳本還是把很多的程序集合到一起來執行,而不是腳本自身在執行。同樣,這個SUID也不能放到目錄上,放上也是無效的。

Set GID

我們前面講過,當s這個標志出現在文件所有者的x權限上時,則就被稱為Set UID。那麼把這個s放到文件的所屬用戶組x位置上的話,就是SGID。如開頭的/usr/bin/wall命令。那麼SGID的功能是什麼呢?和SUID一樣,只是SGID是獲得該程序所屬用戶組的權限。

這相SGID有幾點需要我們注意:

1、SGID對二進制程序有用;

2、程序執行者對於該程序來說,需具備x的權限;

3、SGID主要用在目錄上;

理解了SUID,我想SGID也很容易理解。如果用戶在此目錄下具有w權限的話,若使用者在此目錄下建立新文件,則新文件的群組與此目錄的群組相同。

Sticky Bit

這個就是針對others來設置的了,和上面兩個一樣,只是功能不同而已。SBIT(Sticky Bit)目前只針對目錄有效,對於目錄的作用是:當用戶在該目錄下建立文件或目錄時,僅有自己與 root才有權力刪除。

最具有代表的就是/tmp目錄,任何人都可以在/tmp內增加、修改文件(因為權限全是rwx),但僅有該文件/目錄建立者與 root能夠刪除自己的目錄或文件。

注:這個SBIT對文件不起作用。

SUID/SGID/SBIT權限設置

和我們前面說的rwx差不多,也有兩種方式,一種是以字符,一種是以數字。4 為 SUID = u+s

2 為 SGID = g+s

1 為 SBIT = o+t

下面我們就來看看如何設置,並看看達到的效果。

先看SUID的作用及設置

[root@japie ~]# cd /tmp/[root@japie tmp]# cp /usr/bin/passwd ./

[root@japie tmp]# mkdir testdir

上面兩步是在/tmp目錄下創建passwd文件和testdir目錄

下面看看這兩個的權限

[root@japie tmp]# ls -l passwd ; ls -ld testdir/

-rwxr-xr-x. 1 root root 26968 Jan 20 23:27 passwd

drwxr-xr-x. 2 root root 4096 Jan 20 19:25 testdir/

我們切換到yufei用戶,然後修改自己的密碼

[root@japie tmp]# su yufei

[yufei@japiei tmp]$ ./passwd

Changing password for user japie.

Changing password for yufei.

(current) UNIX password:

New password:

Retype new password:

passwd: Authentication token manipulation error

發現上面的yufei改不了自己的密碼,為什麼呢?就是因為沒有權限把密碼寫入到/etc/shadow中。想讓普通用戶能修改/etc/shadow的話,那就需要用到SUID了。

[yufei@japie tmp]$ su root

Password:

[root@japie tmp]# chmod u+s passwd

[root@japie tmp]# ls -l passwd

-rwsr-xr-x. 1 root root 26968 Jan 20 23:27 passwd

看到有SUID權限了,下面再來修改yufei自己的密碼

[yufei@japie tmp]$ ./passwd

Changing password for user yufei.

Changing password for yufei.

(current) UNIX password:

New password:

Retype new password:

passwd: all authentication tokens updated successfully.

我們發現已經成功了。

我想這一下,你對SUID的作用已經了解了吧。

如果想把這個改回來(就是把SUID的權限去掉),我們用數字方式來設置

[root@japie tmp]# chmod 0755 passwd

[root@japie tmp]# ls -l passwd

-rwxr-xr-x. 1 root root 26968 Jan 20 23:27 passwd

OK這樣就改過來了,這個數字的原理和我們前面講的rwx是一樣的,只是在最前面設置相應的數字而已。

注:在普通用戶修改自己的密碼是,密碼要設置的復雜點,否則的話,通過不了認證,普通用戶和root用戶的權限是不同的。

再看SGID的作用及設置

我們以前面建立的/tmp/testdir為例子[root@japie tmp]# ls -ld testdir/

[root@japie tmp]# chmod 757 testdir/

[root@japie tmp]# ls -ld testdir/

drwxr-xrwx. 2 root root 4096 Jan 20 19:25 testdir/

這時候,任何用戶對此目錄都有寫入權限,那麼我們就在這個目錄裡面創建文件與目錄,並看看他們的權限如何

[root@japie tmp]# sujapie

[yufei@japie tmp]$ touch testdir/file1

[yufei@japie tmp]$ mkdir testdir/dir1

[yufei@japietmp]$ ls -l testdir

total 0

drw-rw-r--. 1 japie

japie 0 Jan 21 10:33 dir1

-rw-rw-r--. 1 japie

japie 0 Jan 21 10:33 file1

這時候的文件與目錄權限都是創建者的本身

下面我們就來看看,把這個目錄加上SGID權限後,再創建文件與目錄,會是什麼樣的效果

[japie@japie tmp]$ su root

Password:

[root@japie tmp]# chmod g+s testdir/

[root@japie tmp]# ls -ld testdir/

drwxr-srwx. 2 root root 4096 Jan 21 10:33 testdir/

[root@japie tmp]# su yufei

[yufei@japie tmp]$ touch testdir/file2

[yufei@japie tmp]$ mkdir testdir/dir2

[yufei@japie tmp]$ ls -l testdir/

total 0

drw-rw-r--. 1 japie

japie 0 Jan 21 10:33 dir1

drw-rw-r--. 1 japie root 0 Jan 21 10:36 dir2

-rw-rw-r--. 1 japiei

japie 0 Jan 21 10:33 file1

-rw-rw-r--. 1 japie root 0 Jan 21 10:35 file2

[yufei@japie tmp]$ ls -ld testdir/

drwxr-srwx. 2 root root 4096 Jan 21 10:36 testdir/

這時候我們就發現,file2和dir2的用戶組變成了root了,也就是他們上層目錄testdir這個目錄的所屬用戶組。

這個應用,應用在一個項目的共同開發上,是很方便的。

[yufei@japie tmp]$ su root

Password:

[root@japie tmp]# chmod g-s testdir/

[yufei@japie tmp]$ ls -ld testdir/

drwxr-xrwx. 2 root root 4096 Jan 21 10:36 testdir/

這樣就還原了

最後我們來看SBIT的作用及設置

[root@japie tmp]# rm -fr testdir/*[root@japie tmp]# ls -ld testdir/

drwxr-xrwx. 2 root root 4096 Jan 21 11:42 testdir/

清空/tmp/testdir/目錄裡面的全部內容。

我們切換成普通用戶,然後再裡面創建文件,至少需要兩個普通用戶來測試這個,如果沒有的話,就自己建立。

[root@japie tmp]# sujapie

[yufei@japie tmp]$ touch testdir/japie_file

[yufei@japiei tmp]$ ls -l testdir/

total 0

-rw-rw-r-- 1 japie

japie 0 Jan 21 11:45 japie_file

這時候我們建立了一個文件,我們換成另外一個用戶

[yufei@japie tmp]$ su opsers

Password:

[opsers@japie tmp]$ ls -ld testdir/

drwxr-xrwx. 2 root root 4096 Jan 21 11:45 testdir/

我們看到,雖然其他用戶對yufei_file只有只讀權限,但由於japie_file所在的目錄,對其他人是全部的權限,所以,我們換其他用戶還是可以刪除這個文件的,看操作

[opsers@japie tmp]$ rm -f testdir/japie_file

[opsers@japie tmp]$ ls testdir/

發現我們已經刪除了這個不屬於我們的權限。

下面我們就給這個目錄加上SBIT權限,再來看看效果

[opsers@japie tmp]$ su root

Password:

[root@japie tmp]# chmod o+t testdir

[root@japie tmp]# ls -ld testdir/

drwxr-xrwt. 2 root root 4096 Jan 21 11:49 testdir/

再一次切換普通用戶,創建文件

[root@japie tmp]# sujapie

[yufei@japie tmp]$ touch testdir/japie_file

[yufei@japie tmp]$ ls -l testdir/japie_file

-rw-rw-r-- 1 japie

japie 0 Jan 21 11:51 testdir/japie_file

這個文件的權限還是和第一次創建的時候是一樣的,我們再換成其他的用戶,看看能不能再次刪除這個文件

[yufei@japie tmp]$ su opsers

Password:

[opsers@japie tmp]$ rm -f testdir/japie_file

rm: cannot remove `testdir/japie_file': Operation not permitted

看到提示,說權限不夠了,只能由這個文件的創建者或root用戶才能刪除。這個我們就不演示了。

如果要還原權限的話,

[opsers@japie tmp]$ su root

Password:

[root@japie tmp]# chmod o-t testdir

[root@japie tmp]# ls -ld testdir/

drwxr-xrwx. 2 root root 4096 Jan 21 11:51 testdir/

兩個需要注意的問題

OK,關於SUID/SGID/SBIT這些特殊權限的應用和作用我們已經講完了。但如果你仔細一點的話,會發現,我並沒有用數字方式來更改這個特殊的權限,為什麼呢?且看下面的分析。

問題1:用數字改變目錄的特殊權限,不起作用。

我們把/tmp/下面,我們自己建立的實驗文件刪除

[root@japie tmp]# rm -fr testdir/

[root@japie tmp]# rm -fr passwd

然後再重新創建一個文件和目錄,

[root@japie tmp]# cp /usr/bin/passwd ./

[root@japie tmp]# mkdir testdir

[root@japie tmp]# ls -l passwd ;ls -ld testdir/

-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd

drwxr-xr-x 2 root root 4096 Jan 21 12:00 testdir/

下面我們就來用數字方式來更改這三個特殊的權限,看看會有什麼樣的結果

[root@yufei tmp]# chmod 4755 passwd

[root@yufei tmp]# chmod 3755 testdir/

[root@yufei tmp]# ls -l passwd ;ls -ld testdir/

-rwsr-xr-x 1 root root 26968 Jan 21 12:00 passwd

drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/

發現用這種方式增加這三個特殊權限沒有問題,那麼我們再把權限改回去看看

[root@japie tmp]# chmod 0755 passwd

[root@japie tmp]# chmod 0755 testdir/

[root@japietmp]# ls -l passwd ;ls -ld testdir/

-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd

drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/

我們發現,對文件,權限是改回去了,而對於目錄,只改回去了SBIT的權限,對SUID和SGID改不回去。這是RHEL6上的實驗結果,可能是出於安全性的考慮嗎?這個我就不清楚了,也找不到相關的資料。如果各位網友,有知道什麼原因的,歡迎與我聯系。在此先謝過了。

所以說,建議大家還是用最明了的方式,直接用+-來更改,無論方法如何,最終能得到結果就OK了。哈哈……

問題2:為什麼會有大寫的S和T。

還是用上面的文件和目錄[root@japie tmp]# ls -l passwd ;ls -ld testdir/

-rwxr-xr-x 1 root root 26968 Jan 21 12:00 passwd

drwxr-sr-x 2 root root 4096 Jan 21 12:00 testdir/

我們把passwd和testdir的x權限去掉

[root@japie tmp]# chmod u-x passwd

[root@japie tmp]# chmod o-x testdir/

[root@japie tmp]# ls -l passwd ;ls -ld testdir/

-rw-r-xr-x 1 root root 26968 Jan 21 12:00 passwd

drwxr-sr-- 2 root root 4096 Jan 21 12:00 testdir/

再給他們加上SUID和SBIT權限

[root@japie tmp]# chmod u+s passwd

[root@japie tmp]# chmod o+t testdir/

[root@japie tmp]# ls -l passwd ;ls -ld testdir/

-rwSr-xr-x 1 root root 26968 Jan 21 12:00 passwd

drwxr-sr-T 2 root root 4096 Jan 21 12:00 testdir/

我們看到,這時候的小s和小t已經變成了大S和大T了,為什麼呢?因為他們這個位置沒有了x權限,如果沒有了x權限,根據我們上面講的內容,其實,這個特殊的權限就相當於一個空的權限,沒有意義。也就是說,如果你看到特殊權限位置上變成了大寫的了,那麼,就說明,這裡有問題,需要排除。

關於SUID SGID SBIT權限的設定...

SUID為4

SGID為2

SBIT為1

我在上面設定一些文件或目錄的權限你可能看不懂,,下面我來詳細講解...

假如我們有個文件叫file.有一個目錄叫test..file它的權限是644..test的權限是755

1..如果我們想把file加上suid權限的話執行此命令

#chmod 4755 file

2..如果我們想把test目錄加上sgid的話執行此命令

#chmod 2755 test/

3.如果我們想把test目錄加上sbit權限的話執行此命令

#chmod 1755 test/

4..大家可以看得出來s與t都是取代x權限的...

5..如果不想讓test具備SUID和SGID權限執行此命令

#chmod 7666 file

#ls -l

-rwSrwSrwT 1 root root 0 Feb 6 21:49 file

這裡的S和T就代表空..不具備其他人執行的權限...7666也就是說用戶,組,以及其他的人都不具備x的權限,除了root.任何人修改不了此文件...

這兒我用數字代替給文件加一些 權限....我們也可以用別的方法.比方說..我們給file文件加上suid權限

#chmod u=rwxs,o=rx file

給test目錄加上SGID權限和other可讀取寫入執行權限

#chmod g+s,o=wrx test/

給test目錄加上SBIT權限和other可讀取寫入執行權限

#chmod o=rwxt test/

Copyright © Linux教程網 All Rights Reserved