Linux文件/目錄的權限及歸屬管理精講

1、文件的權限和歸屬概述

一、訪問權限

  • 讀取r:容許查看文件內容、顯示目錄列表;centos

  • 寫入w:容許修改文件內容,容許在目錄中新建、移動、刪除文件或子目錄;ide

  • 可執行x:容許運行程序、切換目錄

二、歸屬(全部權)

  • 屬主:擁有該文件或目錄的用戶帳號;3d

  • 屬組:擁有該文件或目錄的組帳號;

三、查看文件的權限和歸屬

Linux文件/目錄的權限及歸屬管理精講

四、chmod設置文件權限

chmod命令的基本語法格式以下:
Linux文件/目錄的權限及歸屬管理精講code

應用舉例:blog

[root@centos01 ~]# touch 1.txt     <!--建立1.txt文件-->
[root@centos01 ~]# ll 
總用量 8
-rw-r--r--  1 root root    0 1月  11 22:27 1.txt
-rw-------. 1 root root 1572 10月 23 22:37 anaconda-ks.cfg
-rw-r--r--. 1 root root 1603 10月 23 23:36 initial-setup-ks.cfg
[root@centos01 ~]# chmod u+x ./1.txt  <!--屬主用戶添加執行權限-->
[root@centos01 ~]# ll
總用量 8
-rwxr--r--  1 root root    0 1月  11 22:27 1.txt
-rw-------. 1 root root 1572 10月 23 22:37 anaconda-ks.cfg
-rw-r--r--. 1 root root 1603 10月 23 23:36 initial-setup-ks.cfg
[root@centos01 ~]# chmod u-x,g+x,o+w 1.txt   
<!--屬主用戶取消執行權限,組添加執行權限,其餘用戶添加寫入權限-->
[root@centos01 ~]# ll
總用量 8
-rw-r-xrw-  1 root root    0 1月  11 22:27 1.txt
-rw-------. 1 root root 1572 10月 23 22:37 anaconda-ks.cfg
-rw-r--r--. 1 root root 1603 10月 23 23:36 initial-setup-ks.cfg
[root@centos01 ~]# chmod 755 1.txt  <!--添加755權限(rwxr-xr-x)-->
[root@centos01 ~]# ll
總用量 8
-rwxr-xr-x  1 root root    0 1月  17 02:36 1.txt
-rw-------. 1 root root 1572 10月 23 22:37 anaconda-ks.cfg
-rw-r--r--. 1 root root 1603 10月 23 23:36 initial-setup-ks.cfg

五、chown設置文件的歸屬

chown命令的基本語法格式以下:
Linux文件/目錄的權限及歸屬管理精講it

應用舉例:class

[root@centos01 ~]# chown bob 1.txt  <!--1.txt設置屬主-->
[root@centos01 ~]# ll
總用量 8
-rwxr-xr-x  1 bob  root    0 1月  17 02:36 1.txt
-rw-------. 1 root root 1572 10月 23 22:37 anaconda-ks.cfg
-rw-r--r--. 1 root root 1603 10月 23 23:36 initial-setup-ks.cfg
[root@centos01 ~]# chown :benet 1.txt  <!--1.txt設置屬組-->
[root@centos01 ~]# ll
總用量 8
-rwxr-xr-x  1 bob  benet    0 1月  17 02:36 1.txt
-rw-------. 1 root root  1572 10月 23 22:37 anaconda-ks.cfg
-rw-r--r--. 1 root root  1603 10月 23 23:36 initial-setup-ks.cfg
[root@centos01 ~]# chown bob:benet 1.txt  <!--1.txt設置屬主和屬組-->
[root@centos01 ~]# ll
總用量 8
-rwxr-xr-x  1 bob  benet    0 1月  17 02:36 1.txt
-rw-------. 1 root root  1572 10月 23 22:37 anaconda-ks.cfg
-rw-r--r--. 1 root root  1603 10月 23 23:36 initial-setup-ks.cfg
<!---->

2、目錄的權限和歸屬

一、訪問權限

Linux文件/目錄的權限及歸屬管理精講

二、歸屬(全部權)

  • 屬主:擁有該目錄的用戶帳號;循環

  • 屬組:擁有該目錄的組帳號;

三、chmod設置目錄權限

chmod命令設置目錄權限的基本格式以下:
Linux文件/目錄的權限及歸屬管理精講權限

應用舉例:語法

[root@centos01 ~]# chmod -R 755 benet/   
          <!--循環設置benet目錄下的文件或者目錄權限爲755-->
[root@centos01 ~]# ll
總用量 8
-rw-r-xrw-  1 root root    0 1月  11 22:27 1.txt
-rw-------. 1 root root 1572 10月 23 22:37 anaconda-ks.cfg
drwxr-xr-x  3 root root   18 1月  11 22:39 benet
-rw-r--r--. 1 root root 1603 10月 23 23:36 initial-setup-ks.cfg

四、chown設置目錄的歸屬

chown命令設置目錄歸屬的基本格式以下:
Linux文件/目錄的權限及歸屬管理精講

應用舉例:

[root@centos01 ~]# chown -R bob:benet benet/   
   <!--循環設置benet目錄中所屬用戶爲bob,所屬組爲benet-->
[root@centos01 ~]# ll
總用量 8
-rw-r-xrw-  1 root root     0 1月  11 22:27 1.txt
-rw-------. 1 root root  1572 10月 23 22:37 anaconda-ks.cfg
drwxr-xr-x  3 bob  benet   18 1月  11 22:39 benet
-rw-r--r--. 1 root root  1603 10月 23 23:36 initial-setup-ks.cfg

3、權限掩碼umask

一、umask的做用

控制新建的文件或目錄的權限,默認權限去除umask的權限就是新建的文件或者目錄的權限。

二、設置umask

umask 022

三、查看umask

umask

四、應用舉例:

[root@centos01 ~]# umask  <!--查看umask-->
0022
[root@centos01 ~]# umask 000  <!--設置umask爲000-->
[root@centos01 ~]# umask   <!--驗證是否設置成功-->
0000
[root@centos01 ~]# touch 2.txt   <!--建立新文件-->
[root@centos01 ~]# ll
總用量 8
-rwxr-xr-x  1 bob  benet    0 1月  17 03:48 1.txt
-rw-rw-rw-  1 root root     0 1月  17 03:48 2.txt    <!--查看權限-->
-rw-------. 1 root root  1572 10月 23 22:37 anaconda-ks.cfg
-rw-r--r--. 1 root root  1603 10月 23 23:36 initial-setup-ks.cfg
[root@centos01 ~]# umask 022       <!--設置umask爲022-->
[root@centos01 ~]# umask           <!--查看umask-->
0022
[root@centos01 ~]# touch 3.txt        <!--再次建立新文件-->
[root@centos01 ~]# ll
總用量 8
-rwxr-xr-x  1 bob  benet    0 1月  17 03:48 1.txt
-rw-rw-rw-  1 root root     0 1月  17 03:48 2.txt
-rw-r--r--  1 root root     0 1月  17 03:49 3.txt <!--查看權限,明顯不同-->
-rw-------. 1 root root  1572 10月 23 22:37 anaconda-ks.cfg
-rw-r--r--. 1 root root  1603 10月 23 23:36 initial-setup-ks.cfg

———————— 本文至此結束,感謝閱讀 ————————

相關文章
相關標籤/搜索