CentOS系統管理

來源:互聯網
上載者:User

   CentOS系統管理_基本許可權和歸屬的詳解

         Linux系統管理_基本許可權和歸屬-Redhat Enterprise 5

  檔案和目錄在linux系統中是最為重要的,經常使用root使用者登入系統可能沒感覺,一旦使用普通使用者的時候,就會發現許可權這個很棘手的問題,最近一段時間在學習關於檔案和目錄的許可權,想了一下,可以從這四個方面來總結一下:

  一基本許可權和歸屬關係

  二,檔案和目錄的許可權

  三,許可權的設定:chmod,umask,mkdir -m

  四,檔案和目錄的所有者和所屬組:chown,chgrp

  擴充:

  Linux系統管理_附加控制許可權:

  Linux系統管理_使用者和使用者組:

  Linux系統管理_ACL存取控制:

  一 :基本許可權和歸屬關係

  1,存取權限:

  -讀取:允許查看內容-read

  -寫入:允許修改內容-write

  -可執行:允許運行和切換-excute

  註:可執行許可權對於目錄來說,對應的位置有x許可權,意為是否可進入該目錄;

  而對於檔案來說,有x許可權,意為該檔案可執行,如程式(命令)的所有者許可權中都有x許可權。

  2,歸屬關係:

  -屬主:擁有此檔案或目錄的使用者-user

  -屬組:擁有此檔案或目錄的組-group

  -其他使用者:除屬主、屬組以外的使用者-other

  最終許可權:存取權限和歸屬關係共同決定最終許可權

  二:檔案和目錄的許可權

  [root@localhost/]# ll -d /etc/passwd /boot/

  drwxr-xr-x4rootroot1024 2013-07-10 /boot/ //目錄

  -rw-r--r--1rootroot 1681 02-17 10:23 /etc/passwd //檔案

  1 2 3 4 5 6 7 8

  第一段:d代表該目標為目錄,-代表該目標位檔案

  第二段:rwxr-xr-x :檔案和目錄的許可權位

  註:一共九位,前三位為user(所有者)的許可權,中間三位為group(所屬組)的許可權,最後三位為other(其他使用者)的許可權。

  其中r用數字標示為4,w為2,x為1

  第三段:對於檔案來說,為永久連結數;

  對於目錄來說,為該目錄下有多少個目錄,其中包括隱藏目錄“.”和“..”。

  第四段:為屬主,即檔案或目錄的所有者

  第五段:為所屬組

  第六段:檔案的大小,預設情況下單位為bit(位元組)

  第七段:為最後修改的時間

  第八段:檔案或目錄的名稱

  三:設定基本許可權:chmod、umask和mkdir -m

  1,chmod命令

  -格式:chmod [ugoa] [+ - =][rwx] 檔案/目錄

  chmod [nnn] 檔案/目錄(n代表許可權的數字形式)

  常用選項:-R :遞迴更改許可權

  - -reference=:以指定檔案或目錄做模板(這個不重要)

  樣本:

  1,修改Desktop的相關屬性,分別使用字元許可權和數字許可權進行設定

  [root@localhost ~]#ll -d Desktop/

  drwxr-xr-x 3 rootroot 4096 02-16 03:40 Desktop/

  [root@localhost ~]#chmod g+w,o-rx Desktop/

  [root@localhost ~]#ll -d Desktop/

  drwxrwx--- 3 rootroot 4096 02-16 03:40 Desktop/

  [root@localhost ~]#chmod 755 Desktop/

  [root@localhost ~]#ll -d Desktop/

  drwxr-xr-x 3 rootroot 4096 02-16 03:40 Desktop/

  2,建立一個可執行檔,並賦予所有者x許可權

  [root@localhost ~]#echo "echo Hello World" > test.sh

  [root@localhost ~]#ll -lh test.sh

  -rw-r--r-- 1 rootroot 17 02-18 21:12 test.sh

  [root@localhost ~]# chmod +x test.sh //+x預設為所有者添加該許可權

  [root@localhost ~]#ll -lh test.sh

  -rwxr-xr-x 1 rootroot 17 02-18 21:12 test.sh

  [root@localhost ~]#./test.sh

  Hello World

  [root@localhost ~]#

  2,umask命令:建立檔案或目錄的預設許可權

  -一般檔案預設不給x執行許可權

  -其他取決於umask設定

  -umask值可以進行設定(為臨時,umask 0027即講umask值設定為0027,可使用umask查看)

  注1:由於檔案預設不給x許可權,所以建立一個新檔案的最大許可權為666,建立一個目錄的最大許可權為777。

  注2: umask預設值為022(- - -- w-- w -),也就是說:

  建立一個檔案時預設許可權為:

  為rw - rw - rw - 和- - -- w --w - 的差,即為rw - r - - r - -;即為644(註:不能用777或666減去022)

  建立一個目錄時預設許可權為:

  為rwx rwx rwx 和- - -- w --w - 的差,即為rwx r - x r - x;即為755

  樣本:

  [root@localhost ~]# umask

  0022

  [root@localhost ~]# mkdir mulu1

  [root@localhost ~]# touch file1.txt

  [root@localhost ~]# ll -d mulu1/ file1.txt

  -rw-r--r--1 root root 0 02-18 21:22 file1.txt //預設檔案許可權為644

  drwxr-xr-x2 root root 4096 02-18 21:21 mulu1/ //預設目錄許可權為755

  [root@localhost ~]# umask 0027 //將umask值設定為0027

  [root@localhost ~]# umask

  0027 //修改之後umask值為0027

  [root@localhost ~]# mkdir mulu2 //修改umask值後再次建立目錄

  [root@localhost ~]# touch file2.txt //修改umask值後再次建立檔案

  [root@localhost ~]# ll -d mulu2/ file2.txt

  -rw-r-----1 root root 0 02-18 21:28 file2.txt

  drwxr-x---2 root root 4096 02-18 21:28 mulu2/

  [root@localhost ~]#

  可以看到umask值設定為0027之後,那麼建立的目錄和檔案的許可權方面other使用者將不再擁有任何許可權。

  3,mkdir -m

  mkdir為建立一個目錄,-m參數可以直接指定即將建立目錄的許可權

  mkdir

  四,檔案和目錄的所有者和所屬組:chown,chgrp

  1,chown:設定檔案或目錄的歸屬關係

  -格式:chown 屬主 檔案或目錄 //修改檔案或目錄的所有者

  chown :屬組 檔案或目錄 //修改檔案或目錄的所屬組

  chown 屬主:屬組 檔案或目錄 //修改檔案或目錄的所有者和所屬組

  -R選項:遞迴修改許可權

  - -reference選項:以指定目錄或檔案作為模板(作為瞭解)

  樣本:

  首先修改file1.txt的許可權

  然後以file1.txt為模板修改file2.txt檔案的許可權所有者和所屬使用者組。

  [root@localhost ~]# touch file1.txt

  [root@localhost ~]# touch file2.txt

  [root@localhost ~]# ll file*

  -rw-r--r-- 1 rootroot 0 02-18 21:43 file1.txt

  -rw-r--r-- 1 rootroot 0 02-18 21:43 file2.txt

  [root@localhost ~]# useradd user1

  [root@localhost ~]# chown user1:user1 file1.txt //修改file1.txt所有者為user1

  //所屬組為user1

  [root@localhost ~]# ll file*

  -rw-r--r-- 1 user1user1 0 02-18 21:43 file1.txt

  -rw-r--r-- 1root root 0 02-18 21:43 file2.txt

  [root@localhost ~]# chown --reference file1.txt file2.txt //file2.txt將會複製file1.txt的屬性

  [root@localhost ~]# ll file*

  -rw-r--r--1 user1 user1 0 02-18 21:43 file1.txt

  -rw-r--r--1 user1 user1 0 02-18 21:43 file2.txt //所有者和所屬組為和

  //file1.txt相同

  2,chgrp:設定檔案或目錄的所屬組

  chgrp 屬組 檔案或目錄 :修改檔案或目錄為的所屬組

  註:相當於chown :屬組檔案或目錄

  [root@localhost ~]# ll file*

  -rw-r--r--1 user1 user1 0 02-18 21:43 file1.txt

  -rw-r--r--1 user1 user1 0 02-18 21:43 file2.txt

  [root@localhost ~]# chgrp root file1.txt file2.txt //修改file1和file2的屬主

  [root@localhost ~]# ll file*

  -rw-r--r--1 user1 root 0 02-18 21:43 file1.txt //屬主變為root

  -rw-r--r--1 user1 root 0 02-18 21:43 file2.txt //屬主變為了root

  [root@localhost ~]#

  總結:

  chmod,chown,chgrp這三個命令雖然參數很少,但是總是容易搞混,不過用的多了,用的熟練了就能記住了,chmod修改的是許可權,chown修改的是所屬使用者和組,chgrp修改的是所屬組。

  最需要注意的是umask的值的設定,建立檔案和檔案夾時的預設許可權!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.