Linux基礎(day8)

2.14 文件和目錄權限chmod

文件屬性

[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

  • -表示文件的類型,rw-r--r--後面的九位,表示文件的權限
    • r (read)表示可讀權限 --數字4表示,r=4
    • w (write)表示可寫權限 --數字2表示,w=2
    • x (excute)表示可執行權限 --數字1表示,x=1
    • 總結:rwx=7 rw-=6 --x=1 rw-r--r--=644 rw-r-xr-x=655
  • rw-表示第一段,user全部者的權限
  • r--表示第二段,group所屬組的權限
  • r--表示第三段,others其餘用戶權限
  • 【點.】 有的文件有點,有的沒有,意味這個文件受制於SELinux,若是selinux開啓,建立的文件或目錄在這個位置就會有點
  • 數字1,則表示 相同inode的文件數,與目錄下子目錄數有關
  • root(第一個),表示文件所屬主 ,文件全部者
  • root(第二個),表示文件所屬組
  • 0(數字),表示文件大小
  • 25 16:06(時間),表示文件最後一次修改的時間
  • 1.txt,表示文件 (這裏能夠是目錄或文件)

chmod

  • chmod等於change mode
  • chmod命令,用於改變用戶對文件或目錄的讀寫執權限
  • chmod -R 表示能夠批量更改目錄自己以及目錄下的子目錄和文件的權限
[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------. 最後的這個點纔會消失

chmod例子

[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 表示user
  • g 表示group
  • o 表示others
  • a 表示all(所有)
    • 如: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算法

2.15 更改全部者和所屬組chown

chown命令

chown介紹和例子

  • chown等於change owner 更改文件的全部者和所屬組
[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

chown的用法

  • chown -R username:group filename
  • chown 【-R】用戶名 文件名 /更改所屬主
  • chown 【-R】 用戶名:組名 文件名 /更改所屬主和所屬組
[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/
  • chown 【-R】 :組名 文件名 / 只更改所屬組
[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

chgrp

  • chgrp等於change group 更改文件所屬的用戶組
[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
  • chgrp 【-R】 組名 文件名

-R 只用於目錄,做用是級聯更改子目錄以及子文件。bash

2.16 umask

umask命令介紹

  • umask命令,經過這個值能夠肯定文件和目錄的默認權限是什麼。
  • 默認狀況下,目錄的權限值爲755(rwxr-xr-x),普通文件的默認權限爲644(-rw-r--r--),umask默認值爲0022(----w--w-)

例子對比

[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
  • 因此在建立目錄或者文件的時候,文件或目錄的權限是經過:
    • 在建立的目錄或者文件的權限=默認值(文件爲666 rw-rw-rw,目錄爲777 rwxrwxrwx)-umask的值 得來的。

規則:

  • 若用戶建立普通文件。則預設沒有可執行權限,只有rw兩個權限,最大值爲666(-rw-rw-rw)
  • 若用戶創建目錄,則預設開放全部權限,最大值777(rwxrwxrwx)

umask算法

當umask=003
目錄的權限:777(rwxrwxrwx)-003(-------wx)=774(rwxrrxr--)
普通文件的權限:666(rw-rw-rw-)-003(-------wx)=664(rw-rw-r--)
--x減去--w依然是什麼都沒有

2.17 隱藏權限lsattr/chattr

chattr介紹

  • chattr等於change attribute 附加權限
[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": 權限不夠

chattr用法

  • chattr命令,它是一個很是嚴謹的權限
  • chattr +i 則爲文件增長了隱藏屬性
    • 如果有一個文件使它沒法追加更改,刪除,編輯,則就可使用chattr +i屬性
  • 若想去除隱藏熟悉,則chattr -i
[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

    • chattr -a 能夠去除該屬性
    [root@localhost ~]# lsattr 3.txt
    -----a---------- 3.txt
    [root@localhost ~]# chattr -a 3.txt    會看到a屬性去除了
    [root@localhost ~]# lsattr 3.txt
    ---------------- 3.txt

chattr總結

  • chattr 【+-=】 【Asaci】 文件或目錄名

+、-、=分別表示增長、刪除、設定 給目錄加特殊權限,目錄下的文件的文件內容是能夠更改的,這個權限只是做用於目錄自己。code

  • A:增長該屬性後,表示文件或目錄的atime將不可更改。 (小)s:增長該屬性後,會將數據同步寫入磁盤中。
  • a:增長該屬性後,表示只能追加不能刪除,非root用戶不能設定該屬性。
  • c:增長該屬性後,表示自動壓縮該文件,讀取時自動解壓。
  • i:增長該屬性後,表示文件不能刪除、重命名、設定連接、寫入以及新增數據等。

lsatter介紹

  • lsattr等於list attribute 用於查看文件或目錄的特殊屬性

lsatter用法

  • lsattr -d 查看目錄的屬性orm

    • 給目錄加上+i權限後,是和文件添加+i屬性同樣的效果,不能更更名字,不能寫入內容,不能建立子目錄,也不能刪除
    • 給目錄加了一個+a權限或+i權限,能更改目錄裏面文件的內容
  • 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

lsattr總結

  • lsattr 【-aRd】 文件名或目錄名
    • -a:相似於ls的-a選項,連同隱藏文件一同列出

    • -R:連通子目錄子文件的數據一同列出

    • -d:查看目錄自己的特殊權限

相關文章
相關標籤/搜索