[root@localhost ~]# ls -l 總用量 12 -rw-r--r--. 1 root root 0 10月 25 16:06 1.txt -rw-------. 1 root root 1422 10月 21 00:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 10月 25 16:06 1.txtnode
[root@hf-01 ~]# ls -l 總用量 8 -rw-r--r--. 1 root root 924 10月 25 06:49 2.txt -rw-------. 1 root root 973 8月 21 05:05 anaconda-ks.cfg.1 [root@hf-01 ~]# chmod 700 2.txt 更改2.txt文件權限 [root@hf-01 ~]# ls -l 2.txt -rwx------. 1 root root 924 10月 25 06:49 2.txt 這裏會發現2.txt權限改變了 [root@hf-01 ~]# getenforce 查看防火牆是否關閉 [root@hf-01 ~]# setenforce 0 臨時關閉防火牆 若想永久關閉防火牆,則須要更改配置文件 [root@hf-01 ~]# vi /etc/selinux/config 在這個文件下更改 只有關閉了selinux,-rwx------. 最後的這個點纔會消失
[root@hf-01 ~]# mkdir hf/ 新建目錄hf/ [root@hf-01 ~]# ls 2.txt anaconda-ks.cfg.1 hf [root@hf-01 ~]# cd hf/ [root@hf-01 hf]# touch 1.txt 新建文件1.txt [root@hf-01 hf]# ls 1.txt [root@hf-01 hf]# ls -l 總用量 0 -rw-r--r--. 1 root root 0 10月 26 06:56 1.txt [root@hf-01 hf]# cd [root@hf-01 ~]# chmod 770 1.txt chmod: 沒法訪問"1.txt": 沒有那個文件或目錄 這是由於1.txt在目錄hf/下面 [root@hf-01 ~]# chmod 770 hf/ 更改hf/文件夾的權限 [root@hf-01 ~]# ls -l hf/ 會發現裏面的1.txt權限沒有發生變化 總用量 0 -rw-r--r--. 1 root root 0 10月 26 06:56 1.txt [root@hf-01 ~]# ls -ld hf/ 而文件夾的權限則發生了變化 drwxrwx---. 2 root root 18 10月 26 06:56 hf/ [root@hf-01 ~]# chmod -R 661 hf/ 在加上了-R選項,文件和目錄和子目錄批量的更改了權限 [root@hf-01 ~]# ls -l hf/ 總用量 0 -rw-rw---x. 1 root root 0 10月 26 06:56 1.txt [root@hf-01 ~]# ls -ld hf/ drw-rw---x. 2 root root 18 10月 26 06:56 hf/
如:u+(-)rwx,g+(-)rwx,o+(-)rwx 若是更改多個屬性,中間可用「,」隔開。mysql
又如:a+(-)rwxlinux
[root@hf-01 ~]# chmod u=rwx,g=w,o=r hf/ 字母縮寫代替更改權限 [root@hf-01 ~]# ls -ld hf/ drwx-w-r--. 2 root root 18 10月 26 06:56 hf/ [root@hf-01 ~]# ls -l hf/ 總用量 0 -rw-rw---x. 1 root root 0 10月 26 06:56 1.txt [root@hf-01 ~]# chmod a+x hf/ 全部文件權限都加上x執行權限 [root@hf-01 ~]# ls -ld hf/ drwx-wxr-x. 2 root root 18 10月 26 06:56 hf/ [root@hf-01 ~]# chmod o+w hf/ 其餘用戶組加上w可寫的權限 [root@hf-01 ~]# ls -ld hf/ drwx-wxrwx. 2 root root 18 10月 26 06:56 hf/ [root@hf-01 ~]# chmod a-w hf/ 全部文件權限減去w可寫的權限 [root@hf-01 ~]# ls -ld hf/ dr-x--xr-x. 2 root root 18 10月 26 06:56 hf/
在Linux系統中。目錄的默認權限爲755,文件的默認權限爲644算法
[root@hf-01 ~]# ls /tmp aminglinux amning mysql.sock yum.log [root@hf-01 ~]# ls -l /tmp/yum.log 會看到yum.log的全部者是root -rw-r--r--. 1 root root 0 10月 26 07:48 /tmp/yum.log [root@hf-01 ~]# chown aming /tmp/yum.log chown: 無效的用戶: "aming" 這是由於在/etc/passwd中沒有aming這個用戶,須要useradd aming便可 [root@hf-01 ~]# chown hanfeng /tmp/yum.log 這時會看到全部者發生了變化,yum.log文件的全部者變化成hanfeng了 [root@hf-01 ~]# !ls ls -l /tmp/yum.log -rw-r--r--. 1 hanfeng root 0 10月 26 07:48 /tmp/yum.log
[root@hf-01 ~]# !ls ls -l /tmp/yum.log -rw-r--r--. 1 hanfeng user1 0 10月 26 07:48 /tmp/yum.log [root@hf-01 ~]# chown user1:hanfeng /tmp/yum.log [root@hf-01 ~]# !ls 這裏會看到所屬主和所屬組發生了改變,用戶和組中間用:隔開 ls -l /tmp/yum.log -rw-r--r--. 1 user1 hanfeng 0 10月 26 07:48 /tmp/yum.log [root@hf-01 ~]# chown -R hanfeng:user1 /tmp/aminglinux/ [root@hf-01 ~]# ls -l /tmp/aminglinux/ 總用量 0 drwxr-xr-x. 2 hanfeng user1 18 10月 24 07:21 2 drwxr-xr-x. 4 hanfeng user1 31 10月 25 06:55 aming2 [root@hf-01 ~]# ls -l /tmp/aminglinux/ 總用量 0 drwxr-xr-x. 2 hanfeng user1 18 10月 24 07:21 2 drwxr-xr-x. 4 hanfeng user1 31 10月 25 06:55 aming2 [root@hf-01 ~]# ls -ld /tmp/aminglinux/ drwxr-xr-x. 4 hanfeng user1 27 10月 25 07:29 /tmp/aminglinux/ [root@hf-01 ~]# touch /tmp/aminglinux/3.txt [root@hf-01 ~]# chown -R user1:hanfeng /tmp/aminglinux/ [root@hf-01 ~]# ls -l /tmp/aminglinux/ 總用量 0 drwxr-xr-x. 2 user1 hanfeng 18 10月 24 07:21 2 -rw-r--r--. 1 user1 hanfeng 0 10月 26 08:23 3.txt drwxr-xr-x. 4 user1 hanfeng 31 10月 25 06:55 aming2 [root@hf-01 ~]# ls -ld /tmp/aminglinux/ drwxr-xr-x. 4 user1 hanfeng 39 10月 26 08:23 /tmp/aminglinux/
[root@hf-01 ~]# chown :root /tmp/yum.log [root@hf-01 ~]# !ls 這裏只更改了它的所屬組 ls -l /tmp/yum.log -rw-r--r--. 1 user1 root 0 10月 26 07:48 /tmp/yum.log
-R 只用於目錄,做用是級聯更改子目錄以及子文件。sql
[root@hf-01 ~]# chgrp user1 /tmp/yum.log [root@hf-01 ~]# !ls 在以前的所屬組是root,如今所屬組是user1 ls -l /tmp/yum.log -rw-r--r--. 1 hanfeng user1 0 10月 26 07:48 /tmp/yum.log
-R 只用於目錄,做用是級聯更改子目錄以及子文件。bash
[root@hf-01 ~]# touch 11.txt [root@hf-01 ~]# ls -l 11.txt -rw-r--r--. 1 root root 0 10月 26 08:39 11.txt [root@hf-01 ~]# mkdir 123 [root@hf-01 ~]# ls -ld 123 drwxr-xr-x. 2 root root 6 10月 26 08:39 123 [root@hf-01 ~]# umask 這是系統root用戶的umask值0022,經過這個值就能夠確認文件的默認權限,也能夠確認目錄的默認權限是什麼 0022 [root@hf-01 ~]# umask 002 這裏更改默認權限,寫全了是0002,但通常會省去開頭的0,寫成002 [root@hf-01 ~]# touch 33.txt [root@hf-01 ~]# ls -l 33.txt 這裏和上面對比,會發現建立的文本權限發生了變化 -rw-rw-r--. 1 root root 0 10月 26 08:56 33.txt [root@hf-01 ~]# mkdir 234 [root@hf-01 ~]# ls -ld 234 這裏的文件夾權限也發生了變化 drwxrwxr-x. 2 root root 6 10月 26 08:57 234
當umask=003 目錄的權限:777(rwxrwxrwx)-003(-------wx)=774(rwxrrxr--) 普通文件的權限:666(rw-rw-rw-)-003(-------wx)=664(rw-rw-r--) --x減去--w依然是什麼都沒有
[root@hf-01 ~]# chattr +i 33.txt 給33.txt(空文件)增長了隱藏屬性 [root@hf-01 ~]# vi 33.txt 是沒法進去編輯文件,增長內容的,強制保存都不能夠,它會提示說只有可讀權限 [root@hf-01 ~]# head -n2 /etc/passwd > 33.txt -bash: 33.txt: 權限不夠 將文件寫入到33.txt文件中,也會提示權限不夠 [root@hf-01 ~]# ls -l 33.txt 但查看的時候會看到有可讀可寫的權限,這時就要想要它是否添加了隱藏屬性 -rw-rw-r--. 1 root root 0 10月 26 08:56 33.txt [root@hf-01 ~]# lsattr 33.txt 可查看33.txt添加了i隱藏屬性 ----i----------- 33.txt [root@hf-01 ~]# touch ha.txt [root@hf-01 ~]# lsattr ha.txt 查看ha.txt文件的隱藏屬性 ---------------- ha.txt Try 'mv --help' for more information. [root@hf-01 ~]# mv 33.txt 56.txt 這時修改將文件更名,會發現沒法實現 mv: 沒法將"33.txt" 移動至"56.txt": 不容許的操做 [root@hf-01 ~]# rm 33.txt 也沒法去刪除該文件,就算加-f強制去刪除,再去查看的時候,會看到文件依舊存在 rm:是否刪除普通空文件 "33.txt"?y rm: 沒法刪除"33.txt": 不容許的操做 [root@localhost ~]# touch 1.txt 會發現也沒法現更改 touch: 沒法建立"1.txt": 權限不夠
[root@localhost ~]# chattr -i 1.txt 刪除隱藏屬性 [root@localhost ~]# lsattr 1.txt 再來查看,會發現隱藏屬性沒了 ---------------- 1.txt [root@localhost ~]# mv 1.txt 3.txt 這時就能夠更更名稱了(編輯、刪除均可以) [root@localhost ~]# touch 3.txt [root@localhost ~]# echo "gurui" > 3.txt [root@localhost ~]# chattr +a 3.txt 給文件增長a屬性,會發現沒法刪除、編輯,添加內容,只能追加內容 [root@localhost ~]# rm 3.txt rm:是否刪除普通空文件 "3.txt"?y rm: 沒法刪除"3.txt": 不容許的操做 [root@localhost ~]# vi 3.txt 沒法編輯3.txt文件 [root@localhost ~]# echo "hanfeng shuaiguo" > 3.txt 沒法添加內容進去 -bash: 3.txt: 不容許的操做 [root@localhost ~]# echo "hanfeng shuaiguo" >> 3.txt 只能追加內容進3.txt文件中 [root@localhost ~]# cat 3.txt gurui hanfeng shuaiguo [root@localhost ~]# echo "hanfeng shuaiguo" >> 3.txt 繼續追加內容 [root@localhost ~]# cat 3.txt gurui hanfeng shuaiguo hanfeng shuaiguo [root@localhost ~]# touch 3.txt 能夠更改時間信息 [root@localhost ~]# ls -l 3.txt -rw-r--r--. 1 root root 40 10月 26 15:41 3.txt
chattr +a 屬性, 只能追加,能夠touch更改時間信息,但不能刪除,不能更更名字和內容ui
[root@localhost ~]# lsattr 3.txt -----a---------- 3.txt [root@localhost ~]# chattr -a 3.txt 會看到a屬性去除了 [root@localhost ~]# lsattr 3.txt ---------------- 3.txt
+、-、=分別表示增長、刪除、設定 給目錄加特殊權限,目錄下的文件的文件內容是能夠更改的,這個權限只是做用於目錄自己。code
lsattr -d 查看目錄的屬性orm
lsattr -R 會顯示目錄及子目錄下的文件(一層或多層目錄文件),如果不加-R,則僅僅顯示一層目錄文件ci
[root@localhost ~]# mkdir 111 [root@localhost ~]# lsattr 111 [root@localhost ~]# mkdir 111/222/ [root@localhost ~]# lsattr 111/ ---------------- 111/222 [root@localhost ~]# lsattr -d 111/ ---------------- 111/ [root@localhost ~]# chattr +i 111/ [root@localhost ~]# lsattr -d 111/ ----i----------- 111/ [root@localhost ~]# rm -r 111/ rm:是否進入目錄"111/"? y rm:是否刪除目錄 "111/222"?y rm: 沒法刪除"111/222": 權限不夠 [root@localhost ~]# mv 111 1212 mv: 沒法將"111" 移動至"1212": 不容許的操做 [root@localhost ~]# touch 111/12.txt touch: 沒法建立"111/12.txt": 權限不夠 [root@localhost ~]# chattr -i 111/ 給目錄去除-i屬性 [root@localhost ~]# lsattr -d 111/ ---------------- 111/ [root@localhost ~]# chattr +a 111/ 給目錄加上+a權限 [root@localhost ~]# touch 111/23.txt 只能追加一個文件(建立一個文件,也算是追加) [root@localhost ~]# head -n 2 /etc/passwd > 111/23.txt 給目錄加了 [root@localhost ~]# cat 111/23.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin [root@localhost ~]# chattr -a 111 [root@localhost ~]# chattr +i 111/ [root@localhost ~]# head -n 2 /etc/passwd > 111/23.txt [root@localhost ~]# [root@localhost ~]# lsattr -R 111/ 在加上-R會查看111/目錄下的文件和111/子目錄下的文件 ---------------- 111/222 111/222: ---------------- 111/23.txt ---------------- 111/12.txt [root@localhost ~]# lsattr 111/ 如果不加,就僅僅顯示一層的目錄文件 ---------------- 111/222 ---------------- 111/23.txt ---------------- 111/12.txt [root@localhost ~]# tree 111/ 111/ ├── 12.txt ├── 222 └── 23.txt 1 directory, 2 files
-a:相似於ls的-a選項,連同隱藏文件一同列出
-R:連通子目錄子文件的數據一同列出
-d:查看目錄自己的特殊權限