Linux文件系統(tǒng)權(quán)限
文件具有個應用權(quán)限的用戶類別:
- 文件歸用戶所有,通常是創(chuàng)建文件的用戶;
- 文件還歸單個組所有,通常是創(chuàng)建該文件的主要用戶組所有,但是可以進行更改;
- 可以為所屬用戶、所屬組和系統(tǒng)上的非用戶和非所屬組成員的其它用戶設(shè)置不同的權(quán)限
權(quán)限對文件和目錄的影響
- r:可讀取文件內(nèi)容;可列出目錄的內(nèi)容(文件名);
- w:可以更改文件內(nèi)容;可以創(chuàng)建或刪除目錄中的任一文件;
-x:可以作為命令執(zhí)行文件;目錄可以稱為當前工作目錄(可以cd它,但還需要讀取權(quán)限才能列出里面的文件)。
用戶通常對只讀目錄具有讀取和執(zhí)行權(quán)限,因此他們可以列出該目錄,并且對其內(nèi)容具有完整的只讀訪問權(quán)限。如果用戶僅對某目錄具有讀取訪問權(quán)限,可以列出其中的文件的名稱,但是其它信息都不可用,也不可訪問。如果用戶進隊目錄具有執(zhí)行權(quán)限,則無法列出目錄中的文件名。如果他們知道自己有讀取權(quán)限的文件的名稱,可通過顯示指定相對文件名,以便從目錄外部訪問該文件的內(nèi)容。
查看文件和目錄的權(quán)限以及所有權(quán)
- 使用ls命令的-l選項可以顯示有關(guān)權(quán)限和所有權(quán)限的詳細信息:
]$ ls -l
-rwxrwxr-x. 1 user user 18 Mar 18 17:30 aiguo
- 使用-d選項可以顯示有關(guān)權(quán)限和所有權(quán)的詳細信息
[user@serverb ~]$ ls -ld /home/user/tmp/
drwxrwxr-x. 2 user user 22 Mar 16 13:50 /home/user/tmp/
- 在ls -l長列表的顯示下
-- "-"表示常規(guī)文件
-- "d"表示目錄
-- "l"表示軟鏈接
-- "b"或"c"表示硬件設(shè)備
-- "p"或"s"表示特殊用途的文件 - 第一組rwx表示文件所有者權(quán)限;第二組rwx表示文件所屬組權(quán)限;第三組rwx表示其他人對文件的權(quán)限
]$ ls -l
total 4
-rwxrwxr-x. 1 user user 18 Mar 18 17:30 aiguo
-rw-r--r--. 1 root root 0 Mar 14 20:39 ak
drwxrwxr-x. 5 user user 170 Mar 15 21:43 glob
drwxrwxr-x. 2 user user 22 Mar 16 13:50 tmp
從命令行更改文件權(quán)限
1、使用chmod更改文件和目錄權(quán)限
- 字母方法
更改格式“chmod whowhatwhich file|directory”
who:表示所有者(u)、所屬組(g)、其他人(o)、所有人(a)
what:表示添加(+)、刪除(-)、精準設(shè)置(=)
which:表示權(quán)限(可讀)r、(可寫)w、(可執(zhí)行)x、(特殊執(zhí)行,僅針對目錄有效)X
[user@serverb prac]$ touch test
[user@serverb prac]$ ll
total 0
-rw-rw-r--. 1 user user 0 Apr 25 19:17 test
[user@serverb prac]$ chmod u+x test
[user@serverb prac]$ ll
total 0
-rwxrw-r--. 1 user user 0 Apr 25 19:17 test
[user@serverb prac]$ chmod g+x test
[user@serverb prac]$ ll
total 0
-rwxrwxr--. 1 user user 0 Apr 25 19:17 test
[user@serverb prac]$ chmod o+x test
[user@serverb prac]$ ll
total 0
-rwxrwxr-x. 1 user user 0 Apr 25 19:17 test
[user@serverb prac]$ chmod a-x test
[user@serverb prac]$ ll
total 0
-rw-rw-r--. 1 user user 0 Apr 25 19:17 test
[user@serverb prac]$ chmod o=rx test
[user@serverb prac]$ ll
total 0
-rw-rw-r-x. 1 user user 0 Apr 25 19:17 test
- 數(shù)字方法
更改格式“chmod ### file|directory”
r=4
w=2
x=1
注:第一個#表示所有者權(quán)限;第二個#表示所屬組權(quán)限;第三個#表示其他人權(quán)限
[user@serverb prac]$ ll
total 0
-rw-rw-r-x. 1 user user 0 Apr 25 19:17 test
[user@serverb prac]$ chmod 755 test
[user@serverb prac]$ ll
total 0
-rwxr-xr-x. 1 user user 0 Apr 25 19:17 test
[user@serverb prac]$ chmod 741 test
[user@serverb prac]$ ll
total 0
-rwxr----x. 1 user user 0 Apr 25 19:17 test
[user@serverb prac]$ chmod 532 test
[user@serverb prac]$ ll
total 0
-r-x-wx-w-. 1 user user 0 Apr 25 19:17 test
2、更改文件和目錄的用戶和組的所有權(quán)
- 使用chown課改文件的所有權(quán)
[root@serverb prac]# ll
total 0
-r-x-wx-w-. 1 user user 0 Apr 25 19:17 test
[root@serverb prac]# chown root:user test
[root@serverb prac]# ll
total 0
-r-x-wx-w-. 1 root user 0 Apr 25 19:17 test
[root@serverb prac]# chown :root test
[root@serverb prac]# ll
total 0
-r-x-wx-w-. 1 root root 0 Apr 25 19:17 test
[root@serverb prac]# chown user: test
[root@serverb prac]# ll
total 0
-r-x-wx-w-. 1 user user 0 Apr 25 19:17 test
- 使用chgrp命令可以更改文件的所有組
[root@serverb prac]# ll
total 0
-r-x-wx-w-. 1 user user 0 Apr 25 19:17 test
[root@serverb prac]# chgrp root test
[root@serverb prac]# ll
total 0
-r-x-wx-w-. 1 user root 0 Apr 25 19:17 test
[root@serverb prac]# chgrp wheel test
[root@serverb prac]# ll
total 0
-r-x-wx-w-. 1 user wheel 0 Apr 25 19:17 test
管理默認權(quán)限和文件訪問
- 特殊權(quán)限
-- 特殊權(quán)限構(gòu)成了除了基本用戶、組和其他類型之外的第四種權(quán)限類型
-- 特殊權(quán)限對文件和目錄的影響
1.u+s(suid):以擁有文件的用戶身份,而不是以運行文件的用戶身份執(zhí)行文件;對目錄無影響,只作用于可執(zhí)行的二進制命令
2.g+s(sgid):以擁有文件的組身份執(zhí)行文件;在目錄中最新創(chuàng)建的文件將其組所有者設(shè)置成和目錄的組的所有者相匹配
3.o+t(sticky):對文件無影響;對目錄具有寫入訪問權(quán)限的用戶僅可以刪除其所擁有的文件,而無法刪除或強制保存到其它用戶所擁有的文件
#suid權(quán)限
]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 27856 Apr 1 2020 /usr/bin/passwd
#權(quán)限為4755
#sgid權(quán)限
]$ ls -ld /run/log/journal/
drwxr-sr-x. 3 root systemd-journal 60 Mar 22 20:06 /run/log/journal/
#權(quán)限為2755
#sticky權(quán)限
]$ ls -ld /tmp/
drwxrwxrwt. 8 root root 187 Apr 25 03:27 /tmp/
- 默認權(quán)限
-- 在創(chuàng)建文件和目錄時,會為其分配一個初始權(quán)限。有兩個因素會影響這些權(quán)限。首先是要創(chuàng)建的是常規(guī)文件還是目錄,其次是當前的umask。
-- bash shell用戶的系統(tǒng)默認在/etc/profile和/etc/bashrc中定義,用戶也可以在其主目錄的/.bashrc和/.bash_profile文件中覆蓋系統(tǒng)默認值
~]$ umask
0002
~]$ ls -l
-rw-rw-r--. 1 user user 0 Apr 25 19:08 test
drwxrwxr-x. 2 user user 6 Apr 26 00:52 testdir
#臨時修改umask
[user@serverb ~]$ umask
0002
[user@serverb ~]$ umask 0000
[user@serverb ~]$ umask
0000
[user@serverb ~]$ mkdir testdir1
[user@serverb ~]$ ls -ld testdir1
drwxrwxrwx. 2 user user 6 Apr 26 01:27 testdir1
[user@serverb ~]$ touch test1
[user@serverb ~]$ ls -l test1
-rw-rw-rw-. 1 user user 0 Apr 26 01:27 test1