權限命令chmod、全部者所屬組chown、缺省權限umask、隱藏權限lsattr/chattr

9月14日任務

 

2.14 文件和目錄權限chmodlinux

2.15 更改全部者和所屬組chown算法

2.16 umaskspa

2.17 隱藏權限lsattr/chattr遞歸

 

2.14 /文件和目錄權限chmod文檔

[root@localhost ~]# ll
總用量 8
-rwxr--r--. 1 root root  846 9月  13 10:21 2.txt
-rw-r--r--. 1 root root    0 9月  13 10:24 4.txt
-rw-------. 1 root root 1257 9月   5 20:11 anaconda-ks.cfg.1

 -rwxr-xr--    #三個部分分別表示全部者、所屬組、其餘人權限,r   w   x分別表示讀、寫、執行權限,用數字表示權限爲:r=四、w=二、x=1 ,所以用數字表示爲754
 

[root@localhost ~]# chmod 700 2.txt      #修改文件2.txt權限爲700
[root@localhost ~]# ll
總用量 8
-rwx------. 1 root root  846 9月  13 10:21 2.txt
-rw-r--r--. 1 root root    0 9月  13 10:24 4.txt
-rw-------. 1 root root 1257 9月   5 20:11 anaconda-ks.cfg.1

[root@localhost 123]# ll -d /tmp/123
drwxr-xr-x. 3 root root 37 9月  14 09:33 /tmp/123
[root@localhost 123]# ls -l 
總用量 0
-rw-r--r--. 1 root root 0 9月  14 09:33 111.txt
drwxr-xr-x. 2 root root 6 9月  13 09:48 zgxlinux
[root@localhost 123]# chmod -R 700 /tmp/123/        #-R表示遞歸修改,更改目錄權限的同時,該目錄下全部文件的權限遞歸修改。
[root@localhost 123]# ll
總用量 0
-rwx------. 1 root root 0 9月  14 09:33 111.txt
drwx------. 2 root root 6 9月  13 09:48 zgxlinux
[root@localhost 123]# ls -l 111.txt 
-rwx------. 1 root root 0 9月  14 09:33 111.txt
[root@localhost 123]# chmod u=rwx,g=r,o=r 111.txt      #還能夠用這種方式修改權限,u表示全部者,g表示所屬組,o表示其餘用戶
[root@localhost 123]# ls -l 111.txt 
-rwxr--r--. 1 root root 0 9月  14 09:33 111.txt

[root@localhost 123]# chmod a+x 111.txt             #全部用戶添加執行權限,a表示all
[root@localhost 123]# ls -l
總用量 0
-rwxr-xr-x. 1 root root 0 9月  14 09:33 111.txt
drwx------. 2 root root 6 9月  13 09:48 zgxlinux
[root@localhost 123]# chmod a-x 111.txt             #全部用戶去掉執行權限
[root@localhost 123]# ls -l 111.txt 
-rw-r--r--. 1 root root 0 9月  14 09:33 111.txt

 

2.1五、更改全部者所屬組命令chown權限

命令:chown  更改全部者所屬組   chgrp  僅能修改所屬組

[root@zgxlinux-01 zgx]# chown user1 passwd              #更改文件passwd的全部者爲user1用戶
[root@zgxlinux-01 zgx]# ll
總用量 4
drwxrwx---. 4 root  root  28 9月   3 17:33 1
-rwxrwx---. 1 user1 root 944 9月   4 11:12 passwd
[root@zgxlinux-01 zgx]# chgrp user1 passwd              #更改文件passwd所屬組爲user1用戶
[root@zgxlinux-01 zgx]# ll
總用量 4
drwxrwx---. 4 root  root   28 9月   3 17:33 1
-rwxrwx---. 1 user1 user1 944 9月   4 11:12 passwd
[root@zgxlinux-01 zgx]# chown root:root passwd          #更改文件passwd的全部者所屬組爲root用戶 ,chown對於目錄也能夠使用—R選項,遞歸修改。
[root@zgxlinux-01 zgx]# ll
總用量 4
drwxrwx---. 4 root root  28 9月   3 17:33 1
-rwxrwx---. 1 root root 944 9月   4 11:12 passwd
[root@zgxlinux-01 zgx]# chgrp user1:user1 passwd        #更改所屬組命令chgrp 僅能更改文件所屬組
chgrp: 無效的組:"user1:user1"
[root@zgxlinux-01 zgx]# chown zhangguoxiang:user1 /tmp/zgx/passwd
[root@zgxlinux-01 zgx]# ll !$
ll /tmp/zgx/passwd
-rwxrwx---. 1 zhangguoxiang user1 944 9月   4 11:12 /tmp/zgx/passwd

 

