2.14 文件和目錄權限chmod 2.15 更改全部者和所屬組chown 2.16 umask 2.17 隱藏權限lsattr/chattr

1、chmod更改文件和目錄權限

  • 命令chmod(change mode簡寫)用於改變用戶對文件/目錄的讀寫執行權限;
    格式:chmod xyz 文件名 (xyz是數字)
[root@linux-128 tmp]# chmod 750 111
[root@linux-128 tmp]# ls -ld 111
drwxr-x--- 2 root root 6 10月 23 23:11 111
  • linux默認目錄權限最高爲777;默認文件權限最高爲666

查看目錄信息linux

[root@linux-128 tmp]# ls -ld 111
drwxr-xr-x 2 root root 6 10月 23 23:11 111

前面文件屬性的時候有講到,文件或目錄的詳細信息分9段,第1段drwxr-xr-x包含文件的類型和全部者,所屬組,其餘用戶對文件的權限;bash

  • r(read)可讀
  • w(write)可寫
  • x(executable)可執行
    r=4 w=2 x=1
    注意:目錄必須有x權限,文件沒有x權限

1. chmod //修改文件和目錄的權限

[root@linux-128 tmp]# ls -ld 111
drwxr-xr-x 2 root root 6 10月 23 23:11 111
[root@linux-128 tmp]# chmod 750 111
[root@linux-128 tmp]# ls -ld 111
drwxr-x--- 2 root root 6 10月 23 23:11 111

2. chmod –R //聯級更改,修改目錄自己及目錄下全部的子目錄文件權限

[root@linux-128 tmp]# ls -l 222/1.txt  -ld 222/123	
drwxr-xr-x 2 root root 6 10月 25 23:42 222/123
-rw-r--r-- 1 root root 0 10月 25 23:42 222/1.txt
[root@linux-128 tmp]# chmod 700 -R 222
[root@linux-128 tmp]# ls -ld 222 -l 222/1.txt  -ld 222/123
drwx------ 3 root root 30 10月 25 23:42 222
drwx------ 2 root root  6 10月 25 23:42 222/123
-rwx------ 1 root root  0 10月 25 23:42 222/1.txt

還有一種修改方法:code

3. chmod a=rwx,g=rx,o=rx 文件名

  • u(user) 全部者
  • g(group)所屬組
  • o(others)其餘用戶
[root@linux-128 tmp]# chmod u=rwx,g=rx,o=rx -R 222
[root@linux-128 tmp]# ls -ld 222 -l 222/1.txt  -ld 222/123
drwxr-xr-x 3 root root 30 10月 25 23:42 222
drwxr-xr-x 2 root root  6 10月 25 23:42 222/123
-rwxr-xr-x 1 root root  0 10月 25 23:42 222/1.txt
  • (all)所有
[root@linux-128 tmp]# chmod a-x -R 222
[root@linux-128 tmp]# ls -ld 222 -l 222/1.txt  -ld 222/123
drw-r--r-- 3 root root 30 10月 25 23:42 222
drw-r--r-- 2 root root  6 10月 25 23:42 222/123
-rw-r--r-- 1 root root  0 10月 25 23:42 222/1.txt

2、chown更改全部者和所屬組

  • chown(change ownar)命令用來修改文件的所屬主,所屬組,其餘用戶
  • chown -R 做用於目錄聯級修改,目錄自己和子目錄文件。
[root@linux ~]# useradd user1              \\建立用戶user1
[root@linux ~]# groupadd group1            \\建立組  group1
[root@linux ~]# chown user1:group1 5.txt   \\修改文件5.txt所屬主爲user1,所屬組爲group1
[root@linux ~]# ls -l 5.txt
-rw-r--r--. 1 user1 group1 78 10月 26 18:01 5.txt
chown user1 1.txt            \\修改屬主
chown user1:group1 1.txt    \\修改屬主,屬組
chown user1.group1 1.txt     \\修改屬主,屬組
chown :group1 1.txt         \\修改屬組
chown .group1 1.txt          \\修改屬組
root@linux ~]# ls -l 1.txt
-rw-r--r--. 1 root root 0 10月 26 19:44 1.txt
[root@linux ~]# chown wzzhan 1.txt
[root@linux ~]# !ls
ls -l 1.txt
-rw-r--r--. 1 wzzhan root 0 10月 26 19:44 1.txt
[root@linux ~]# chown :wzzhan 1.txt
[root@linux ~]# !ls
ls -l 1.txt
-rw-r--r--. 1 wzzhan wzzhan 0 10月 26 19:44 1.txt
[root@linux ~]# chown root:root 1.txt
[root@linux ~]# ls -l 1.txt
-rw-r--r--. 1 root root 0 10月 26 19:44 1.txt
[root@linux ~]# chown wzzhan.wzzhan 1.txt   \\chown也能夠用點來隔開
[root@linux ~]# !ls
ls -l 1.txt
-rw-r--r--. 1 wzzhan wzzhan 0 10月 26 19:44 1.txt

  • chgrp 修改文件屬組 chgrp group1 1.txt
    chgrp -R group1 111

  • !ls 表示命令歷史中以ls開頭離我問最經的一條命令
  • history 歷史命令
[root@linux ~]# history
   16  midir test
   17  mkdir test
   18  ls

!18  !後面加上歷史命令前面的序號也能夠執行。
[root@linux ~]# !18
ls
111.bak  222  4.txx  5.txt  6.txt  anaconda-ks.cfg  install.log.syslog  test
1.txt    444  4.txy  666    777    install.log      prem

3、umask命令用於改變文件的默認權限

umask XXX(3個x表示數字)
umask 022rem

