Linux中的主要權限有: R(讀), W(寫), X(執行)
兩大文件類型目錄和文件, 分別對應上面的三種權限,
目錄:
R: 可以用ls -l命令查看目錄自己的元數據
W:可以在此目錄中創建或者刪除文件, 前提是一定要有執行權限
X: 可以cd到目錄中,執行相關操作
文件:
R: 可以用ls -l命令查看文件的元數據
W: 可以修改文件
X: 可以執行文件(啓動成進程,並且提給進程), 默認文件沒有執行權限, 爲了避免權限過高的人誤運行腳本文件.
下面說下文件或目錄權限修改的命令的幾種格式
1. 八進制權限修改模式
chmod [OPTION]... OCTAL-MODE FILE...
用八進制碼來表示權限
每個文件都有屬主, 屬組, 和其他關係, 且分別對應RWX, 所有當我們ls -l查看某個文件時可以看到所有的權限
下圖, a的屬主,屬組爲root, 分別對應讀寫和只讀, 其他對應的權限是隻讀
[root@CentOS02 ~]# ls -l a -rw-r--r-- 1 root root 22 Sep 11 01:33 a
所以這裏一共有這八種組合
權限表示 | 對應的八進制格式 |
--- | 0 |
--x | 1 |
-w- | 2 |
-wx | 3 |
r-- | 4 |
r-x | 5 |
rw- | 6 |
rwx | 7 |
例
[root@CentOS02 ~]# chmod 774 a [root@CentOS02 ~]# ls -l a -rwxrwxr-- 1 root root 22 Sep 11 01:33 a
2. 直接指定使用者的權限
chmod [OPTION]... MODE[,MODE]... FILE...
前提:只有文件的屬主或root才能修改權限
直接用u或者g或者o後邊=r,w,x來指定權限, 什麼都不加等於---
可以多選項指定uo=r
[root@CentOS02 ~]# chmod u= a [root@CentOS02 ~]# ls -l a ----rwxr-- 1 root root 22 Sep 11 01:33 a [root@CentOS02 ~]# chmod g=rw a [root@CentOS02 ~]# ls -l a ----rw-r-- 1 root root 22 Sep 11 01:33 a [root@CentOS02 ~]# chmod uo=w a [root@CentOS02 ~]# ls -l a --w--wx-w- 1 root root 22 Sep 11 01:33 a
或者直接用+rwx 或-rwx來設定說有三個的權限, 注意:如果有w在,只修改屬主的
也可以分別只配置三個選項中的任意 go+x
[root@CentOS02 ~]# chmod -rwx a chmod: a: new permissions are ----w----, not --------- [root@CentOS02 ~]# ls -l a -----w---- 1 root root 22 Sep 11 01:33 a [root@CentOS02 ~]# chmod go+x a [root@CentOS02 ~]# ls -l a -----wx--x 1 root root 22 Sep 11 01:33 a
3. 參考文件權限
chmod [OPTION]... --reference=RFILE FILE...
[root@CentOS02 ~]# ls -l {a,b} --w--wx-w- 1 root root 22 Sep 11 01:33 a -rw-r--r-- 1 root root 0 Sep 11 02:21 b [root@CentOS02 ~]# chmod --reference a b [root@CentOS02 ~]# ls -l {a,b} --w--wx-w- 1 root root 22 Sep 11 01:33 a --w--wx-w- 1 root root 0 Sep 11 02:21 b
4, 修改目錄和目錄裏所有文件的屬性
加選項-R
[root@CentOS02 ~]# ls -ld test/ drwxr-xr-x 2 root root 4096 Sep 11 02:28 test/ [root@CentOS02 ~]# ls -l test/ total 0 -rw-r--r-- 1 root root 0 Sep 11 02:28 a -rw-r--r-- 1 root root 0 Sep 11 02:28 b [root@CentOS02 ~]# chmod -R 777 test/ [root@CentOS02 ~]# ls -l test/ total 0 -rwxrwxrwx 1 root root 0 Sep 11 02:28 a -rwxrwxrwx 1 root root 0 Sep 11 02:28 b [root@CentOS02 ~]# ls -ld test/ drwxrwxrwx 2 root root 4096 Sep 11 02:28 test/ [root@CentOS02 ~]#
修改屬主屬組命令
chown [OPTION]... [OWNER][:[GROUP]] FILE...
前提: 只有root才能修改屬主屬組.
1. 引號兩邊加要修改的屬主屬組, joice:jerry
2. 如果只修改屬主就不加引號 joice
3 .如果屬主屬組都修改成相同的 joice: 或者joice:joice
4. 只修改屬組 :joice
[root@CentOS02 ~]# ls -l a --w--wx-w- 1 root root 22 Sep 11 01:33 a [root@CentOS02 ~]# chown joice:jerry a [root@CentOS02 ~]# ls -l a --w--wx-w- 1 joice jerry 22 Sep 11 01:33 a
chown [OPTION]... --reference=RFILE FILE... 上chmod一樣, 也可以用引用修改
[root@CentOS02 ~]# ls -l a --w--wx-w- 1 joice jerry 22 Sep 11 01:33 a [root@CentOS02 ~]# ls -l {a,b} --w--wx-w- 1 joice jerry 22 Sep 11 01:33 a --w--wx-w- 1 root root 0 Sep 11 02:21 b [root@CentOS02 ~]# chown --reference a b [root@CentOS02 ~]# ls -l {a,b} --w--wx-w- 1 joice jerry 22 Sep 11 01:33 a --w--wx-w- 1 joice jerry 0 Sep 11 02:21 b
-R修改目錄和文件的屬主屬組
[root@CentOS02 ~]# chown -R :jerry test/ [root@CentOS02 ~]# ls -l test/ total 0 -rwxrwxrwx 1 root jerry 0 Sep 11 02:28 a -rwxrwxrwx 1 root jerry 0 Sep 11 02:28 b [root@CentOS02 ~]# ls -ld test/ drwxrwxrwx 2 root jerry 4096 Sep 11 02:28 test/21
只修改屬組命令
chgrp [OPTION]... GROUP FILE... 和上面命令類似, 但只修改屬組
[root@CentOS02 test]# chgrp joice a [root@CentOS02 test]# ls -l a -rwxrwxrwx 1 root joice 0 Sep 11 02:28 a
chgrp [OPTION]... --reference=RFILE FILE... 和上面命令類似, 但只引用屬組
[root@CentOS02 test]# ll total 0 -rwxrwxrwx 1 tom tom 0 Sep 11 02:28 a -rwxrwxrwx 1 tom jerry 0 Sep 11 02:28 b [root@CentOS02 test]# chgrp --reference a b [root@CentOS02 test]# ls -l total 0 -rwxrwxrwx 1 tom tom 0 Sep 11 02:28 a -rwxrwxrwx 1 tom tom 0 Sep 11 02:28 b
文件的特殊權
先來說說, 命令也是文件的一種, 它們的權限一般是775, 也就是說對所有人都有執行權限
[root@centos test]# ll `which grep` -rwxr-xr-x. 1 root root 111680 Jun 4 2014 /bin/grep [root@centos test]# ll `which find` -rwxr-xr-x. 1 root root 238976 Nov 11 2010 /bin/find
如果運行cat命令也就是用戶通過cat發起一個進程, 這個進程的權限就是用戶本身, 如果用cat打開一個文件,也就是以用戶的權限來打開文件. 而文件時候允許執行, 就需要一次查看文件屬主屬組和其他的屬性有無匹配.
權限匹配模型的順序
進程的發起者與被訪問文件的屬主有爲相同
進程的發起者與在不在被訪問文件的屬組裏
也other的訪問權限
特殊權限1: suid:Set UID
前提: 文件爲可執行文件或腳本
功效:任何用戶運行此文件文一個進程時, 進程的權限不是用本身, 而是命令文件的屬主
查詢: 屬主的執行位爲s, 如果本身有執行權限位小寫s, 沒有爲大寫S
設置: chmod u+s 文件
[root@centos test]# ll `which passwd` -rwsr-xr-x. 1 root root 30768 Feb 22 2012 /usr/bin/passwd [root@centos test]# chmod u+s a [root@centos test]# ll total 48 -r-Sr--r--. 1 root root 23617 Sep 16 23:35 a -rw-r--r--. 1 user1 root 23057 Sep 16 23:36 b -rw-rw-r--. 1 502 root 0 Sep 16 23:14 c [root@centos test]#
特殊權限2: sgid: Set GID
前提:作用於目錄
功效: 在此文件夾內創建的文件都屬於目錄的組, 也就是說所有用戶可以修改對方的文件, 如果umask002
查詢: 屬組的執行位爲s, 如果本身有執行權限位小寫s, 沒有爲大寫S
設置: chmod g+s 目錄
[root@centos test]# chmod g+s test01/ [root@centos test]# ll -d test01/ drwxr-sr-x. 2 root root 4096 Sep 17 14:11 test01/ [root@centos test]# chgrp user1 test01/ [root@centos test]# ll -d test01/ drwxr-sr-x. 2 root user1 4096 Sep 17 14:11 test01/ #修改sgid, 並制定user1爲屬組 [root@centos test]# touch test01/a [root@centos test]# ll test01/ total 0 -rw-r--r--. 1 root user1 0 Sep 17 14:15 a
特殊權限3: sticky
前提: 作用於目錄
功效: 在此文件的
[user2@centos test]$ id user1 uid=500(user1) gid=500(user1) groups=500(user1) [user2@centos test]$ id user2 uid=501(user2) gid=501(user2) groups=501(user2),500(user1) [root@centos test]# chmod o+t test01/ [root@centos test]# ll -d test01/ drwxrwsr-t. 3 root user1 4096 Sep 17 15:13 test01/ #修改文件t權限 [user2@centos test]$ touch test01/d [user2@centos test]$ ll test01/ total 4 -rw-r--r--. 1 root user1 0 Sep 17 14:15 a -rw-r--r--. 1 root user1 0 Sep 17 14:17 b -rw-r--r--. 1 root user1 0 Sep 17 14:51 c -rw-rw-r--. 1 user2 user1 0 Sep 17 15:13 d drwxr-sr-x. 2 root user1 4096 Sep 17 14:16 folder01 [user2@centos test]$ rm test01/a rm: remove write-protected regular empty file `test01/a'? y rm: cannot remove `test01/a': Operation not permitted #目錄對組user1有寫權限, 而user1, 和user2 都屬於user1的組,user2不能刪除user1創建的文件
特殊權限的八進制權限位
suid | sgid | sticky | |
0 | 0 | 0 | 0 |
0 | 0 | 1 | 1 |
0 | 1 | 0 | 2 |
0 | 1 | 1 | 3 |
1 | 0 | 0 | 4 |
1 | 0 | 1 | 5 |
1 | 1 | 0 | 6 |
1 | 1 | 1 | 7 |
在一般的3爲權限前多出一位表示特殊權限
chmond 2700 /tmp/test/a , a文件的guid設置爲有效和 chmond g+s /etc/test/a相同.
補充: 一般文件不能隨便修改屬主屬組, 我們又不能隨便給o寫權限, 所有就引入了facl的概念
FACL: File Access Control List
前提: 必須是root用戶執行
例子
[root@centos test]# setfacl -m u:user2:rw b [root@centos test]# getfacl b # file: b # owner: user1 # group: user1 user::rwx user:user2:rw- group::rwx mask::rwx other::rwx #給user2讀寫權限, 也可以設置組權限 [root@centos test]# setfacl -m g:user2:rw b [root@centos test]# getfacl b # file: b # owner: user1 # group: user1 user::rwx user:user2:rw- group::rwx group:user2:rw- mask::rwx other::rwx #或刪除user2的權限 [root@centos test]# setfacl -x u:user2 b [root@centos test]# getfacl b # file: b # owner: user1 # group: user1 user::rwx group::rwx group:user2:rw- mask::rwx other::rwx
-R 選項和chmond相同可以,改變目錄裏的所有文件.
facl和普通權限的訪問模型
用戶訪問這個文件時候
1. 先檢查原有屬主. 2. facl的屬主. 3. 原有的屬組. 4. facl的屬組. 5原有的其他