1. 程式人生 > >玩轉並理解linux中的檔案/目錄的rwx許可權

玩轉並理解linux中的檔案/目錄的rwx許可權

        linux是一個相對安全的系統, 其中的許可權更是無處不在。 在本文中, 我們來談談linux中的檔案/目錄的rwx許可權。 為了簡便起見, 我們僅僅以檔案owner的rwx為例。

        一. 檔案的rwx許可權分別是什麼意思?

         1. r許可權:可讀許可權, 驗證如下:

 [[email protected] learn_c]$ ls -l
total 0
[[email protected] learn_c]$ echo hello > a.txt
[[email protected] learn_c]$ ls -l
total 4
-rw-rw-r-- 1 taoge taoge 6 May  6 03:51 a.txt
[
[email protected]
 learn_c]$ chmod 000 a.txt 
[[email protected] learn_c]$ ls -l
total 4
---------- 1 taoge taoge 6 May  6 03:51 a.txt
[[email protected] learn_c]$ cat a.txt 
cat: a.txt: Permission denied
[[email protected] learn_c]$ chmod u+r a.txt 
[[email protected] learn_c]$ ls -l
total 4
-r-------- 1 taoge taoge 6 May  6 03:51 a.txt
[
[email protected]
 learn_c]$ cat a.txt
hello
[[email protected] learn_c]$ 

         2. w許可權: 可寫許可權, 驗證如下:

 [[email protected] learn_c]$ ls -l
total 0
[[email protected] learn_c]$ touch a.txt
[[email protected] learn_c]$ ls -l
total 0
-rw-rw-r-- 1 taoge taoge 0 May  6 03:56 a.txt
[[email protected]
 learn_c]$ chmod 000 a.txt
[[email protected] learn_c]$ ls -l
total 0
---------- 1 taoge taoge 0 May  6 03:56 a.txt
[[email protected] learn_c]$ chmod u+w a.txt
[[email protected] learn_c]$ ls -l
total 0
--w------- 1 taoge taoge 0 May  6 03:56 a.txt
[[email protected] learn_c]$ echo hello > a.txt
[[email protected] learn_c]$ cat a.txt
cat: a.txt: Permission denied
[[email protected] learn_c]$ chmod u+r a.txt
[[email protected] learn_c]$ cat a.txt
hello
[[email protected] learn_c]$ 

     3. x許可權:可執行許可權, 驗證如下:

 [[email protected] learn_c]$ ls -l
total 4
-rw-rw-r-- 1 taoge taoge 65 May  6 04:02 test.c
[[email protected] learn_c]$ cat test.c 
#include <stdio.h>

int main()
{
printf("good\n");
return 0;
}
[[email protected] learn_c]$ gcc test.c 
[[email protected] learn_c]$ ls -l
total 12
-rwxrwxr-x 1 taoge taoge 4638 May  6 04:04 a.out
-rw-rw-r-- 1 taoge taoge   65 May  6 04:02 test.c
[[email protected] learn_c]$ ./a.out 
good
[[email protected] learn_c]$ chmod 000 a.out 
[[email protected] learn_c]$ ./a.out
bash: ./a.out: Permission denied
[[email protected] learn_c]$ chmod u+x a.out 
[[email protected] learn_c]$ ./a.out 
good
[[email protected] learn_c]$ 
        

       二. 目錄的rwx許可權分別是什麼意思?

        1. r許可權:可讀許可權(可列舉檢視目錄下的內容), 驗證如下:

[[email protected] learn_c]$ ls -l
total 0  [[email protected] learn_c]$ mkdir test
[[email protected] learn_c]$ ls -l
total 4
drwxrwxr-x 2 taoge taoge 4096 May  6 04:07 test
[[email protected] learn_c]$ touch ./test/a.txt
[[email protected] learn_c]$ ls ./test/
a.txt
[[email protected] learn_c]$ chmod u-r test/
[[email protected] learn_c]$ ls ./test/
ls: cannot open directory ./test/: Permission denied
[[email protected] learn_c]$ 

      2. w許可權:可寫許可權(可以往目錄中寫東東, 比如檔案), 驗證如下:

 [[email protected] learn_c]$ ls -l
