Linux系統(tǒng)下如何查看及修改文件讀寫權限

查看文件權限的語句:

在終端輸入:

ls -l xxx.xxx

(xxx.xxx是文件名)

那么就會出現(xiàn)相類似的信息,主要都是這些:

-rw-rw-r--

一共有10位數(shù)

其中:最前面那個-代表的是類型

中間那三個rw-代表的是所有者(user)

然后那三個rw-代表的是組群(group)

最后那三個r--代表的是其他人(other)

然后我再解釋一下后面那9位數(shù):

r表示文件可以被讀(read)

w表示文件可以被寫(write)

x表示文件可以被執(zhí)行(如果它是程序的話)

-表示相應的權限還沒有被授予

現(xiàn)在該說說修改文件權限了

在終端輸入:

chmod o w xxx.xxx

表示給其他人授予寫xxx.xxx這個文件的權限

chmod go-rw xxx.xxx

表示刪除xxx.xxx中組群和其他人的讀和寫的權限

其中:

u代表所有者(user)

g代表所有者所在的組群(group)

o代表其他人,但不是u和g(other)

a代表全部的人,也就是包括u,g和o

r表示文件可以被讀(read)

w表示文件可以被寫(write)

x表示文件可以被執(zhí)行(如果它是程序的話)

其中:rwx也可以用數(shù)字來代替

r ------------4

w -----------2

x ------------1

- ------------0

行動:

表示添加權限

-表示刪除權限

=表示使之成為唯一的權限

當大家都明白了上面的東西之后,那么我們常見的以下的一些權限就很容易都明白了:

-rw------- (600)只有所有者才有讀和寫的權限

-rw-r--r-- (644)只有所有者才有讀和寫的權限,組群和其他人只有讀的權限

-rwx------ (700)只有所有者才有讀,寫,執(zhí)行的權限

-rwxr-xr-x (755)只有所有者才有讀,寫,執(zhí)行的權限,組群和其他人只有讀和執(zhí)行的權限

-rwx--x--x (711)只有所有者才有讀,寫,執(zhí)行的權限,組群和其他人只有執(zhí)行的權限

-rw-rw-rw- (666)每個人都有讀寫的權限

-rwxrwxrwx (777)每個人都有讀寫和執(zhí)行的權限

Linux文件和目錄訪問權限設置

使用chmod和數(shù)字改變文件或目錄的訪問權限文件和目錄的權限表示,是用rwx這三個字符來代表所有者、用戶組和其他用戶的權限。有時候,字符似乎過于麻煩,因此還有另外一種方法是以數(shù)字來表示權限,而且僅需三個數(shù)字。

r:

對應數(shù)值4

w:

對應數(shù)值2

x

:對應數(shù)值1

-:對應數(shù)值0

數(shù)字設定的關鍵是mode的取值,一開始許多初學者會被搞糊涂,其實很簡單,我們將rwx看成二進制數(shù),如果有則有1表示,沒有則有0表示,那么rwx r-x r- -則可以表示成為:

111 101 100

再將其每三位轉換成為一個十進制數(shù),就是754。例如,我們想讓a.txt這個文件的權限為:自己同組用戶其他用戶可讀是是是可寫是是可執(zhí)行那么,我們先根據(jù)上表得到權限串為:rw-rw-r--,那么轉換成二進制數(shù)就是110 110 100,再每三位轉換成為一個十進制數(shù),就得到664,因此我們執(zhí)行命令:

[root@localhost ~]# chmod 664 a.txt

按照上面的規(guī)則,rwx合起來就是4 2 1=7,一個rwxrwxrwx權限全開放的文件,數(shù)值表示為777;而完全不開放權限的文件“---------”其數(shù)字表示為000。下面舉幾個例子:

-rwx------:

等于數(shù)字表示700。

-rwxr—r--:

等于數(shù)字表示744。

-rw-rw-r-x:

等于數(shù)字表示665。

drwx—x—x:

等于數(shù)字表示711。

drwx------:

等于數(shù)字表示700。在文本模式下,可執(zhí)行chmod命令去改變文件和目錄的權限。我們先執(zhí)行l(wèi)s -l看看目錄內的情況:

[root@localhost ~]# ls -l

總用量368

-rw-r--r-- 1 root root 12172 8

月15 23:18 conkyrc.sample

drwxr-xr-x 2 root root 48 9

月4 16:32 Desktop

-r--r--r-- 1 root root 331844 10

月22 21:08 libfreetype.so.6

drwxr-xr-x 2 root root 48 8

月12 22:25 MyMusic

-rwxr-xr-x 1 root root 9776 11

月5 08:08 net.eth0

-rwxr-xr-x 1 root root 9776 11

月5 08:08 net.eth1

-rwxr-xr-x 1 root root 512 11

月5 08:08 net.lo

drwxr-xr-x 2 root root 48 9

月6 13:06 vmware

可以看到當然文件conkyrc.sample文件的權限是644,然后把這個文件的權限改成777。執(zhí)行下面命令

[root@localhost ~]# chmod 777 conkyrc.sample

然后ls -l看一下執(zhí)行后的結果:

[root@localhost ~]# ls -l

總用量368

