linux入門系列9--用戶管理及文件權限控制

​ 前面文章分享了Linux下常用命令以及Shell編程相關知識,本節繼續學習Linux用戶管理及文件權限控制。

​ Linux是多用戶多任務操作系統,具有很好的穩定性和安全性。既然是多用戶,那就意味着多個用戶可以同時使用同一個Linux操作系統,因此就會涉及用戶的添加、修改、刪除等管理工作以及權限分配問題;平時使用Linux系統一般是用於信息處理,而文件是信息載體,因此也需要掌握文件相關的操作和權限。

​ 相信大家平時在使用windows操作系統時,爲了不讓別人輕易看到某些敏感文件,而把文件設置爲隱藏文件,Linux下是否也能實現同樣的操作?是否能通過隱藏權限讓黑客最多隻能查看某些日誌文件但不能進行修改和刪除操作?是否能夠對某個用戶或某個用戶組進行特殊的權限設置,讓其只有能滿足工作需求的最小權限,從而降低安全風險?本篇文章將逐一解決這些疑問。

一、用戶及用戶組管理

1.1 用戶相關命令

​ 針對初學者,在日常工作中,一般都是領導分配一個擁有一定權限的賬號,然後開展各項工作,不會開放很高的權限。但初學階段,正如前面系列文章所演示,都是直接用root賬戶進行操作,這樣的目的是減少權限帶來的干擾,讓我們更專注於相應知識點的學習。但是在生產環境中建議慎用root,因爲權限太大,控制不當會有安全隱患。

1.1.1 who命令

​ who命令用於查看登錄用戶信息,包括:who、whoami、who am i。

  • whoami

功能描述:查看當前登錄用戶的用戶名

案例:

[root@heimatengyun test]# whoami
root
[root@heimatengyun test]# su - test
Last login: Sat Nov 30 22:55:38 CST 2019 on pts/0
[test@heimatengyun ~]$ whoami
test
[test@heimatengyun ~]$ exit
logout

可以看到,切換用戶後,相應的結果發生變化,只顯示當前登錄的用戶。

  • who am i

功能描述:顯示最初登錄時用的用戶名(無論切換幾次)

案例:

[root@heimatengyun test]# who am i
root     pts/0        2019-12-17 22:23 (192.168.78.1)
[root@heimatengyun test]# su - test
Last login: Tue Dec 17 22:31:09 CST 2019 on pts/0
[test@heimatengyun ~]$ who am i
root     pts/0        2019-12-17 22:23 (192.168.78.1)
[test@heimatengyun ~]$ exit
logout

​ 可以看到,切換後用戶名還是顯示最開始登錄時的用戶名稱。

  • who

功能描述:顯示當前有哪些用戶真正登錄到了本臺機器(不會顯示那些用su命令切換的用戶)

案例:

[root@heimatengyun test]# who
(unknown) :0           2019-12-17 22:22 (:0)
root     pts/0        2019-12-17 22:23 (192.168.78.1)
[root@heimatengyun test]# su - test
Last login: Tue Dec 17 22:34:44 CST 2019 on pts/0
[test@heimatengyun ~]$ who
(unknown) :0           2019-12-17 22:22 (:0)
root     pts/0        2019-12-17 22:23 (192.168.78.1)
[test@heimatengyun ~]$ exit
logout

​ 可以看到用su命令切換用戶後,顯示結果中並咩有test用戶,因此顯示的知識真正登錄到本機的所有用戶。

1.1.2 id命令

語法:id 用戶名

功能描述:判斷用戶是否存在

案例:

[root@heimatengyun test]# id test
uid=1000(test) gid=1000(test) groups=1000(test)
[root@heimatengyun test]# id lover
id: lover: no such user

​ 如果用戶存在返回用戶信息,如果用戶不存在則提示no such user

1.1.3 useradd命令

語法:

​ useradd [選項] 用戶名

功能描述:

​ 添加新用戶,默認的用戶家目錄存放在/home目錄中,默認的Shell解釋器爲 /bin/bash,同時會默認創建一個與該用戶同名的基本用戶組。在創建用戶時,通過以下參數可以修改默認設置。

選項:

參數 作用
-d home-dir,指定用戶的家目錄,默認爲/home/username
-e expiredate,賬戶到期時間,格式:YYYY-MM-DD
-u uid,指定用戶默認的UID
-g gid,指定初始用戶基本組,組必須已存在
-G groups,指定一個或多個擴展用戶組
-N no-user-group,不創建與用戶同名的基本用戶組
-s shell,指定用戶默認的Shell解釋器

案例:

(1)採用默認參數創建用戶

[root@heimatengyun test]# id lover
id: lover: no such user
[root@heimatengyun test]# useradd lover
[root@heimatengyun test]# id lover
uid=1001(lover) gid=1001(lover) groups=1001(lover)
[root@heimatengyun ~]# cat /etc/passwd
...省略部分內容
lover:x:1001:1001::/home/lover:/bin/bash