total 0
[[email protected] learn_c]$ mkdir test
[[email protected] learn_c]$ ls -l
total 4
drwxrwxr-x 2 taoge taoge 4096 May  6 04:13 test
[[email protected] learn_c]$ touch ./test/a.txt
[[email protected] learn_c]$ chmod u-w test
[[email protected] learn_c]$ touch ./test/b.txt
touch: cannot touch `./test/b.txt': Permission denied
[[email protected] learn_c]$ 

     3. x許可權: 可執行許可權(可以cd進去), 驗證如下:  [[email protected] learn_c]$ ls -l
total 0
[[email protected] learn_c]$ mkdir test
[[email protected] learn_c]$ ls -l
total 4
drwxrwxr-x 2 taoge taoge 4096 May  6 04:17 test
[[email protected] learn_c]$ cd test/
[[email protected] test]$ cd -
/home/taoge/Desktop/learn_c
[[email protected] learn_c]$ chmod u-x test/
[[email protected] learn_c]$ cd test/
bash: cd: test/: Permission denied
[[email protected] learn_c]$ 
       好,最後我們再來看一個問題:在某目錄test中建立一個檔案或者刪除一個檔案, 需要test目錄具備什麼許可權呢? 答曰:需要目錄test具備wx許可權, 驗證如下:  [[email protected] learn_c]$ ls -l
total 0
[[email protected] learn_c]$ mkdir test
[[email protected] learn_c]$ touch ./test/a.txt ./test/b.txt ./test/c.txt ./test/d.txt
[[email protected] learn_c]$ ls -l
total 4
drwxrwxr-x 2 taoge taoge 4096 May  6 04:33 test
[[email protected] learn_c]$ chmod u-r test/
[[email protected] learn_c]$ touch ./test/e.txt
[[email protected] learn_c]$ chmod u-w test/
[[email protected] learn_c]$ touch ./test/f.txt
touch: cannot touch `./test/f.txt': Permission denied
[[email protected] learn_c]$ rm ./test/a.txt
rm: cannot remove `./test/a.txt': Permission denied
[[email protected] learn_c]$ chmod u+w test/
[[email protected] learn_c]$ chmod u-x test/
[[email protected] learn_c]$ touch ./test/f.txt
touch: cannot touch `./test/f.txt': Permission denied
[[email protected] learn_c]$ rm ./test/a.txt
rm: cannot remove `./test/a.txt': Permission denied
[[email protected] learn_c]$ chmod u+x test/
[[email protected] learn_c]$ 

       因此, 如果某一目錄test刪除不掉, 很可能是因為其中有不可刪除的檔案, 從本質上來講, 就是test自己沒有wx許可權了。

       好, 本文先閒談到這裡。

相關推薦

理解linux檔案/目錄rwx許可權

        linux是一個相對安全的系統, 其中的許可權更是無處不在。 在本文中, 我們來談談linux中的檔案/目錄的rwx許可權。 為了簡便起見, 我們僅僅以檔案owner的rwx為例。         一. 檔案的rwx許可權分別是什麼意思?        

Linux檔案的隱藏許可權

  在linux中使用ls -l列出的檔案許可權有-rwxrwxrwx這9個,其實除了這9個外還有另外的一些隱藏的許可權,這些許可權需要使用lsattr這個命令才可以檢視到,而如果要修改隱藏許可權,則使用chattr這個命令來進行修改。   chattr:引數

.NeterLinux系列之二:Linux下的檔案目錄檔案目錄許可權

基礎篇 實戰篇 一、Linux下的檔案目錄 簡介:linux的檔案系統是採用級層式的樹狀目錄結構,在此 結構中的最上層是根目錄“/”,然後在此目錄下再建立 其他的目錄。深刻理解linux檔案目錄是非常重要的,如下圖所示: 將來你用哪個使用者登入,你就會在那個使用

Linux系統下python學習筆記——Linux檔案目錄常用命令詳解

一、檢視目錄內容 ls命令說明: 英文單詞list的簡寫,功能為列出目錄的內容,是使用者最常用的命令字義    Linux下檔案和目錄的特點: Linux檔案或目錄名稱最長可以有256個字元 以 . 開頭的檔案為隱藏檔案,需要用-a引數才能顯示(all

Linux替換目錄下多個檔案的字串

在Linux中,替換多個檔案中的某個字串可以使用grep和sed的組合來實現這個功能。 基本格式 grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g' matchstring是你將要被替換

Linux目錄檔案時間

Windows中目錄的檔案時間: Windows下的檔案和目錄的時間有三個:建立時間,修改時間和訪問時間 Linux中目錄的檔案時間: Linux中檔案也有三個時間:Access time、Modify time和Change time,稱為Atime,Mtime和Ctime。

Linux檔案目錄的預設許可權umask命令、查詢命令、壓縮命令

主要內容: 1.檔案和目錄的許可權 2.檔案和目錄的預設許可權和umask 3.Linux查詢及壓縮 1.檔案和目錄的許可權 許可權對於檔案: r(read):可以獲取檔案的內容 w(write):可以編輯、新增或修改檔案的內容(不包括刪除該檔案) x(e

Linux 檔案許可權目錄配置

上圖分別表示 許可權 連結 擁有者 群組 檔案大小 修改日期 檔名 許可權 第一個字元代表這個檔案是“目錄,檔案或連結檔案等等” d 表示 資料夾 - 表示 檔案 i 表示 連結檔案 b 表示 可隨機儲存裝置 c 表示 一次性讀取裝置 接下來的字元中,以三個為一組

linux檢視目錄下隱藏檔案方式?

Linux系統中,除了儲存了大量可見的檔案和資料夾,還附帶了很多隱藏的檔案和資料夾,那麼該如何進行檢視?這些隱藏檔案又有什麼用呢?檢視Linux主目錄隱藏檔案可以通過執行ls–a來實現,對於隱藏檔案,不建議進行更改和刪除操作,原因是,主目錄中的隱藏檔案和目錄包括使用者程式訪問

Linux進入目錄和顯示檔案所需要的許可權

1.在Linux下進入一個目錄需要怎樣的許可權呢? 我們先建立一個許可權為 000 的目錄檔案 進入該目錄檔案發現許可權不夠,不能進入 給該目錄檔案的所有者加上讀許可權 發現還是不能進入,提示許可權不夠 給所有者去掉讀許可權加上寫許可權,發現還是不能進入

Linux查詢目錄檔案的內容總結

查詢目錄下的所有檔案中是否含有某個字串 find .|xargs grep -ri "IBM" 查詢目錄下的所有檔案中是否含有某個字串,並且只打印出檔名 find .|xargs grep -ri "IBM" -l 1.正則表示式    (1)正則表示式一般用來描述文字模式的特殊用法,由普通字元(例如字元a-

Linux使用筆記: 設定Samba伺服器新建檔案/目錄許可權

From: http://easwy.com/blog/archives/set-file-directory-attribute-for-linux-samba/ 通過Samba伺服器實現windows和Linux之間的檔案共享,相信是絕大多數人的選擇。通常我們都會使

linux檔案描述符fd和檔案指標flip的理解

整理自:http://www.cnblogs.com/Jezze/archive/2011/12/23/2299861.html簡單歸納:fd(file descriptor)只是一個整數,在open時產生。起到一個索引的作用。每個程序在PCB(Process Control

Linux常見目錄的作用

執行 保存 配置文件 家目錄 pro 系統 配置 命令 dev bin目錄   有四個bin目錄,分別是/bin、/sbin、/usr/bin/、/usr/sbin/   用來保存系統命令,區別是 前兩個目錄下的命令所有用戶都可以執行,後兩個目錄下的命令只有超級用戶可以執

Linux目錄功能(Red Hat 7)

linux zhang linux中 文件的 程序 tmp red hat 7 /tmp hang 目錄的基本功能: /bin:存放普通用戶使用的命令 /sbin:存放管理員可以執行的命令 /home:存放普通的家目錄  如張三家目錄為/home/zhangsan /roo

Linux /boot 目錄介紹 【轉載】

cond 備份 ges 直接 modprobe 虛擬 人員 完成 linux中 Linux中 /boot 目錄介紹 轉自:點擊打開鏈接 一、/boot/目錄中的文件和目錄 Linux系統在本地啟動時,目錄/boot/非常重要,其中的文件和目錄有:

()詳解LinuxSSH遠程訪問控制

體系 字符 配置文件 art 文件 優先 class 遠程訪問 安全 詳解Linux中SSH遠程訪問控制 原文:http://blog.51cto.com/dengqi/1260038 SSH:是一種安全通道協議,主要用來實現字符界面的遠程登錄,遠程復制等功能(使用TCP的

大神教你輕松Docker和Kubernetes如何運行MongoDB微服務

cdn 成員 實現 細節 步驟 class 就會 接受 blog 本文介紹了利用Docker和Kubernetes搭建一套具有冗余備份集合的MongoDB服務,從容器對CI和CD引發的改變入手,討論了容器技術對MongoDB帶來的挑戰和機會,然後實戰如何部署一套穩定的Mo

ASP.NET Core的日誌組件

文件夾 tst allow sting fault optional manage erp www 簡介 日誌組件,作為程序員使用頻率最高的組件,給程序員開發調試程序提供了必要的信息。ASP.NET Core中內置了一個通用日誌接口ILogger,並實現了多種內置的日誌提供

Linux/下目錄包含的內容

規則 安裝 設備文件 oot spl nbsp 用戶命令 cdr mkdir /bin 用來貯存用戶命令。/usr/bin 也被用來貯存用戶命令。 /sbin 許多系統命令(例如 shutdown)的貯存位置。/usr/sbin 中也包括了許多系統命令。 /root 根