[root@linux ~]# umask
0022

注意:一般只用到後面三位022 --- -w- -w-同步

  • 系統默認狀況下目錄權限值爲755,文件權限爲644,這個是有umask規定的
777  rwx rwx rwx        666  rw- rw- rw-
-022   --- -w- -w-        -022  --- -w- -w-  
=      rwx r-x r-x                      rw- r-- r--
=      7  5   5                          6   4   4

4、lsattr 查看文件的特殊屬性

  1. lsattr -d 查看目錄自己特殊屬性
[root@linux-128 tmp]# lsattr 222
---------------- 222/1.txt
---------------- 222/123

[root@linux-128 tmp]# chattr +i 222/1.txt
[root@linux-128 tmp]# chattr +a 222/
123/   1.txt  2.txt
[root@linux-128 tmp]# chattr +a 222/123/
[root@linux-128 tmp]# lsattr -d 222
-----a---------- 222
  1. lsattr -R 聯級,查看目錄下子目錄子文件的特殊屬性
[root@linux-128 tmp]# lsattr -R 222
----i----------- 222/1.txt
-----a---------- 222/123
222/123:
---------------- 222/2.txt

5、chattr 增長刪除設定特殊屬性

  • chattr +a 增長後只能追加,不能刪除或者直接更改它的內容,非root用戶不能設置該屬性
  • chattr +i 增長後不能作任何操做,不能添加,刪除,重命名,設定連接,修改屬主,屬組。
  • chattr +A 增長後文件或者目錄的atime將不可被修改
  • chattr +S 增長後會將數據同步寫入磁盤中
  1. 例子1:給文件1.txt加上i權限,而後進行刪除,修改、追加內容,修改文件屬性,全部者,所屬組。
[root@linux-128 tmp]# chattr +i 222/1.txt
[root@linux-128 tmp]# lsattr 222/1.txt
----i----------- 222/1.txt
[root@linux-128 tmp]# rm 222/1.txt
rm:是否刪除普通空文件 "222/1.txt"?y
rm: 沒法刪除"222/1.txt": 不容許的操做
[root@linux-128 tmp]# echo '11111'>222/1.txt
-bash: 222/1.txt: 權限不夠
[root@linux-128 tmp]# echo '11111'>>222/1.txt
-bash: 222/1.txt: 權限不夠
[root@linux-128 tmp]# chmod 755 222/1.txt
chmod: 更改"222/1.txt" 的權限: 不容許的操做
[root@linux-128 tmp]# chown wuzhou:wuzhou  222/1.txt
chown: 正在更改"222/1.txt" 的全部者: 不容許的操做

刪除i權限後就能正常修改了。it

[root@linux-128 tmp]# chattr -i 222/1.txt
[root@linux-128 tmp]# chown wuzhou:wuzhou  222/1.txt
[root@linux-128 tmp]# ls -l 222/1.txt
-rw-r--r-- 1 wuzhou wuzhou 0 10月 25 23:42 222/1.txt
  1. 例子2:給文件1.txt加上a權限,而後進行刪除,修改、追加內容,修改文件屬性,全部者,所屬組。
[root@linux-128 tmp]# chattr +a 222/1.txt
[root@linux-128 tmp]# lsattr 222/1.txt
-----a---------- 222/1.txt
[root@linux-128 tmp]# rm 222/1.txt
rm:是否刪除普通空文件 "222/1.txt"?y
rm: 沒法刪除"222/1.txt": 不容許的操做
[root@linux-128 tmp]# echo '1111'>222/1.txt
-bash: 222/1.txt: 不容許的操做
[root@linux-128 tmp]# echo '1111'>>222/1.txt
[root@linux-128 tmp]# cat 222/1.txt
1111
[root@linux-128 tmp]# chmod 755 222/1.txt
chmod: 更改"222/1.txt" 的權限: 不容許的操做
[root@linux-128 tmp]# mv 222/1.txt 222/2.txt
mv: 沒法將"222/1.txt" 移動至"222/2.txt": 不容許的操做
[root@linux-128 tmp]# chown root:root 222/2.txt
chown: 沒法訪問"222/2.txt": 沒有那個文件或目錄
  • 注意:1.txt加上a權限後,只能進行追加內容

  • 針對目錄來添加a,i權限
  1. 給目錄加一個i 屬性後,不能給這個目錄作任何操做,可是不妨礙已經存在的文件進行修改內容,但不能刪除這個文件。 例子:
[root@linux-128 tmp]# chattr +i 222
[root@linux-128 tmp]# touch 222/3.txt
touch: 沒法建立"222/3.txt": 權限不夠
[root@linux-128 tmp]# rm 222/1.txt
rm:是否刪除普通文件 "222/1.txt"?y
rm: 沒法刪除"222/1.txt": 權限不夠
[root@linux-128 tmp]# echo '123123'>222/1.txt
[root@linux-128 tmp]# cat 222/1.txt
123123
chattr +i   111
  1. 給目錄加一個a 屬性後,該目錄能建立文件,修改文件裏面的內容,可是不能刪除。 例子:
[root@linux-128 tmp]# chattr +a 222
[root@linux-128 tmp]# touch 222/2.txt
[root@linux-128 tmp]# ls 222
123  1.txt  2.txt
[root@linux-128 tmp]# echo '112233'>>222/2.txt
[root@linux-128 tmp]# echo '112233'>222/2.txt
[root@linux-128 tmp]# cat 222/2.txt
112233
[root@linux-128 tmp]# rm 222/2.txt
rm:是否刪除普通文件 "222/2.txt"?y
rm: 沒法刪除"222/2.txt": 不容許的操做
相關文章
相關標籤/搜索