linux 用戶及權限補充

linux用戶及權限補充

我們都知道linux系統是多用戶、多任務的操作系統,用戶管理和權限管理參考http://2148fa7e.wiz03.com/share/s/0xifF-0vJQcD2FDV9W2F5M9r1aFl-f0ZNQvj2HbV7c277fDm

今天我們將對linux系統原有權限進行補充。
r w x -> 4 2 1

一、linux系統默認權限

當我們創建用戶和文件的時候,沒有設置任何權限,但是文件或者用戶都已經有了權限,這些權限是從哪裏來的呢?可以修改這些默認權限嗎?當然是可以的。
umask是指定目前用戶在創建文件或目錄時權限的默認值。
# 查看權限默認值(與一般權限有關的是後面三個數字)
umask
# 使用鍵值對的方式顯示, user、group、other的權限綁定。-S ( Symbolic )
umask -S
# u=rwx,g=rwx,o=rx

二、linux 文件隱藏屬性

沒錯,linux文件系統除了支持9位的普通權限外,還有隱藏屬性。不要小看隱藏屬性,對於系統的安全(Security)非常重要。
主要是lsattr和chattr兩個命令來實現。
注意: chattr命令只能在ext2、ext3、ext4的linux傳統文件系統上完整生效。最新的xfs文件系統上僅支持部分命令。
屬性設置常見的是a和i的設置值,設置該值需要使用root權限才能夠設置。  
xfs 文件系統僅支持 AadiS參數。

[root@study ~]# chattr [+-=][ASacdistu] 文件或目錄名稱 
選項與參數: 

+ :增加某一個特殊參數,其他原本存在參數則不動。 
- :移除某一個特殊參數,其他原本存在參數則不動。 
= :設置一定,且僅有後面接的參數 

A :當設置了 A 這個屬性時,若你有存取此文件(或目錄)時,他的存取時間 atime 將不會被修改, 可避免 I/O 較慢的機器過度的存取磁盤。(目前建議使用文件系統掛載參數處理這個項目)

S :一般文件是非同步寫入磁盤的(原理請參考前一章sync的說明),如果加上 S 這個屬性時, 當你進行任何文件的修改,該更動會“同步”寫入磁盤中。 

a :當設置 a 之後,這個文件將只能增加數據,而不能刪除也不能修改數據,只有root 才能設置這屬性 

c :這個屬性設置之後,將會自動的將此文件“壓縮”,在讀取的時候將會自動解壓縮, 但是在儲存的時候,將會先進行壓縮後再儲存(看來對於大文件似乎蠻有用的!) 

d :當 dump 程序被執行的時候,設置 d 屬性將可使該文件(或目錄)不會被 dump 備份 

i :這個 i 可就很厲害了!他可以讓一個文件“不能被刪除、改名、設置鏈接也無法寫入或新增數據!” 對於系統安全性有相當大的助益!只有 root 能設置此屬性 

s :當文件設置了 s 屬性時,如果這個文件被刪除,他將會被完全的移除出這個硬盤空間, 所以如果誤刪了,完全無法救回來了喔! 

u :與 s 相反的,當使用 u 來設置文件時,如果該文件被刪除了,則數據內容其實還存在磁盤中,可以使用來恢復文件。
attr 使用
# 查看文件隱藏屬性
[root@localhost custom-user]# lsattr
---------------- ./1.txt
---------------- ./2.txt
---------------- ./3.sh
---------------- ./tmp

# 設置文件隱藏屬性
[root@localhost custom-user]# chattr +a 1.txt
[root@localhost custom-user]# lsattr
-----a---------- ./1.txt
---------------- ./2.txt
---------------- ./3.sh
---------------- ./tmp

三、文件特殊權限 SUID SGID SBIT

