linux下更改文件的權限

  1. 更改所屬組 chgrp

語法:chgrp  [組名]  [文件名]

[root@localhost ~]# groupadd testgroup
[root@localhost ~]# touch test1
[root@localhost ~]# ls -l test1
-rw-r--r-- 1 root root 0 5月  10 08:41 test1
[root@localhost ~]# chgrp testgroup test1
[root@localhost ~]# ls -l test1
-rw-r--r-- 1 root testgroup 0 5月  10 08:41 test1

這裏用到了 ‘groupadd’ 命令,其含義爲增加一個用戶組。該命令在以後章節中做詳細介紹,您只要知道它是用來增加用戶組的即可。除了更改文件的所屬組,還可以更改目錄的所屬組。

[root@localhost ~]# ls -l dirb/
總用量 8
drwxr-xr-x. 2 root root 4096 5月  10 05:08 dirc
-rw-r--r--. 1 root root   20 5月  10 05:37 filee
[root@localhost ~]# ls -ld dirb/
drwxr-xr-x. 3 root root 4096 5月  10 05:10 dirb/
[root@localhost ~]# chgrp testgroup dirb
[root@localhost ~]# ls -ld dirb/
drwxr-xr-x. 3 root testgroup 4096 5月  10 05:10 dirb/
[root@localhost ~]# ls -l dirb/
總用量 8
drwxr-xr-x. 2 root root 4096 5月  10 05:08 dirc
-rw-r--r--. 1 root root   20 5月  10 05:37 filee

‘chgrp’命令也可以更改目錄的所屬組,但是隻能更改目錄本身,而目錄下面的目錄或者文件沒有更改,要想級聯更改子目錄以及子文件,有個選項可以實現:

[root@localhost ~]# chgrp -R testgroup dirb
[root@localhost ~]# ls -l dirb
總用量 8
drwxr-xr-x. 2 root testgroup 4096 5月  10 05:08 dirc
-rw-r--r--. 1 root testgroup   20 5月  10 05:37 filee

‘chgroup’ 命令阿銘使用的不多,因爲還有一個命令可以替代。

  1. 更改文件的所屬主 chown

語法: chown [ -R ] 賬戶名 文件名 chown [ -R ] 賬戶名:組名 文件名

這裏的-R選項只作用於目錄,作用是級聯更改,即不僅更改當前目錄,連目錄裏的目錄或者文件全部更改。

[root@localhost ~]# mkdir test // 創建 'test' 目錄
[root@localhost ~]# useradd user1 // 創建用戶 'user1', 關於 'useradd' 命令會在後續章節介紹。
[root@localhost ~]# touch test/test2 // 在test目錄下創建test2文件
[root@localhost ~]# chown user1 test
[root@localhost ~]# ls -l test
總用量 0
-rw-r--r-- 1 root root 0 5月  10 09:00 test2
[root@localhost ~]# ls -ld test   // test目錄所屬主已經由 'root' 改爲 'user1'.
drwxr-xr-x 2 user1 root 4096 5月  10 09:00 test
[root@localhost ~]# ls -l test  // 但是test目錄下的test2文件所屬主依舊是 'root'.
總用量 0
-rw-r--r-- 1 root root 0 5月  10 09:00 test2
[root@localhost ~]# chown -R user1:testgroup test
[root@localhost ~]# ls -l test
總用量 0
-rw-r--r-- 1 user1 testgroup 0 5月  10 09:00 test2

‘chown -R user1:testgroup’ 把test目錄以及目錄下的文件都修改成所屬主爲user1, 所屬組爲testgroup.

  1. 改變用戶對文件的讀寫執行權限 chmod

在linux中爲了方便更改這些權限,linux使用數字去代替rwx, 具體規則爲 ‘r’ 等於4, ‘w’ 等於2, ‘x’ 等於1, ‘-‘ 等於0. 舉個例子: ‘-rwxrwx—’ 用數字表示就是 ‘770’, 具體是這樣來的: ‘rwx’ = 4+2+1=7; ‘rwx’ = 4+2+1=7; ‘- - -‘ = 0+0+0=0.

