Linux第二周學習筆記(8)
2.14 文件或目錄權限chmod
(1). 權限表示
[root@daizhihong01 ~]# ls -l
總用量 12
-rw-------. 1 root root 1418 1月 22 08:19 anaconda-ks.cfg
-rw-r--r-- 1 root root 4358 1月 29 23:24 anaconda-ks.cfg.1
文件的權限是和所有者、所屬組有關系的,一個文件總共有三個權限位總共有9位分成三段,加上第一位表示設備類型總共十位,一下進行解析:
-rw-r--r--
第一位“-”:表示設備類型
d:表示目錄
-:表示普通文件(包含文本文檔及帶色標記的二進制文檔)
c: 表示字符串的設備(如:鼠標、鍵盤)
l:軟件連接或者硬連接文件(軟連接相當於一個快捷方式)
s:通訊文件(如:進程相互間的通訊)
rw-r--r--(從第二位開始表示權限)
w:是否可寫(-不可寫);r:是否可讀(-不可讀);x:是否可執行;
-:不可執行。
第一組rw-:表示所有者(root),文件是誰所有。
rw-表示:r可讀,w可寫,-不可執行。
第二組-r--:表示所屬組(root),文件是屬於那一個用戶組。
r--表示:r可讀,-不可寫,-不可執行。
第三組r--:表示其它用戶,除了所有者、所有組以外的其它用戶
r--表示:r可讀,-不可寫,-不可執行。
權限使用數字表示:
r=4 w=2 x=1
rwx=7(三個數字相加之和)表示可讀可寫可執行
rw-=6可讀可寫不可執行
--x=1只可執行
rw-r--r--=664
rw-r-xr-x=655
-rw-------.:在這權限當中最後一位有一個“.”是代表什麽意思,意味著這個文件受制於seLinux,如果seLinux開啟的話創建的目錄最後一位就會有一個“.”。可使用getenforce命令暫時關閉,如果要完全關閉的話需要更改配置文件vi /etc/seLinux/config
-----------------------------------------------------------------------------------------
"/etc/selinux/config" 14L, 547C
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing(更改為disabled即可)這節課這裏只做了解,不做任何更改
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
"/etc/selinux/config" 36L, 1133C
-----------------------------------------------------------------------------------------
(2.).chmod命令:chmod命令用來變更文件或目錄的權限。在UNIX系統家族裏,文件或目錄權限的控制分別以讀取、寫入、執行3種一般權限來區分,另有3種特殊權限可供運用。用戶可以使用chmod指令去變更文件與目錄的權限,設置方式采用文字或數字代號皆可。符號連接的權限無法變更,如果用戶對符號連接修改權限,其改變會作用在被連接的原始文件。
chmod-R命令:chmod命令只是對文件或者目錄生效的僅僅只是指定的這個目錄本身,如果要把目錄下面的子目錄還有子文件全部一次性批量更改的權限,就要使用-R參數
---------------------------------------------------------------------------------------
chmod命令更改權限:新建123.txt文件進行權限更改,權限更改為:所有者可讀、可寫、可執行,用戶組不可讀、不可寫、不可執行,其它用戶不可讀、不可寫、不可執行;權限表示如下:rwx------=700
[root@daizhihong01 ~]# touch 123.txt
[root@daizhihong01 ~]# ls
123.txt anaconda-ks.cfg anaconda-ks.cfg.1
[root@daizhihong01 ~]# ls -l
總用量 12
-rw-r--r-- 1 root root 0 1月 30 20:53 123.txt
-rw-------. 1 root root 1418 1月 22 08:19 anaconda-ks.cfg
-rw-r--r-- 1 root root 4358 1月 29 23:24 anaconda-ks.cfg.1
[root@daizhihong01 ~]# chmod 700 123.txt
[root@daizhihong01 ~]# ls -l
總用量 12
-rwx------ 1 root root 0 1月 30 20:53 123.txt
-rw-------. 1 root root 1418 1月 22 08:19 anaconda-ks.cfg
-rw-r--r-- 1 root root 4358 1月 29 23:24 anaconda-ks.cfg.1
更改daizhihong3權限表示如下:drwxr-xr-x
u (User):即文件或目錄的擁有者;
g( Group):即文件或目錄的所屬群組;
o (Other):除了文件或目錄擁有者或所屬群組之外,其他用戶皆屬於這個範圍;
[root@daizhihong01 tmp]# ls -l
總用量 8
-rw-r--r-- 1 root root 2564 1月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1月 29 13:36 daizhihong
drwxr-x--x 3 root root 24 1月 28 21:58 daizhihong3
drwxr-xr-x 5 root root 53 1月 29 11:43 daizhong
-rw-------. 1 root root 0 1月 22 08:14 yum.log
[root@daizhihong01 tmp]# chmod u=wxr,g=xr,o=rx daizhihong3(在錄入的時候是不能加“-”)
[root@daizhihong01 tmp]# ls -l
總用量 8
-rw-r--r-- 1 root root 2564 1月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1月 29 13:36 daizhihong
drwxr-xr-x 3 root root 24 1月 28 21:58 daizhihong3
drwxr-xr-x 5 root root 53 1月 29 11:43 daizhong
使用chmod三組權限全部加權限:
[root@daizhihong01 tmp]# ls -l
總用量 8
-rw-r--r-- 1 root root 2564 1月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1月 29 13:36 daizhihong
d-wx--x--- 3 root root 24 1月 28 21:58 daizhihong3
drwxr-xr-x 5 root root 53 1月 29 11:43 daizhong
[root@daizhihong01 tmp]# chmod a+r daizhihong3(三組權限全部加r,a代表全部)
[root@daizhihong01 tmp]# ls -l
總用量 8
-rw-r--r-- 1 root root 2564 1月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1月 29 13:36 daizhihong
drwxr-xr-- 3 root root 24 1月 28 21:58 daizhihong3(全部都加上了r)
drwxr-xr-x 5 root root 53 1月 29 11:43 daizhong
使用chmod三組權限全部減權限:
[root@daizhihong01 tmp]# ls -l
總用量 8
-rw-r--r-- 1 root root 2564 1月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1月 29 13:36 daizhihong
drwxr-xr-- 3 root root 24 1月 28 21:58 daizhihong3
drwxr-xr-x 5 root root 53 1月 29 11:43 daizhong
[root@daizhihong01 tmp]# chmod a-r daizhihong3(三組權限全部減r,a代表全部)
[root@daizhihong01 tmp]# ls -l
總用量 8
-rw-r--r-- 1 root root 2564 1月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1月 29 13:36 daizhihong
d-wx--x--- 3 root root 24 1月 28 21:58 daizhihong3(三組權限全部減r)
drwxr-xr-x 5 root root 53 1月 29 11:43 daizhong
也可以這樣使用:
chmod u+r daizhihong3
chmod u-r daizhihong3
chmod g+r daizhihong3
chmod g-r daizhihong3
chmod o+r daizhihong3
chmod o-r daizhihong3
--------------------------------------------------------------------------------
在前面講密鑰認證的時候,在做密鑰認證的時候就用到過如下:
[root@daizhihong01 ~]# chmod 700 /root/.ssh
[root@daizhihong01 ~]# ls -ld /root/.ssh/
drwx------. 2 root root 48 1月 26 04:29 /root/.ssh/
--------------------------------------------------------------------------------------------
chmod-R命令: chmod命令只是對文件或者目錄生效的僅僅只是指定的這個目錄本身,如果要把目錄下面的子目錄還有子文件全部一次性批量更改的權限,就要使用-R參數
未加R參數:
[root@daizhihong01 tmp]# ls -l
總用量 8
-rw-r--r-- 1 root root 2564 1月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1月 29 13:36 daizhihong
drwxr-xr-x 3 root root 24 1月 28 21:58 daizhihong3
drwxr-xr-x 5 root root 53 1月 29 11:43 daizhong
[root@daizhihong01 tmp]# ls -l daizhihong3
總用量 0
[root@daizhihong01 tmp]# chmod 770 daizhihong3(更改daizhihong3權限成770rwxrwx---)
[root@daizhihong01 tmp]# ls -l
總用量 8
-rw-r--r-- 1 root root 2564 1月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1月 29 13:36 daizhihong
drwxrwx--- 3 root root 24 1月 28 21:58 daizhihong3(更改成功)
drwxr-xr-x 5 root root 53 1月 29 11:43 daizhong
[root@daizhihong01 tmp]# ls -l daizhihong3
總用量 0
drwxr-xr-x 2 root root 20 1月 28 21:58 daizhihong(但是在daizhihong3下面的子目錄權限並未更改)
加R參數:
[root@daizhihong01 tmp]# chmod -R 770 daizhihong3
[root@daizhihong01 tmp]# ls -l
總用量 8
-rw-r--r-- 1 root root 2564 1月 29 23:22 anaconda-ks.cfg.1
drwxr-xr-x 2 root root 20 1月 29 13:36 daizhihong
drwxrwx--- 3 root root 24 1月 28 21:58 daizhihong3
drwxr-xr-x 5 root root 53 1月 29 11:43 daizhong
[root@daizhihong01 tmp]# ls -l daizhihong3
總用量 0
drwxrwx--- 2 root root 20 1月 28 21:58 daizhihong(加入-R參數後連著子目錄或者子文件的權限一起更改)
--------------------------------------------------------------------------------------
Linux第二周學習筆記(8)