權限名稱 作用範圍 符號 權限制 描述
SET UID 二進制文件 s 4 1. SUID 權限僅對二進制程序(binary program)有效;
2. 執行者對於該程序需要具有 x 的可執行權限;
3. 本權限僅在執行該程序的過程中有效 (run-time);
4. 執行者將具有該程序擁有者 (owner) 的權限。
SET GID 二進制文件/文件夾 s 2 文件:
1. SGID 對二進制程序有用;
2. 程序執行者對於該程序來說,需具備 x 的權限;
3. 執行者在執行的過程中將會獲得該程序羣組的支持!
文件夾:
1. 使用者若對於此目錄具有 r 與 x 的權限時,該使用者能夠進入此目錄;
2. 使用者在此目錄下的有效羣組(effective group)將會變成該目錄的羣組;
3.用途:若使用者在此目錄下具有 w 的權限(可以新建文件),則使用者所創建的新文件,該新文件的羣組與此目錄的羣組相同。
Sticky Bit 文件夾 t 1 1. 當使用者對於此目錄具有 w, x 權限,亦即具有寫入的權限時;
2. 當使用者在該目錄下創建文件或目錄時,僅有自己與 root 纔有權力刪除該文件。
SUID 執行過程
suid-01

同樣支持數字設置和符號設置

# 數字設置 -- beign --
[root@localhost custom-user]# chmod 4777 3.sh
[root@localhost custom-user]# ll 3.sh
-rwsrwxrwx 1 custom-user custom-group 21 2月  14 16:46 3.sh
# 去除user的s
[root@localhost custom-user]# chmod 0777 3.sh
[root@localhost custom-user]# ll 3.sh
-rwxrwxrwx 1 custom-user custom-group 21 2月  14 16:46 3.sh

# 設置文件夾
[root@localhost custom-user]# mkdir tmp
[root@localhost custom-user]# chmod 1777 tmp
[root@localhost custom-user]# ll
總用量 8
drwxrwxrwt 2 root        root           6 3月   8 03:08 tmp
# 刪除文件夾的Sticky Bit
[root@localhost custom-user]# chmod 0777 tmp/
[root@localhost custom-user]# ll
總用量 8
-rw------- 1 root        root           0 2月  14 14:06 1.txt
-r-xr--r-- 1 custom-user custom-group 889 2月  14 16:45 2.txt
-rwxrwxrwx 1 custom-user custom-group  21 2月  14 16:46 3.sh
drwxrwxrwx 2 root        root           6 3月   8 03:08 tmp

# 給沒有x權限的文件增加s權限
[root@localhost custom-user]# chmod 4677 3.sh
[root@localhost custom-user]# ll 3.sh
-rwSrwxrwx 1 custom-user custom-group 21 2月  14 16:46 3.sh

############  注意: 這個時候我們發現S變成了大寫。這說明s沒有生效,也就是說 s和t都是在 x基礎之上的權限,無法獨立設置。  ############


# 數字設置 -- end --

# 符號設置 -- beign --

# 設置SUID
[root@localhost custom-user]# chmod 777 1.txt
[root@localhost custom-user]# chmod u+s 1.txt
[root@localhost custom-user]# ll 1.txt
-rwsrwxrwx 1 root root 0 2月  14 14:06 1.txt

# 設置SGID
[root@localhost custom-user]# chmod g+s 1.txt
[root@localhost custom-user]# ll 1.txt
-rwsrwsrwx 1 root root 0 2月  14 14:06 1.txt

# 設置Sticky Bit
[root@localhost custom-user]# chmod o+t tmp
[root@localhost custom-user]# ll
總用量 8
-rwsrwsrwx 1 root        root           0 2月  14 14:06 1.txt
-r-xr--r-- 1 custom-user custom-group 889 2月  14 16:45 2.txt
-rwSrwxrwx 1 custom-user custom-group  21 2月  14 16:46 3.sh
drwxrwxrwt 2 root        root           6 3月   8 03:08 tmp

# 符號設置 -- end --
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章