創建的用戶保存在/etc/passwd文件中,可以通過此文件查看用戶信息。

一行爲一條用戶記錄,分爲7個字段,每個字段用冒號分隔。每個字段分別對應:

用戶名:密碼:UID:GID:註釋:家目錄:僞用戶

(2)創建用戶指定家目錄、UID以及Shell解釋器

[root@heimatengyun ~]# useradd -d /home/heima -u 9988 -s /sbin/nologin heimage
[root@heimatengyun ~]# id heimage
uid=9988(heimage) gid=9988(heimage) groups=9988(heimage)
[root@heimatengyun ~]# ls /home/
heima  test

/sbin/nologin是終端解釋器中的一員,但是與Bash解釋器不同,被設置爲nologin後,用戶將不能登錄到系統中。

RHEL7(Centos7)系統中,用戶身份有3種:管理員、系統用戶、普通用戶。系統的管理員用戶UID爲0;系統用戶UID 爲 1~999, Linux 系統爲了避免因某個服務程序出現漏洞而被黑客提 權至整臺服務器,默認服務程序會有獨立的系統用戶負責運行,進而有效控制被破壞 範圍;普通用戶 UID 從 1000 開始,是由管理員創建的用於日常工作的用戶。

需要注意的是,UID 是不能衝突的,而且管理員創建的普通用戶的 UID 默認是從 1000 開始的(即使前面有閒置的號碼)。

另外,在 Linux 系統中創建每個用戶時,將自動創建一個與其同名的基本用戶組,而且 這個基本用戶組只有該用戶一個人。如果該用戶以後被歸納入其他用戶組,則這個其他用戶 組稱之爲擴展用戶組。一個用戶只有一個基本用戶組,但是可以有多個擴展用戶組,從而滿 足日常的工作需要。

1.1.4 passwd命令

語法:

​ passwd [選項] 用戶名

功能描述:

​ 設置或修改用戶密碼、過期時間、認證信息等。

選項:

參數 作用
-l lock,鎖定用戶,禁止登錄
-u unlock,解除鎖定,允許用戶登錄
-e expire,強制用戶在下次登錄時修改密碼
-S status,顯示yoghurt的密碼是否被鎖定,以及密碼採用的加密算法名稱

案例:

(1)修改其他賬戶密碼

[root@heimatengyun test]# passwd lover
Changing password for user lover.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

​ 修改的密碼不能太簡單,否則修改不成功。修改成功後,即可使用賬戶進行登錄。在界面中登錄後,即可查看當前登錄用戶

[root@heimatengyun test]# who
lover    :0           2019-12-17 23:05 (:0)
root     pts/0        2019-12-17 22:23 (192.168.78.1)

新建一個用戶,如果沒有設置密碼,則用戶無法直接登錄,只能通過root用戶使用su命令切換。因此,一般新建用戶就會同時設置密碼。也就是說useradd和passed命令一般是一起使用。

​ 無論是普通用戶還是超級權限用戶都可以運行passwd命令,但是如果是普通用戶則只能修改自己的密碼。配合選項參數可以實現更豐富的功能,具體用法可以通過man命令進行查看。

(2)鎖定及解鎖賬戶

假設你部門有一位同事要休假半年,那麼可以通過-l參數鎖定用戶,禁止其登錄,等休假完畢回來上班後再使用-u參數將其解鎖。這樣避免了刪除用戶、添加用戶帶來的麻煩同時也保證了這段時間內系統的安全。

[root@heimatengyun ~]# passwd -S test
test PS 2019-11-27 0 99999 7 -1 (Password set, SHA512 crypt.)
[root@heimatengyun ~]# passwd -l test
Locking password for user test.
passwd: Success
[root@heimatengyun ~]# passwd -S test
test LK 2019-11-27 0 99999 7 -1 (Password locked.)
[root@heimatengyun ~]# passwd -u test
Unlocking password for user test.
passwd: Success
[root@heimatengyun ~]# passwd -S test
test PS 2019-11-27 0 99999 7 -1 (Password set, SHA512 crypt.)
1.1.5 usermod命令

語法:

​ usermod [選項] 用戶名

功能描述:

​ 修改用戶信息

Linux系統一切皆文件,修改用戶也就是修改配置文件。用戶信息保存在/etc/passwd文件中,可以直接採用文本編輯器修改也可以通過usermod命令進行修改。

選項:

參數 作用
-e expiredate,賬號到期時間,格式爲YYYY-MM-DD
-g gid,變更所屬用戶組
-G groups,變更擴展用戶組
-L lock,鎖定用戶禁止其登錄
-U unlock,解鎖用戶,允許其登錄
-u uid,修改用戶的UID

案例:

​ 通過-G參數將上邊創建的lover用戶加入到root用戶組

[root@heimatengyun test]# id lover
uid=1001(lover) gid=1001(lover) groups=1001(lover)
[root@heimatengyun test]# usermod -G root lover
[root@heimatengyun test]# id lover             
uid=1001(lover) gid=1001(lover) groups=1001(lover),0(root)
1.1.5 userdel命令