chmod 語法: chmod [-R] xyz 文件名 (這裏的xyz,表示數字)

‘-R’ 選項作用同chown,級聯更改。

值得提一下的是,在linux系統中,默認一個目錄的權限爲 755,而一個文件的默認權限爲644.

[root@localhost ~]# ls -ld test
drwxr-xr-x 2 user1 testgroup 4096 5月  10 09:00 test
[root@localhost ~]# ls -l test
總用量 0
-rw-r--r-- 1 user1 testgroup 0 5月  10 09:00 test2
[root@localhost ~]# chmod 750 test
[root@localhost ~]# ls -ld test
drwxr-x--- 2 user1 testgroup 4096 5月  10 09:00 test
[root@localhost ~]# ls -l test/test2
-rw-r--r-- 1 user1 testgroup 0 5月  10 09:00 test/test2
[root@localhost ~]# chmod 700 test/test2
[root@localhost ~]# chmod -R 700 test
[root@localhost ~]# ls -ld test
drwx------ 2 user1 testgroup 4096 5月  10 09:00 test
[root@localhost ~]# ls -l test
總用量 0
-rwx------ 1 user1 testgroup 0 5月  10 09:00 test2

如果您創建了一個目錄,而該目錄不想讓其他人看到內容,則只需設置成 ‘rwxr—–’ (740) 即可。’chmod’ 還支持使用rwx的方式來設置權限。從之前的介紹中我們可以發現,基本上就九個屬性分別是(1)user (2)group (3)others, 我們可以使用u, g, o 來代表它們三個的屬性,此外, a 則代表 all 亦即全部。阿銘舉例來介紹他們的用法:

[root@localhost ~]# chmod u=rwx,og=rx test/test2
[root@localhost ~]# ls -l test/test2
-rwxr-xr-x 1 user1 testgroup 0 5月  10 09:00 test/test2

這樣可以把 ‘test/test2’ 文件權限修改爲 ‘rwxr-xr-x’. 另外還可以針對u, g, o, a增加或者減少某個權限(讀,寫,執行),例如:

[root@localhost ~]# chmod u-x test/test2
[root@localhost ~]# ls -l test
總用量 0
-rw-r-xr-x 1 user1 testgroup 0 5月  10 09:00 test2
[root@localhost ~]# chmod a-x test/test2
[root@localhost ~]# ls -l test/test2
-rw-r--r-- 1 user1 testgroup 0 5月  10 09:00 test/test2
[root@localhost ~]# chmod u+x test/test2
[root@localhost ~]# ls -l test/test2
-rwxr--r-- 1 user1 testgroup 0 5月  10 09:00 test/test2

命令: umask

上邊也提到了默認情況下,目錄權限值爲755, 普通文件權限值爲644, 那麼這個值是由誰規定呢?追究其原因就涉及到了 ‘umask’.

umask語法: umask  xxx (這裏的xxx代表三個數字)

查看umask值只要輸入 ‘umask’ 然後回車。

[root@localhost ~]# umask
0022

umask預設是0022,其代表什麼含義?先看一下下面的規則:

1)若用戶建立爲普通文件,則預設 ‘沒有可執行權限’, 只有’rw’兩個權限。最大爲666 (‘-rw-rw-rw-‘).

2)若用戶建立爲目錄,則預設所有權限均開放,即777 (‘drwxrwxrwx’).

umask數值代表的含義爲,上邊兩條規則中的默認值(文件爲666,目錄爲777)需要減掉的權限。所以目錄的權限爲 'rwxrwxrwx' - '----w--w-' = 'rwxr-xr-x',普通文件的權限爲 'rw-rw-rw-' - '----w--w-' = 'rw-r--r--'. umask的值是可以自定義的,比如設定umask 爲 002,您再創建目錄或者文件時,默認權限分別爲'rwxrwxrwx' - '-------w-' = 'rwxrwxr-x' 和 'rw-rw-rw-' - '-------w-' = 'rw-rw-r--'.