-rwxrwxrwx 1 root root 12172 8

月15 23:18 conkyrc.sample

drwxr-xr-x 2 root root 48 9

月4 16:32 Desktop

-r--r--r-- 1 root root 331844 10

月22 21:08 libfreetype.so.6

drwxr-xr-x 2 root root 48 8

月12 22:25 MyMusic

-rwxr-xr-x 1 root root 9776 11

月5 08:08 net.eth0

-rwxr-xr-x 1 root root 9776 11

月5 08:08 net.eth1

-rwxr-xr-x 1 root root 512 11

月5 08:08 net.lo

drwxr-xr-x 2 root root 48 9

月6 13:06 vmware

可以看到conkyrc.sample文件的權限已經修改為rwxrwxrwx

如果要加上特殊權限,就必須使用4位數(shù)字才能表示。特殊權限的對應數(shù)值為:

s

或S(SUID):對應數(shù)值4。

s

或S(SGID):對應數(shù)值2。

t

或T:對應數(shù)值1。

用同樣的方法修改文件權限就可以了例如:

[root@localhost ~]# chmod 7600 conkyrc.sample

[root@localhost ~]# ls -l

總用量368

-rwS--S--T 1 root root 12172 8

月15 23:18 conkyrc.sample

drwxr-xr-x 2 root root 48 9

月4 16:32 Desktop

-r--r--r-- 1 root root 331844 10

月22 21:08 libfreetype.so.6

drwxr-xr-x 2 root root 48 8

月12 22:25 MyMusic

-rwxr-xr-x 1 root root 9776 11

月5 08:08 net.eth0

-rwxr-xr-x 1 root root 9776 11

月5 08:08 net.eth1

-rwxr-xr-x 1 root root 512 11

月5 08:08 net.lo

drwxr-xr-x 2 root root 48 9

月6 13:06 vmware

加入想一次修改某個目錄下所有文件的權限,包括子目錄中的文件權限也要修改,要使用參數(shù)-R表示啟動遞歸處理。例如:

[root@localhost ~]# chmod 777 /home/user

注:僅把/home/user目錄的權限設置為rwxrwxrwx

[root@localhost ~]# chmod -R 777 /home/user

注:表示將整個/home/user目錄與其中的文件和子目錄的權限都設置為rwxrwxrwx

使用命令chown改變目錄或文件的所有權文件與目錄不僅可以改變權限,其所有權及所屬用戶組也能修改,和設置權限類似,用戶可以通過圖形界面來設置,或執(zhí)行chown命令來修改。我們先執(zhí)行l(wèi)s -l看看目錄情況:

[root@localhost ~]# ls -l

總用量368

-rwxrwxrwx 1 root root 12172 8

月15 23:18 conkyrc.sample

drwxr-xr-x 2 root root 48 9

月4 16:32 Desktop

-r--r--r-- 1 root root 331844 10

月22 21:08 libfreetype.so.6

drwxr-xr-x 2 root root 48 8

月12 22:25 MyMusic

-rwxr-xr-x 1 root root 9776 11

月5 08:08 net.eth0

-rwxr-xr-x 1 root root 9776 11

月5 08:08 net.eth1

-rwxr-xr-x 1 root root 512 11

月5 08:08 net.lo

drwxr-xr-x 2 root root 48 9

月6 13:06 vmware

可以看到conkyrc.sample文件的所屬用戶組為root,所有者為root。執(zhí)行下面命令,把conkyrc.sample文件的所有權轉移到用戶user:

[root@localhost ~]# chown user conkyrc.sample

[root@localhost ~]# ls -l

總用量368

-rwxrwxrwx 1 user root 12172 8

月15 23:18 conkyrc.sample

drwxr-xr-x 2 root root 48 9

月4 16:32 Desktop

-r--r--r-- 1 root root 331844 10

月22 21:08 libfreetype.so.6

drwxr-xr-x 2 root root 48 8

月12 22:25 MyMusic

-rwxr-xr-x 1 root root 9776 11

月5 08:08 net.eth0

-rwxr-xr-x 1 root root 9776 11

月5 08:08 net.eth1

-rwxr-xr-x 1 root root 512 11

月5 08:08 net.lo

drwxr-xr-x 2 root root 48 9

月6 13:06 vmware

要改變所屬組,可使用下面命令:

[root@localhost ~]# chown :users conkyrc.sample

[root@localhost ~]# ls -l

總用量368

-rwxrwxrwx 1 user users 12172 8

月15 23:18 conkyrc.sample

drwxr-xr-x 2 root root 48 9

月4 16:32 Desktop

-r--r--r-- 1 root root 331844 10

月22 21:08 libfreetype.so.6

drwxr-xr-x 2 root root 48 8

月12 22:25 MyMusic

-rwxr-xr-x 1 root root 9776 11

月5 08:08 net.eth0

-rwxr-xr-x 1 root root 9776 11

月5 08:08 net.eth1

-rwxr-xr-x 1 root root 512 11

月5 08:08 net.lo

drwxr-xr-x 2 root root 48 9

月6 13:06 vmware

要修改目錄的權限,使用-R參數(shù)就可以了,方法和前面一樣。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容