語法:

​ userdel [選項] 用戶名

功能描述:

​ 當用戶不會再登錄系統,則使用此命令刪除用戶

選項:

參數 作用
-f force,強制刪除
-r remove,刪除用戶及用戶家目錄

案例:

​ 刪除之前創建的lover用戶,並刪除目錄

[root@heimatengyun home]# pwd
/home
[root@heimatengyun home]# ls
lover  test
[root@heimatengyun home]# userdel -r lover
userdel: user lover is currently used by process 4419
[root@heimatengyun home]# userdel -rf lover
[root@heimatengyun home]# ls
test

​ 刪除用戶時默認會保留用戶主目錄,添加-r參數則會刪除home目錄下用戶主目錄。-f表示強制刪除,由於前文通過界面上登錄了lover用戶,所以提示有進程在使用,通過-f強制刪除用戶。

1.2 用戶組相關命令

1.2.1 groupadd命令

語法:

​ groupadd [選項] 組名

功能描述:

​ 添加用戶組,有時候爲了高效的管理系統中各個用戶的權限,經常會將多個用戶添加到一個指定組中。

案例:

添加heima用戶組並查看組信息

[root@heimatengyun ~]# groupadd heima
[root@heimatengyun ~]# cat /etc/group
...省略部分內容
test:x:1000:test
heima:x:1001:

/etc/group文件包含所有組信息,可以查看到剛纔添加的heima用戶組。

/etc/group文件每行表示一條記錄,標識一個用戶組。每條記錄分爲四個字段,用冒號分割。第一字段:用戶組名稱;第二字段:用戶組密碼;第三字段:GID;第四字段:用戶列表,每個用戶之間用逗號分隔,本字段可以爲空

1.2.2 groupmod命令

語法:

​ groupmod [選項] 新組名 老組名

功能描述:

選項:

參數 作用
-n 修改組名稱

案例:

(1)修改heima組名稱heimage

[root@heimatengyun ~]# groupmod -n heimage heima
[root@heimatengyun ~]# cat /etc/group
...省略部分內容
test:x:1000:test
heimage:x:1001:

(2)新建並添加用戶到heimage組

[root@heimatengyun ~]# cat /etc/group
...省略部分內容
test:x:1000:test
heimage:x:1001:
[root@heimatengyun ~]# useradd -g 1001 heimagege
[root@heimatengyun ~]# id heimagege
uid=1001(heimagege) gid=1001(heimage) groups=1001(heimage)
[root@heimatengyun ~]# cat /etc/group
...省略部分內容
test:x:1000:test
heimage:x:1001:
[root@heimatengyun ~]# cat /etc/passwd
...省略部分內容
heimagege:x:1001:1001::/home/heimagege:/bin/bash
1.2.3 groupdel命令

語法:

​ groupdel 組名

功能描述:

​ 刪除組,前提是組內沒有用戶才能刪除

案例:

(1)刪除用戶組

[root@heimatengyun ~]# groupdel heimage
groupdel: cannot remove the primary group of user 'heimagege'
[root@heimatengyun ~]# userdel heimagege
[root@heimatengyun ~]# groupdel heimage 

(2)查看組內用戶

如案例1所示,如果組內有用戶則無法直接刪除組。可以通過/etc/group文件匹配對應的組名查看對應組內有哪些用戶

[root@heimatengyun ~]# grep 'test' /etc/group
test:x:1000:test

查找之前建的test組內有哪些用戶,第四個字段即爲該組內所有用戶列表。

二、文件及相關權限

2.1 文件權限

2.1.1 ll命令查看文件權限

Linux中一切皆文件,但是每個文件類型可能不同,如何區分文件類型呢?每個文件都有所有者和所有組以及其他人對文件擁有的讀、寫、執行權限,如何查看文件的這些權限呢?

當然是通過ls或ll命令就可以查看

[root@heimatengyun test]# ll
-rw-r--r--. 1 root root   9 Nov 30 20:43 test1.txt
drwxr-xr-x. 2 root root   6 Dec 20 11:32 test1

ll命令顯示結果詳解

file

以test1.txt文件爲例,各部分代表的含義依次爲如下

  • 文件類型:
    第一個字符“-”表示文件類型,此處表示test1.txt是一個普通文件。不同文件類型用不同字符表示,文件類型與字符對應關係如下
符號 文件類型
- 普通文件
d 目錄文件
l 連接文件
b 塊設備文件
c 字符設備文件
p 管理文件
s 套接字文件
  • 文件權限:

    “rw-r--r--.”,可以分爲四段,前三段每三位爲一段,最後一個點單獨爲一段。第一段rw-表示文件創建者/所有者對該文件所具有的權限,第二段r--表示創建者/所有者所在的組的其他用戶所具有的權限,第三段r--表示其他組的其他用戶所具有的權限。第四段.