[root@localhost ~]# umask 002
[root@localhost ~]# mkdir test2
[root@localhost ~]# ls -ld test2
drwxrwxr-x 2 root root 4096 5月  10 09:44 test2
[root@localhost ~]# touch test3
[root@localhost ~]# ls -l test3
-rw-rw-r-- 1 root root 0 5月  10 09:45 test3

可以看到創建的目錄權限默認變爲775, 而文件默認權限變爲664. 然後再把umask改回來。

[root@localhost ~]# umask 022
[root@localhost ~]# touch test4
[root@localhost ~]# ls -l test4
-rw-r--r-- 1 root root 0 5月  10 09:45 test4

umask 可以在 /etc/bashrc 裏面更改,預設情況下,root的umask爲022,而一般使用者則爲002,因爲可寫的權限非常重要,因此預設會去掉寫權限。

  1. 修改文件的特殊屬性

命令 : chattr

語法: chattr  [+-=][ASaci [文件或者目錄名]

‘+-=’ : 分別爲增加、減少、設定

‘A’ : 增加該屬性後,文件或目錄的atime將不可被修改;

‘S’ : 增加該屬性後,會將數據同步寫入磁盤中;

‘a’ : 增加該屬性後,只能追加不能刪除,非root用戶不能設定該屬性;

‘c’ : 自動壓縮該文件,讀取時會自動解壓;

‘i’ : 增加後,使文件不能被刪除、重命名、設定鏈接接、寫入、新增數據;

[root@localhost ~]# chattr +i test2
[root@localhost ~]# touch test2/test1
touch: 無法創建'test2/test1': 權限不夠
[root@localhost ~]# chattr -i test2
[root@localhost ~]# touch test2/test1
[root@localhost ~]# chattr +i test2
[root@localhost ~]# rm -f test2/test1
rm: 無法刪除'test2/test1': 權限不夠

對 ‘test2’ 目錄增加 ‘i’ 權限後,即使是root賬戶也不能在 ‘test2’ 裏創建或刪除test1文件。

[root@localhost ~]# chattr -i test2
[root@localhost ~]# touch test2/test3
[root@localhost ~]# ls test2
test1  test3
[root@localhost ~]# chattr +a test2
[root@localhost ~]# rm -f test2/test1
rm: 無法刪除 'test2/test1': 不允許的操作
[root@localhost ~]# touch test2/test4
[root@localhost ~]# ls test2
test1  test3  test4

test2目錄增加 ‘a’ 權限後,只可以在裏面創建文件,而不能刪除文件。文件同樣可以適用這些權限。

[root@localhost ~]# chattr +a test2/test1
[root@localhost ~]# echo '11111' > test2/test1
-bash: test2/test1: 不允許的操作
[root@localhost ~]# echo '11111' >> test2/test1
[root@localhost ~]# cat test2/test1
11111
[root@localhost ~]# chattr +i test2/test3
[root@localhost ~]# echo '11111' >> test2/test3
-bash: test2/test3: 權限不夠
[root@localhost ~]# echo '11111' > test2/test3
-bash: test2/test3: 權限不夠
[root@localhost ~]# rm -f test2/test3
rm: 無法刪除'test2/test3': 權限不夠

命令 : lsattr

該命令用來讀取文件或者目錄的特殊權限,語法爲 lsattr  [-aR] [文件/目錄名]

‘-a’ : 類似與ls 的-a 選項,即連同隱藏文件一同列出;

‘-R’ : 連同子目錄的數據一同列出

[root@localhost ~]# lsattr test2
-----a-------e- test2/test1
----i--------e- test2/test3
-------------e- test2/test4
[root@localhost ~]# lsattr -aR test2
----i--------e- test2/.
-----a-------e- test2/test1
-------------e- test2/..
----i--------e- test2/test3
-------------e- test2/test4
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章