2周第3次課 文件或目錄權限chmod 更改文件所有者和所屬組chown umask掩碼 文件特殊屬性chattr_lsattr
一、 文件或目錄權限 chmod
chmod命令用來變更文件或目錄的權限。只有文件主和超級用戶才可以便用該命令
打開終端切換到/tmp,運行ls -l
[root@centos701 tmp]# ls -l
總用量 55076
drwxr-xr-x. 3 root root 15 10月 23 21:24 1
drwxr-xr-x. 2 root root 6 10月 23 21:24 123
-rw-r--r--. 1 root root 4 10月 24 21:11 1.txt
drwxr-xr-x. 3 root root 17 10月 24 20:49 aminglinux
以上是目錄或文件的詳細屬性截圖,我們默認將 ls 出來的信息劃分為9列,其中第1列又分為11個字段
除開第1字段表示類型,11字段表示selinux狀態外,2-10字段則表示該文件或者目錄權限
r=讀取屬性 值=4
w=寫入屬性 值=2
x=執行屬性 值=1
如/tmp/1.txt該文件的權限的解讀是:屬主-可讀可寫(rw-);屬組-可讀(r--);其他-可讀(r--),數字表示為:644。很多時候我們根據需要會對某個文件或目錄進行權限的修改,這時候就用到chmod
語法
chmod (選項) (參數)
選項
-c或——changes:效果類似“-v”參數,但僅回報更改的部分;
-f或--quiet或——silent:不顯示錯誤信息;
-R或——recursive:遞歸處理,將指令目錄下的所有文件及子目錄一並處理;
-v或——verbose:顯示指令執行過程;
參數
權限模式:指定文件的權限模式;
文件:要改變權限的文件。
實例
使/tmp/1.txt具有自己可讀可寫可執行,組用戶可讀可寫,其他用戶可讀
#chmod 764 /tmp/1.txt
二、更改所有者和所屬組chown chgrp
chown命令改變某個文件或目錄的所有者和所屬的組,該命令可以向某個用戶授權,使該用戶變成指定文件的所有者或者改變文件所屬的組。用戶可以是用戶或者是用戶D,用戶組可以是組名或組id。文件名可以使由空格分開的文件列表,在文件名中可以包含通配符。
實例
更改1.txt文件的所屬主為aming
[root@centos701 tmp]# ls -l 1.txt
-rw-r--r--. 1 root root 4 10月 24 21:11 1.txt
[root@centos701 tmp]# chown aming 1.txt
[root@centos701 tmp]# ls -l 1.txt
-rw-r--r--. 1 aming root 4 10月 24 21:11 1.txt
更改1.txt文件的所屬主為user1,所屬組為aming
[root@centos701 tmp]# ls -l 1.txt
-rw-r--r--. 1 aming root 4 10月 24 21:11 1.txt
[root@centos701 tmp]# chown user1:aming 1.txt
[root@centos701 tmp]# ls -l 1.txt
-rw-r--r--. 1 user1 aming 4 10月 24 21:11 1.txt
更改1.txt文件的屬組為root
[root@centos701 tmp]# ls -l 1.txt
-rw-r--r--. 1 user1 aming 4 10月 24 21:11 1.txt
[root@centos701 tmp]# chown :root 1.txt
[root@centos701 tmp]# ls -l 1.txt
-rw-r--r--. 1 user1 root 4 10月 24 21:11 1.txt
第2個辦法
[root@centos701 tmp]# ls -l 1.txt
-rw-r--r--. 1 user1 aming 4 10月 24 21:11 1.txt
[root@centos701 tmp]# chgrp root 1.txt
[root@centos701 tmp]# ls -l 1.txt
-rw-r--r--. 1 user1 root 4 10月 24 21:11 1.txt
更改aminglinux目錄及其子目錄和文件的屬主改成user1 屬組改成aming
[root@centos701 tmp]# chown -R user1:aming /tmp/aminglinux/
[root@centos701 tmp]# ls -l /tmp/aminglinux/
總用量 0
drwxr-xr-x. 2 user1 aming 19 10月 24 20:59 ls2
[root@centos701 tmp]# ls -ld /tmp/aminglinux/
drwxr-xr-x. 3 user1 aming 17 10月 24 20:49 /tmp/aminglinux/
三、umask
umask是限制新建文件權限的掩碼,也就是說它可以用來決定新建文件和目錄的權限。
使用方法:umask xxx 如系統默認的是 0002 在設置的時候可以暫時忽略第1個0 即: umask 003
根據目錄如果沒有x權限就無法瀏覽,文件沒有x權限就無法執行,可得目錄最大權限為777,文件最大權限為666
目錄權限=777-umask(換算成相應的rwx 逐位去減)
文件權限=666-umask
當 umask=003 時 ,計算目錄和文件的權限(註意 rwx - -wx時,其中--x=-,即沒有的位數減去有值的位數,依然等於-)
目錄權限=777-003=(rwx rwx rwx)-(--- --- --- -wx)=rwx rwx r--=774
文件權限=666-003=(rw- rw- rw-)-(--- --- -wx)=rw- rw- r--=664
操作
[root@centos701 ~]# ls -l --修改前默認文件和目錄的權限分別為 644與755
總用量 4
-rw-r--r-- 1 root root 0 10月 25 22:47 11.txt
drwxr-xr-x 2 root root 6 10月 25 22:48 123
-rw-rw-r-- 1 root root 0 10月 25 22:49 12.txt
-rw-------. 1 root root 1431 10月 24 02:55 anaconda-ks.cfg
[root@centos701 ~]# umask
0022
[root@centos701 ~]# umask 003
[root@centos701 ~]# touch 13.txt
[root@centos701 ~]# mkdir 456
[root@centos701 ~]# ls -l --修改後新建了文件13.txt和目錄456的權限分別為 664與774,改之前的文件權限保持不變
總用量 4
-rw-r--r-- 1 root root 0 10月 25 22:47 11.txt
drwxr-xr-x 2 root root 6 10月 25 22:48 123
-rw-rw-r-- 1 root root 0 10月 25 22:49 12.txt
-rw-rw-r-- 1 root root 0 10月 25 23:33 13.txt
drwxrwxr-- 2 root root 6 10月 25 23:33 456
-rw-------. 1 root root 1431 10月 24 02:55 anaconda-ks.cfg
四、隱藏權限lsattr_chattr
chattr命令用來改變文件屬性;lsattr命令用來查看文件屬性。文件和目錄屬性共有以下8種模式
a:讓文件或目錄僅供附加用途; (log日誌的重定向,防止日誌被篡改)
b:不更新文件或目錄的最後存取時間;
c:將文件或目錄壓縮後存放;
d:將文件或目錄排除在傾倒操作之外;
i:不得任意更動文件或目錄; (重要文件可以使用防止修改刪除)
s:保密性刪除文件或目錄;
S:即時更新文件或目錄;
u:預防意外刪除。
4.1.chattr
語法
chattr (選項)
-R:遞歸處理,將指令目錄下的所有文件及子目錄一並處理;
-v<版本編號>:設置文件或目錄版本;
-V:顯示指令執行過程;
+<屬性>:開啟文件或目錄的該項屬性;
-<屬性>:關閉文件或目錄的該項屬性;
=<屬性>:指定文件或目錄的該項屬性。
4.2.lsattr
語法
lsattr (選項)
-E:可顯示設備屬性的當前值,但這個當前值是從用戶設備數據庫中獲得的,而不是從設備直接獲得的。
-D:顯示屬性的名稱,屬性的默認值,描述和用戶是否可以修改屬性值的標誌。
-R:遞歸的操作方式;
-V:顯示指令的版本信息;
-a:列出目錄中的所有文件,包括隱藏文件。
-D,-E,-R這三個選項不可以一起使用,互相排斥
實例
1 chattr
[root@centos701 ~]# chattr +i 1.txt --如果需要取消則可以輸入chattr -i 1.txt
[root@centos701 ~]# vi 1.txt
編輯1.txt時會提示,你正在更改一個只讀文件,無論使用任何辦法都無法保存
2.lsattr
[root@centos701 ~]# lsattr -R --加上-R可以列出所有目錄及子目錄和文件
[root@centos701 ~]# lsattr -R
---------------- ./anaconda-ks.cfg
---------------- ./11.txt
---------------- ./123
./123:
---------------- ./12.txt
---------------- ./1234
./1234:
---------------- ./13.txt
---------------- ./456
./456:
----i----------- ./1.txt
---------------- ./111
./111:
---------------- ./111/12.txt
發現1.txt這裏有多了一個"i",其他都是-
2周第3次課 文件或目錄權限chmod 更改文件所有者和所屬組chown umask掩碼 文件特殊屬性chattr_lsattr