字符 權限類型
r read,讀取權限,數字表示爲4。對文件而言,具有讀取文件內容的權限;對目錄來說,具有瀏覽目錄的權限
w write,寫入權限,數字表示爲2。對文件而言,具有新增、修改文件內容的權限;對目錄來說,具有刪除、移動目錄內文件的權限
x execute,執行權限,數字表示爲1。對文件而言,具有執行文件的權限;對目錄來說,該用戶具有進入目錄的權限。
s或S SUID,Set UID,可執行的文件搭配這個權限,便能得到特權,任意存取該文件的所有者能使用的全部系統資源。請注意具備SUID權限的文件,黑客經常利用這種權限,以SUID配上root帳號擁有者,無聲無息地在系統中開扇後門,供日後進出使用。
t或T sticky,/tmp和 /var/tmp目錄供所有用戶暫時存取文件,亦即每位用戶皆擁有完整的權限進入該目錄,去瀏覽、刪除和移動文件。
.或+ 如果爲+表示設置了ACL

此處test1.txt文件,其創建者/所有者具有可讀可寫的權限,其創建者/所有者所在的組的其他用戶具有可讀權限,其他組的其他用戶則具有可讀權限。

文件權限除了可以使用rwx表示,也可以用數字表示。如777代表:rwxrwxrwx(r=4,w=2,x=1,4+2+1=7=rwx),由此可以看出數字表示會簡潔一些。

  • 連接個數

    對於文件,表示指向它的鏈接文件的個數;對於目錄文件,表示它的第一級子目錄的個數。注意此處看到的值要減2纔等於該目錄下的子目錄的實際個數,比如test1目錄下其實並沒有任何文件和目錄,但此處顯示爲2,這是因爲要加上.目錄和..目錄。在linux下,.目錄表示當前目錄,..目錄表示上一級目錄。

  • 所有者和所屬組

    表示該文件的所有者/創建者(owner)及其所在的組(group)。

  • 文件大小

    如果是文件,則表示該文件的大小,單位爲字節。如果是目錄,則表示該目錄符所佔的大小,並不表示該目錄下所有文件的大小。

  • 修改日期

    該文件最後修改的日期時間。

  • 文件名稱

    文件或目錄的名稱。

不同的shell窗口,還能用顏色區分文件的屬性,能一目瞭然通過顏色區分文件類型,普通文件、可執行文件、壓縮文件、目錄、連接文件都有不同的顏色區分。不同工具可以根據自己需要進行顏色方案的修改和配置,這裏就不說了。

2.1.2 文件權限設置
2.1.2.1 chmod命令

語法:

​ chmod [選項] [ugoa] [+-=] [rwx或數字] 文件或目錄

選項參數:

選項 含義
-R recursive,遞歸執行
屬性符號 作用
u 文件擁有者
g 文件所屬組
o 文件所屬組外的其他用戶
a 所有用戶,相當於是ugo同時使用
操作符號 作用
+ 添加權限
- 刪除權限
= 設置權限,未指定的部分將被清除
權限符號 作用
r 對於文件有查看權限,對於目錄可以列出目錄內容
w 對於文件有修改權限,對於目錄可以在目錄中創建和刪除
x 對於文件有執行權限,對於目錄可以進入目錄

功能描述:

​ 改變文件或目錄權限

案例:

(1)分別通過ugoa添加文件的可執行權限

