前面文章分享了Linux下經常使用命令以及Shell編程相關知識,本節繼續學習Linux用戶管理及文件權限控制。linux
Linux是多用戶多任務操做系統,具備很好的穩定性和安全性。既然是多用戶,那就意味着多個用戶能夠同時使用同一個Linux操做系統,所以就會涉及用戶的添加、修改、刪除等管理工做以及權限分配問題;平時使用Linux系統通常是用於信息處理,而文件是信息載體,所以也須要掌握文件相關的操做和權限。算法
相信你們平時在使用windows操做系統時,爲了避免讓別人輕易看到某些敏感文件,而把文件設置爲隱藏文件,Linux下是否也能實現一樣的操做?是否能經過隱藏權限讓黑客最多隻能查看某些日誌文件但不能進行修改和刪除操做?是否可以對某個用戶或某個用戶組進行特殊的權限設置,讓其只有能知足工做需求的最小權限,從而下降安全風險?本篇文章將逐一解決這些疑問。shell
針對初學者,在平常工做中,通常都是領導分配一個擁有必定權限的帳號,而後開展各項工做,不會開放很高的權限。但初學階段,正如前面系列文章所演示,都是直接用root帳戶進行操做,這樣的目的是減小權限帶來的干擾,讓咱們更專一於相應知識點的學習。可是在生產環境中建議慎用root,由於權限太大,控制不當會有安全隱患。編程
who命令用於查看登陸用戶信息,包括:who、whoami、who am i。windows
功能描述:查看當前登陸用戶的用戶名安全
案例:bash
[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
能夠看到,切換用戶後,相應的結果發生變化,只顯示當前登陸的用戶。服務器
功能描述:顯示最初登陸時用的用戶名(不管切換幾回)編輯器
案例:ide
[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
能夠看到,切換後用戶名仍是顯示最開始登陸時的用戶名稱。
功能描述:顯示當前有哪些用戶真正登陸到了本臺機器(不會顯示那些用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用戶,所以顯示的知識真正登陸到本機的全部用戶。
語法: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
語法:
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 系統中建立每一個用戶時,將自動建立一個與其同名的基本用戶組,並且 這個基本用戶組只有該用戶一我的。若是該用戶之後被概括入其餘用戶組,則這個其餘用戶 組稱之爲擴展用戶組。一個用戶只有一個基本用戶組,可是能夠有多個擴展用戶組,從而滿 足平常的工做須要。
語法:
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.)
語法:
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)
語法:
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強制刪除用戶。
語法:
groupadd [選項] 組名
功能描述:
添加用戶組,有時候爲了高效的管理系統中各個用戶的權限,常常會將多個用戶添加到一個指定組中。
案例:
添加heima用戶組並查看組信息
[root@heimatengyun ~]# groupadd heima [root@heimatengyun ~]# cat /etc/group ...省略部份內容 test:x:1000:test heima:x:1001:
/etc/group文件包含全部組信息,能夠查看到剛纔添加的heima用戶組。
/etc/group文件每行表示一條記錄,標識一個用戶組。每條記錄分爲四個字段,用冒號分割。第一字段:用戶組名稱;第二字段:用戶組密碼;第三字段:GID;第四字段:用戶列表,每一個用戶之間用逗號分隔,本字段能夠爲空
語法:
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
語法:
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組內有哪些用戶,第四個字段即爲該組內全部用戶列表。
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命令顯示結果詳解
以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窗口,還能用顏色區分文件的屬性,能一目瞭然經過顏色區分文件類型,普通文件、可執行文件、壓縮文件、目錄、鏈接文件都有不一樣的顏色區分。不一樣工具能夠根據本身須要進行顏色方案的修改和配置,這裏就不說了。
語法:
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
能夠看到內部的全部文件權限一次性被修改。
語法:
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目錄。
語法:
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
單純設置文件的 rwx 通常權限沒法知足咱們對安全和靈活性的需求,所以便有了 SUID、SGID 與 SBIT 的特殊權限位。
這是一種對文件權限進行設置的特殊功 能,能夠與通常權限同時使用,以彌補通常權限不能實現的功能。
針對二進制程序設置的特殊權限,可讓二進制程序的執行者臨時擁有屬主的權限(僅對擁有執行權限的二進制程序有效)。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
主要應用場景和功能有:
(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本身。這樣針對所屬組設置權限,其餘用戶就能夠實現操做文件。
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
Linux 系統中的文件除了具有通常權限和特殊權限以外,還有一種隱藏權限,即被隱藏起 來的權限,默認狀況下不能直接被用戶發覺。當你新接手一臺服務器,碰到明明權限充足但卻沒法刪除某個文件的狀況,或者僅能在日誌文件中追加內容而不能修改或刪除內容,這肯you可能就是設置了文件隱藏屬性。這種屬性在必定程度上阻止了黑客篡改系統日誌的圖謀,所以這種「奇怪」的文件也保障了Linux 系統的安全性。
語法:
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
語法:
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]#
能夠看到,刪除隱藏屬性後文件刪除成功。
前文講解的通常權限、特殊權限、隱藏權限都是針對某一類用戶設置的。若是但願對某個指定的用戶進行單獨的權限控制,就須要用到文件 的訪問控制列表(ACL)了。
基於普通文件或目錄設置 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。
語法:
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/
平時學習通常直接用root能夠避免各類配置服務或權限致使的干擾問題,使得學習中心放在相應的知識點上。可是正式工做中,每每不多用root操做,所以可能會涉及用戶切換以及用戶提權問題。
su 命令能夠解決切換用戶身份的需求,使得當前用戶在不退出登陸的狀況下,順暢地切 換到其餘用戶。
因爲前文已經演示了su命令的用法,所以再也不贅述。只是要注意su命令與用戶名之間有一個減號(-),這意味着徹底切 換到新的用戶,即把環境變量信息也變動爲新用戶的相應信息,而不是保留原始的信息。強 烈建議在切換用戶身份時添加這個減號(-)。另外,當從 root 管理員切換到普通用戶時是不須要密碼驗證的,而從普通用戶切換成 root 管理員就須要進行密碼驗證了;這也是一個必要的安全檢查。
儘管使用 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目錄內容。這樣權限就獲得了很好的控制。
學習完用戶及文件相關權限知識後,下一篇文章咱們將講解防火牆相關知識。