2.16 、缺省權限umask  默認值0022查詢

root@zgxlinux-01 zgx]# umask              #查詢權限
   0022
   [root@zgxlinux-01 zgx]# umask 002      #修改默認權限爲0002
   [root@zgxlinux-01 zgx]# umask
   0002 
   [root@zgxlinux-01 zgx]# umask -S        #選項-S  以rwx形式顯示缺省權限
   u=rwx,g=rwx,o=r
   [root@zgxlinux-01 zgx]# touch 4.txt
   [root@zgxlinux-01 zgx]# ll 4.txt
   -rw-rw-r--. 1 root root 0 9月   4 17:21 4.txt
   [root@zgxlinux-01 zgx]# ll -d 123/        #目錄必須有x權限,不然目錄不能打開
   drwxrwxr-x. 2 root root 6 9月   4 17:21 123/
   [root@zgxlinux-01 zgx]# umask 003
   [root@zgxlinux-01 zgx]# touch 12.txt
   [root@zgxlinux-01 zgx]# ll 12
   -rw-rw-r--. 1 root root 0 9月   4 17:27 12.txt
   [root@zgxlinux-01 zgx]# mkdir 122
   [root@zgxlinux-01 zgx]# ll -d 122/
   drwxrwxr--. 2 root root 6 9月   4 17:28 122/

   目錄權限算法  777-umask  777-003 = (rwxrwxrwx) - (-------wx) = (rwxrwxr--) = 774

   文件權限算法  666-umask  666-003 = (rw-rw-rw-) - (-------wx) = (rw-rw-r--) = 664

 

2.17 、隱藏權限lsattr/chattrtouch

命令   chattr  設置隱藏權限   lsattr  顯示隱藏權限

A、 針對文件   選項:chattr +i 添加i權限 chattr -i 去掉i權限 

[root@zgxlinux-01 zgx]# lsattr 1.txt
----i----------- 1.txt
 #添加i權限,沒法刪除、移動、修改文檔,也不能追加內容

   #選項: chattr +a  添加權限     chattr -a  q去掉a權限

[root@zgxlinux-01 zgx]# chattr +a 2.txt
[root@zgxlinux-01 zgx]# mv 2.txt 1.txt
mv: 沒法將"2.txt" 移動至"1.txt": 不容許的操做

[root@zgxlinux-01 zgx]# chattr -a 2.txt
[root@zgxlinux-01 zgx]# mv 2.txt 1.txt
[root@zgxlinux-01 zgx]# ll
總用量 4
-rw-r--r--. 1 root root 1169 9月   5 14:37 1.txt
[root@zgxlinux-01 zgx]# tail -5 /etc/shadow >>1.txt
# 添加a權限沒法刪除、移動、修改文檔,可是能夠追加內容進去

B、針對目錄

[root@zgxlinux-01 zgx]# chattr +i 111/
[root@zgxlinux-01 zgx]# lsattr -d 111/
----i----------- 111/
[root@zgxlinux-01 zgx]# touch 111/1.txt
touch: 沒法建立"111/1.txt": 權限不夠

#給目錄添加i或者a權限以後 ,不能新建文件,但原目錄下的文檔不受影響。其餘用法跟文檔用法相同。
相關文章
相關標籤/搜索