[root@heimatengyun test1]# ll
total 8
-rw-r--r--. 1 root root 6 Dec 20 14:52 hello
-rw-r--r--. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod u+x hello
[root@heimatengyun test1]# ll
total 8
-rwxr--r--. 1 root root 6 Dec 20 14:52 hello
-rw-r--r--. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod g+x hello
[root@heimatengyun test1]# ll
total 8
-rwxr-xr--. 1 root root 6 Dec 20 14:52 hello
-rw-r--r--. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod o+x hello
[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rw-r--r--. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod a+x test
[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxr-xr-x. 1 root root 6 Dec 20 14:51 test

從示例可以看到可以通過u、g、o分別對不同部分賦予權限,也可以直接用o一次性賦值。根據實際需要靈活選擇即可。

(2)移除所有者或所屬組之外其他用戶的執行權限

[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxr-xr-x. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod o-x test
[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxr-xr--. 1 root root 6 Dec 20 14:51 test

(3)用數字設置權限

[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxr-xr--. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod 777 test
[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 root root 6 Dec 20 14:51 test

(4)通過=號設置權限

[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chmod o=w test
[root@heimatengyun test1]# ll
total 8
-rwxr-xr-x. 1 root root 6 Dec 20 14:52 hello
-rwxrwx-w-. 1 root root 6 Dec 20 14:51 test

(5)改變目錄下所有文件

[root@heimatengyun test]# chmod -R 777 test1
[root@heimatengyun test]# ll
drwxrwxrwx. 2 root root  29 Dec 20 14:52 test1
[root@heimatengyun test]# cd test1/
[root@heimatengyun test1]# ll
total 8
-rwxrwxrwx. 1 root root 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 root root 6 Dec 20 14:51 test

可以看到內部的所有文件權限一次性被修改。

2.1.2.2 chown命令

語法:

​ chown [選項] 最終用戶[:最終所屬組] 文件或目錄

參數:

參數 作用
-R 遞歸修改

功能描述:

​ 改變文件或目錄的所有者

案例:

(1)修改文件所有者

[root@heimatengyun test1]# ll
total 8
-rwxrwxrwx. 1 root root 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 root root 6 Dec 20 14:51 test
[root@heimatengyun test1]# chown test test
[root@heimatengyun test1]# ll
total 8
-rwxrwxrwx. 1 root root 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 test root 6 Dec 20 14:51 test

(2)遞歸修改目錄及其下所有文件所有者

[root@heimatengyun test]# chown -R test:test /root/test/test1
[root@heimatengyun test]# ll
drwxrwxrwx. 2 test test  29 Dec 20 14:52 test1
[root@heimatengyun test]# cd test1/
[root@heimatengyun test1]# ll
total 8
-rwxrwxrwx. 1 test test 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 test test 6 Dec 20 14:51 test

這種用法在修改所有者時同時修改了所屬組。注意,修改的當前目錄及其下的所有文件都會修改,上級目錄不會影響。如此處的/root/test目錄不會變化,只會影響test1目錄。

2.1.2.3 chgrp命令

語法:

​ chgrp 最終用戶組 文件或目錄

功能描述:

​ 改變文件或目錄的所屬組

案例:

[root@heimatengyun test1]# ll
total 8
-rwxrwxrwx. 1 test test 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 test test 6 Dec 20 14:51 test
[root@heimatengyun test1]# chgrp root test
[root@heimatengyun test1]# ll
total 8
-rwxrwxrwx. 1 test test 6 Dec 20 14:52 hello
-rwxrwxrwx. 1 test root 6 Dec 20 14:51 test

2.2 文件特殊權限

​ 單純設置文件的 rwx 一般權限無法滿足我們對安全和靈活性的需求,因此便有了 SUID、SGID 與 SBIT 的特殊權限位。

​ 這是一種對文件權限進行設置的特殊功 能,可以與一般權限同時使用,以彌補一般權限不能實現的功能。

2.2.1 SUID

​ 針對二進制程序設置的特殊權限,可以讓二進制程序的執行者臨時擁有屬主的權限(僅對擁有執行權限的二進制程序有效)。SUID是一種有條件的、臨時的特殊權限授權方法,下文以passwd命令進行介紹。

​ 還記得1.1.4講的passwd命令嗎?該命令用於修改用戶密碼,所有用戶都可以執行 passwd 命 令來修改自己的用戶密碼,而用戶密碼保存在/etc/shadow 文件中,執行passwd命令本質就是修改shadow文件。

​ 但仔細查看這個文件就會發 現它的默認權限是 000,也就是說除了 root 管理員以外,所有用戶都沒有查看或編輯該文件的權限。

[root@heimatengyun test1]# ll /etc/shadow
----------. 1 root root 1157 Dec 20 00:06 /etc/shadow

​ 既然沒有此密碼文件讀寫權限,那爲何所有用戶都可以執行命令修改密碼呢?先不急,我們來看看passed命令的權限。

[root@heimatengyun test1]# ll /bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /bin/passwd

​ 可以看到所有者權限爲rws,以前講過可執行權限爲x,此處爲s。對,就是因爲所有者的權限由 rwx變成了rws,其中x改變成s就意味着該文件被賦予了SUID 權限。在使用 passwd 命令時如果加上 SUID 特殊權限位,就可讓普通用戶臨時獲得程序所有者的身份,把變更的密碼信息寫入到 shadow 文件中。

說明:設置SUID後,如果原本沒有執行權限,則爲S,有執行權限則爲s。如rwx將變爲rws,rw-變爲rwS。

設置SUID權限,就是使用之前介紹的chmod命令即可,針對命令或可執行二進制文件進行設置。

[root@heimatengyun test]# ll
-rwxrwxrwx. 1 root root 145 Dec  1 16:06 mypid.sh
[root@heimatengyun test]# chmod u+s mypid.sh 
[root@heimatengyun test]# ll
-rwsrwxrwx. 1 root root 145 Dec  1 16:06 mypid.sh
2.2.2 SGID

主要應用場景和功能有:

(1)讓執行者臨時擁有所屬組權限,對擁有執行權限的二進制程序進行設置。

(2)在某個目錄中創建的文件自動繼承該目錄的用戶組,只可以對目錄進行設置。

SGID 的第一種功能是參考 SUID 而設計的,不同點在於執行程序的用戶獲取的不再是文 件所有者的臨時權限,而是獲取到文件所屬組的權限。

針對第二種功能,我們知道,每個文件都有其歸屬的所有者和所屬組,當創建或傳送一個文件後,這個文件就會自動歸屬於執行這個操作的用戶,即該用戶就是文件的所有者。

假設有這樣一種情況,需要在部門內創建一個共享目錄,部門內所有人員都能讀取目錄中的內容。如果每個人都去建立各自的文件,所有者和所屬組都是創建者自己,別人無法使用。SGID的出現就是爲了解決這個問題,創建部門共享目錄後,在該目錄上設置 SGID 特殊權限位。這樣,部門內的任何人員在裏面創建的任何文件都會歸屬於該目錄的所屬組,而不再是自己的基本用戶組。

SGID功能就是在某個目錄中創建的文件自動繼承該目錄的用戶組,只可以對目錄進行設置。

下面演示在目錄上創建SGID

[root@heimatengyun test]# mkdir sgid
[root@heimatengyun test]# ll
drwxr-xr-x. 2 root root  6 Dec 20 18:11 sgid
[root@heimatengyun test]# chmod 777 sgid/
[root@heimatengyun test]# ll
drwxrwxrwx. 2 root root  6 Dec 20 18:11 sgid
[root@heimatengyun test]# chmod g+s sgid/
[root@heimatengyun test]# ll
drwxrwsrwx. 2 root root  6 Dec 20 18:11 sgid

創建SGID後,就可以切換到普通用戶,創建文件,觀察文件的所屬組

[root@heimatengyun test]# su - test
Last login: Fri Dec 20 17:26:36 CST 2019 on pts/0
[test@heimatengyun ~]$ cd sgid/
[test@heimatengyun sgid]$ echo 'hello'>hello
[test@heimatengyun sgid]$ ll
total 4
-rw-rw-r--. 1 test root 6 Dec 20 18:14 hello

可以看到test普通用戶創建的文件所屬組變爲給上層文件夾一致屬於root,不再屬於test自己。這樣針對所屬組設置權限,其他用戶就可以實現操作文件。

2.2.3 SBIT

SBIT 特殊權限位可確保用戶只能刪除自己的文件,而不能刪除其他用戶的文件。當對某個目錄設置了 SBIT 粘滯位權限後,那麼該目錄中的文件就只能被其所有者執行刪除操作了。

回到上文的例子,當一個部門共享一個目錄後,如何避免用戶刪除其他用戶的文件呢?顯然用SBIT就可以保證用戶不能刪除別人的文件。

設置目錄SBIT,同樣用chmod命令

[root@heimatengyun test]# mkdir sbit
[root@heimatengyun test]# ll
drwxr-xr-x. 2 root root  6 Dec 20 18:26 sbit
[root@heimatengyun test]# chmod -R o+t sbit/
[root@heimatengyun test]# ll
drwxr-xr-t. 2 root root  6 Dec 20 18:26 sbit

與前面所講的 SUID 和 SGID 權限顯示方法不同,當目錄被設置 SBIT 特殊權限位後,文件的其他人權限部分的 x 執行權限就會被替換成 t 或者 T,原本有 x 執行權限則會寫成 t,原本沒有 x 執行權限則會被寫成 T。

我們仔細觀察,就會發現/tmp目錄其實就是默認設置了SBIT,它是一個共享目錄,保證用戶只能刪除自己的文件。

[root@heimatengyun /]# ll -d /tmp/
drwxrwxrwt. 15 root root 4096 Dec 20 18:30 /tmp/

創建新用戶並在tmp下創建文件,驗證用其他用戶去刪除看能否刪除,答案肯定是不能刪除的。

[root@heimatengyun /]# useradd heimagege
[root@heimatengyun /]# su - heimagege
[heimagege@heimatengyun ~]$ cd /tmp/
[heimagege@heimatengyun tmp]$ echo 'heimagege'>heimagege
[heimagege@heimatengyun tmp]$ ll
-rw-rw-r--. 1 heimagege heimagege 10 Dec 20 18:34 heimagege
[root@heimatengyun /]# su - test
Last login: Fri Dec 20 18:29:10 CST 2019 on pts/0
[test@heimatengyun ~]$ cd /tmp/
[test@heimatengyun tmp]$ rm -f heimagege 
rm: cannot remove ‘heimagege’: Operation not permitted

2.3 文件隱藏屬性

​ Linux 系統中的文件除了具備一般權限和特殊權限之外,還有一種隱藏權限,即被隱藏起 來的權限,默認情況下不能直接被用戶發覺。當你新接手一臺服務器,碰到明明權限充足但卻無法刪除某個文件的情況,或者僅能在日誌文件中追加內容而不能修改或刪除內容,這肯you可能就是設置了文件隱藏屬性。這種屬性在一定程度上阻止了黑客篡改系統日誌的圖謀,因此這種“奇怪”的文件也保障了Linux 系統的安全性。

2.3.1 chattr命令

語法:

​ chattr [選項] [+-=參數] 文件

選項及參數:

選項 作用
R 遞歸
參數 作用
i 無法對文件進行修改;若對目錄設置了該參數,則僅能修改其中的子文件內容 而不能新建或刪除文件
a 僅允許補充(追加)內容,無法覆蓋/刪除內容(Append Only)
S 文件內容在變更後立即同步到硬盤(sync)
s 徹底從硬盤中刪除,不可恢復(用 0 填充原文件所在硬盤區域)
A 不再修改這個文件或目錄的最後訪問時間(atime)
b 不再修改文件或目錄的存取時間
d 使用 dump 命令備份時忽略本文件/目錄
u 當刪除該文件後依然保留其在硬盤中的數據,方便日後恢復

功能描述:

​ 設置文件的隱藏權限

案例:

通過隱藏屬性,設置文件不能刪除

[root@heimatengyun test]# echo 'test chattr'>testattr
[root@heimatengyun test]# chattr +a testattr 
[root@heimatengyun test]# rm testattr 
rm: remove regular file ‘testattr’? y
rm: cannot remove ‘testattr’: Operation not permitted
2.3.2 lsattr命令

語法:

​ lsattr 文件

功能描述:

​ 顯示文件的隱藏權限

案例:

​ 查看文件隱藏屬性,刪除隱藏屬性

[root@heimatengyun test]# lsattr testattr 
-----a---------- testattr
[root@heimatengyun test]# chattr -a testattr 
[root@heimatengyun test]# lsattr testattr    
---------------- testattr
[root@heimatengyun test]# rm testattr 
rm: remove regular file ‘testattr’? y
[root@heimatengyun test]# 

​ 可以看到,刪除隱藏屬性後文件刪除成功。

2.3 文件訪問控制列表

前文講解的一般權限、特殊權限、隱藏權限都是針對某一類用戶設置的。如果希望對某個指定的用戶進行單獨的權限控制,就需要用到文件 的訪問控制列表(ACL)了。

2.3.1 setfacl命令

基於普通文件或目錄設置 ACL 其實就是針對指定的用戶或用戶組設置文件或目錄的操作權限。另外,如果針對某個目錄設置了 ACL,則目錄中 的文件會繼承其 ACL;若針對文件設置了 ACL,則文件不再繼承其所在目錄的 ACL。

文件的 ACL 提供的是在所有者、所屬組、其他人的讀/寫/執行權限之外的特殊權限控制,使用 setfacl 命令可以針對單一用戶或用戶組、單一文件或目錄來進行讀/寫/執行權限的控制。

語法:

​ setfacl [參數] 文件名稱

參數選項:

參數 作用
-R 遞歸參數
-m modify修改目錄或文件的ACL
-b 刪除擴展ACL,保留原有的基礎權限

功能描述:

​ 管理文件的 ACL 規則

案例:

讓普通用戶能訪問root目錄

[root@heimatengyun ~]# ll -d /root/
dr-xr-x---. 16 root root 4096 Dec 20 11:49 /root/
[root@heimatengyun ~]# su - test
Last login: Fri Dec 20 18:34:47 CST 2019 on pts/0
[test@heimatengyun ~]$ ls /root/
ls: cannot open directory /root/: Permission denied
[test@heimatengyun ~]$ exit
logout

未設置ACL前其他用戶是不能訪問root目錄的,設置root目錄ACL允許test訪問。

[root@heimatengyun ~]# setfacl -Rm u:test:rwx /root/
[root@heimatengyun ~]# ll -d /root/                 
dr-xrwx---+ 16 root root 4096 Dec 20 11:49 /root/
[root@heimatengyun ~]# su - test
Last login: Fri Dec 20 22:10:26 CST 2019 on pts/0
[test@heimatengyun ~]$ ls /root/
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates  Videos  Desktop          Downloads  Music                 Public    test

同時通過ll命令可以看到權限後邊的點變爲了+。這個標識說明已經設置了ACL。

2.3.2 getfacl命令

語法:

​ getfacl 文件名稱

功能描述:

​ 查看文件上設置的ACL信息

案例:

(1)查看文件上設置的ACL

[test@heimatengyun ~]$ getfacl /root/
getfacl: Removing leading '/' from absolute path names
# file: root/
# owner: root
# group: root
user::r-x
user:test:rwx
group::r-x
mask::rwx
other::---

(2)刪除文件ACL

[root@heimatengyun ~]# ll -d /root/
dr-xrwx---+ 16 root root 4096 Dec 20 11:49 /root/
[root@heimatengyun ~]# setfacl -b /root/
[root@heimatengyun ~]# ll -d /root/     
dr-xr-x---. 16 root root 4096 Dec 20 11:49 /root/

2.4 臨時權限提升

​ 平時學習一般直接用root可以避免各種配置服務或權限導致的干擾問題,使得學習中心放在相應的知識點上。但是正式工作中,往往很少用root操作,因此可能會涉及用戶切換以及用戶提權問題。

2.4.1 su命令

​ su 命令可以解決切換用戶身份的需求,使得當前用戶在不退出登錄的情況下,順暢地切 換到其他用戶。

​ 由於前文已經演示了su命令的用法,因此不再贅述。只是要注意su命令與用戶名之間有一個減號(-),這意味着完全切 換到新的用戶,即把環境變量信息也變更爲新用戶的相應信息,而不是保留原始的信息。強 烈建議在切換用戶身份時添加這個減號(-)。另外,當從 root 管理員切換到普通用戶時是不需要密碼驗證的,而從普通用戶切換成 root 管理員就需要進行密碼驗證了;這也是一個必要的安全檢查。

2.4.2 sudo命令

​ 儘管使用 su 命令後,普通用戶可以完全切換到 root 管理員身份來完成相應工作,但這將暴露 root 管理員的密碼,從而增大了系統密碼被黑客獲取的機率;這並不是最安全的方案。

​ sudo 命令可以把特定命令的執行權限賦予給指定用戶而無需給出root密碼, 這樣既可保證普通用戶能夠完成特定的工作,也可以避免泄露 root 管理員密碼。

語法格式:

​ sudo [參數] 命令

參數:

參數 作用
-l 列出當前用戶可執行的命令

功能描述:

​ sudo 命令用於給普通用戶提供額外的權限來完成原本 root 管理員才能完成的任務。

​ 使用sudo之前需要先配置sudo服務,配置文件爲/etc/sudoers,可以直接編輯此文件,也可以使用visudo命令進行配置。

案例:

(1)爲普通用戶test添加sudo權限

​ 未爲普通用戶配置sudo服務時,通過sodo命令查看能執行的命令,將提升沒有配置。

[root@heimatengyun ~]# su - test
Last login: Fri Dec 20 22:14:02 CST 2019 on pts/0
[test@heimatengyun ~]$ sudo -l
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.
[sudo] password for test: 
Sorry, user test may not run sudo on heimatengyun.

​ 通過visudo命令,在“root ALL=(ALL) ALL”後仿照添加“test ALL=(ALL) ALL”,然後保存退出。操作方式跟vi編輯器一致。

配置內容解釋:test ALL=(ALL) ALL 其中test爲用戶名,表示誰可以使用sudo,第一個ALL表示是運行使用的主機,等號之後括號內的ALL表示以誰的身份執行,最後的ALL表示可執行命令的列表。“誰可以使用 允許使用的主機=(以誰的身份) 可執行命令的列表”

配置後,再次使用test用戶來查看,將得出如下結果:

[root@heimatengyun ~]# su - test
Last login: Fri Dec 20 22:41:52 CST 2019 on pts/0
[test@heimatengyun ~]$ sudo -l
[sudo] password for test: 
Matching Defaults entries for test on this host:
    requiretty, !visiblepw, always_set_home, env_reset, env_keep="COLORS
    DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS", env_keep+="MAIL PS1
    PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE
    LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY
    LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL
    LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY",
    secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin
User test may run the following commands on this host:
    (ALL) ALL

表明針對test用戶的sudo服務配置成功。配置成功之後,就可以採用sudo提升test用戶的權限了。

[test@heimatengyun ~]$ ls /root/
ls: cannot open directory /root/: Permission denied
[test@heimatengyun ~]$ sudo ls /root/
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates  Videos
Desktop          Downloads  Music                 Public    test
[test@heimatengyun ~]$ 

是不是很神奇,test用戶通過sudo提權就可以看到root目錄內容了。

(2)按需爲用戶配置sudo權限

我們前邊直接給了ALL最大的權限,實際情況應該按需分配最小權限,比如我們只給test用戶cat命令權限。

通過whereis命令查看cat所在路徑,一定要給命令的絕對路徑,不然系統無法識別命令。

[test@heimatengyun ~]$ whereis cat
cat: /usr/bin/cat /usr/share/man/man1/cat.1.gz /usr/share/man/man1p/cat.1p.gz

通過visudo命令修改之前添加的內容爲:test ALL=(ALL) /usr/bin/cat。保存後切換到test普通用戶查看效果

[root@heimatengyun ~]# visudo 
...省略部分內容
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
#test ALL=(ALL) ALL
test ALL=(ALL) /usr/bin/cat
...省略部分內容
[test@heimatengyun ~]$ sudo -l
[sudo] password for test: 
Matching Defaults entries for test on this host:
    requiretty, !visiblepw, always_set_home, env_reset, env_keep="COLORS
    DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS", env_keep+="MAIL PS1
    PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE
    LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY
    LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL
    LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY",
    secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin
User test may run the following commands on this host:
    (ALL) /usr/bin/cat
[test@heimatengyun ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
[test@heimatengyun ~]$ sudo cat /etc/shadow
...省略部分內容
heimagege:!!:18250:0:99999:7:::
[test@heimatengyun ~]$ ls /root/
ls: cannot open directory /root/: Permission denied
[test@heimatengyun ~]$ 

從實驗結果可以看出,配置cat命令後,只能執行cat命令,再次使用ls命令就不能看到root目錄內容。這樣權限就得到了很好的控制。

學習完用戶及文件相關權限知識後,下一篇文章我們將講解防火牆相關知識。

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