linux的chmod与chown
linux的文件根據文件及目錄的權限范圍,包括:
- u:USER,文件或目錄的擁有者
- g:Group,文件或目錄的所屬群組
- o:Other,除了文件或目錄擁有者或所屬群組之外,其他用戶皆屬于這個范圍
- a:all,全部的用戶,包括擁有者、所屬群組及其他用戶
權限包闊:
- r:讀取權限,數字為4
- w:寫入權限,數字為2
- x:執行或切換權限,代號為1
chmod的使用說明
SYNOPSIS
DESCRIPTION
? ? ? ?This ?manual ?page ?documents the GNU version of chmod. ?chmod changes
? ? ? ?the file mode bits of each given file according to mode, which can ?be
? ? ? ?either ?a symbolic representation of changes to make, or an octal num‐
? ? ? ?ber representing the bit pattern for the new mode bits.
? ? ? ?The format of a symbolic mode is [ugoa...][[-+=][perms...]...], ?where
? ? ? ?perms ?is either zero or more letters from the set rwxXst, or a single
? ? ? ?letter from the set ugo. ?Multiple symbolic modes can be given, ?sepa‐
? ? ? ?rated by commas.
? ? ? ?A ?combination of the letters ugoa controls which users' access to the
? ? ? ?file will be changed: the user who owns it (u), ?other ?users ?in ?the
? ? ? ?file's ?group ?(g), ?other ?users ?not in the file's group (o), or all
? ? ? ?users (a). ?If none of these are given, the effect is as if ?(a) ?were
? ? ? ?given, but bits that are set in the umask are not affected.
? ? ? ?The ?operator ?+ causes the selected file mode bits to be added to the
? ? ? ?existing file mode bits of each file; - causes them to be removed; and
? ? ? ?= ?causes ?them ?to be added and causes unmentioned bits to be removed
? ? ? ?except that a directory's unmentioned set user and group ID ?bits ?are
? ? ? ?not affected.
? ? ? ?The ?letters rwxXst select file mode bits for the affected users: read
? ? ? ?(r), ?write ?(w), ?execute ?(or ?search ?for ?directories) ?(x), ?exe‐
? ? ? ?cute/search ?only ?if ?the ?file is a directory or already has execute
? ? ? ?permission for some user (X), set user or group ID on ?execution ?(s),
? ? ? ?restricted deletion flag or sticky bit (t). ?Instead of one or more of
? ? ? ?these letters, you can specify exactly one of ?the ?letters ?ugo: ?the
? ? ? ?permissions granted to the user who owns the file (u), the permissions
? ? ? ?granted to other users who are members of the file's ?group ?(g), ?and
? ? ? ?the ?permissions ?granted to users that are in neither of the two pre‐
? ? ? ?ceding categories (o).
????????
? ? ? ?A numeric mode is from one to four ?octal ?digits ?(0-7), ?derived ?by
? ? ? ?adding ?up ?the ?bits with values 4, 2, and 1. ?Omitted digits are as‐
? ? ? ?sumed to be leading zeros. ?The first digit selects the ?set ?user ?ID
? ? ? ?(4) ?and ?set ?group ?ID (2) and restricted deletion or sticky (1) at‐
? ? ? ?tributes. ?The second digit selects permissions for the user who ?owns
? ? ? ?the file: read (4), write (2), and execute (1); the third selects per‐
? ? ? ?missions for other users in the file's group, with ?the ?same ?values;
? ? ? ?and ?the fourth for other users not in the file's group, with the same
? ? ? ?values.
? ? ? ?chmod never changes the permissions of symbolic links; the chmod ?sys‐
? ? ? ?tem call cannot change their permissions. ?This is not a problem since
? ? ? ?the permissions of symbolic links are never used. ?However, ?for ?each
? ? ? ?symbolic ?link ?listed ?on the command line, chmod changes the permis‐
? ? ? ?sions of the pointed-to file. ?In ?contrast, ?chmod ?ignores ?symbolic
? ? ? ?links encountered during recursive directory traversals.
????????
SETUID AND SETGID BITS
? ? ? ?chmod ?clears ?the ?set-group-ID ?bit ?of a regular file if the file's
? ? ? ?group ID does not match the user's effective group ID or ?one ?of ?the
? ? ? ?user's supplementary group IDs, unless the user has appropriate privi‐
? ? ? ?leges. ?Additional restrictions may cause ?the ?set-user-ID ?and ?set-
? ? ? ?group-ID ?bits ?of MODE or RFILE to be ignored. ?This behavior depends
? ? ? ?on the policy and functionality of the underlying chmod ?system ?call.
? ? ? ?When in doubt, check the underlying system behavior.
? ? ? ?For ?directories chmod preserves set-user-ID and set-group-ID bits un‐
? ? ? ?less you explicitly specify otherwise. ?You can set or clear the ?bits
? ? ? ?with ?symbolic modes like u+s and g-s. ?To clear these bits for direc‐
? ? ? ?tories with a numeric mode requires an ?additional ?leading ?zero, ?or
? ? ? ?leading = like 00755 , or =755
RESTRICTED DELETION FLAG OR STICKY BIT
? ? ? ?The ?restricted deletion flag or sticky bit is a single bit, whose in‐
? ? ? ?terpretation depends on the file type. ?For directories, ?it ?prevents
? ? ? ?unprivileged ?users ?from removing or renaming a file in the directory
? ? ? ?unless they own the file or the directory; ?this ?is ?called ?the ?re‐
? ? ? ?stricted ?deletion ?flag ?for ?the directory, and is commonly found on
? ? ? ?world-writable directories like /tmp. ?For regular files on some older
? ? ? ?systems, ?the bit saves the program's text image on the swap device so
? ? ? ?it will load more quickly when run; this is called the sticky bit.
chown的使用說明
chown將指定文件的擁有者改為指定的用戶或組,用戶可以是用戶名或者用戶ID;組可以是組名或者組ID;文件是以空格分開的要改變權限的文件列表,支持通配符。系統管理員經常使用chown命令,在將文件拷貝到另一個用戶的名錄下之后,讓用戶擁有使用該文件的權限。?
chown 命令用于更改文件的所有者為指定用戶,其語法格式如下:
?
命令參數
| -c | 顯示更改的部分的信息 |
| -f | 忽略錯誤信息 |
| -h | 修復符號鏈接 |
| -R | 處理指定目錄以及其子目錄下的所有文件 |
| -v | 顯示詳細的處理信息 |
| -deference | 作用與符號鏈接的指定,而不是鏈接文件本身 |
例如:
sudo chown cx:cx .file/ sudo chown cx:cx -R file/區別:
第一、指令名稱 : chmod
使用權限 : 所有使用者
使用方式 :?chmod [-cfvR] [--help] [--version] mode file...
說明 : Linux/Unix 的檔案存取權限分為三級 : 檔案擁有者、群組、其他。利用 chmod 可以藉以控制檔案如何被他人所存取。
mode : 權限設定字串,格式如下 :?[ugoa...][[+-=][rwxX]...][,...],其中u 表示該檔案的擁有者,g 表示與該檔案的擁有者屬于同一個群體(group)者,o 表示其他以外的人,a 表示這三者皆是。
- + 表示增加權限、- 表示取消權限、= 表示唯一設定權限。
- r 表示可讀取,w 表示可寫入,x 表示可執行,X 表示只有當該檔案是個子目錄或者該檔案已經被
- 設定過為可執行。
- -c : 若該檔案權限確實已經更改,才顯示其更改動作
- -f : 若該檔案權限無法被更改也不要顯示錯誤訊息
- -v : 顯示權限變更的詳細資料
- -R : 對目前目錄下的所有檔案與子目錄進行相同的權限變更(即以遞回的方式逐個變更)
- --help : 顯示輔助說明
- --version : 顯示版本
第二、指令名稱 : chown
使用權限 : root
使用方式 :?chown [-cfhvR] [--help] [--version] user[:group] file...
說明 : Linux/Unix 是多人多工作業系統,所有的檔案皆有擁有者。利用 chown 可以將檔案的擁有者加以改變。一般來說,這個指令只有是由系統管理者(root)所使用,一般使用者沒有權限可以改變別人的檔案擁有者,也沒有權限可以自己的檔案擁有者改設為別人。只有系統管理者(root)才有這樣的權限。
- user : 新的檔案擁有者的使用者 ID
- group : 新的檔案擁有者的使用者群體(group)
- -c或-change:作用與-v相似,但只傳回修改的部分
- -f或–quiet或–silent:不顯示錯誤信息
- -h或–no-dereference:只對符號鏈接的文件做修改,而不更改其他任何相關文件
- -R或-recursive:遞歸處理,將指定目錄下的所有文件及子目錄一并處理
- -v或–verbose:顯示指令執行過程
- –dereference:作用和-h剛好相反
- –help:顯示在線說明
- –reference=<參考文件或目錄>:把指定文件或目錄的所有者與所屬組,統統設置成和參考文件或目錄的所有者與所屬組相同
- –version:顯示版本信息
chown 修改文件和文件夾的用戶和用戶組屬性
chmod 修改文件和文件夾讀寫執行屬性
?
總結
以上是生活随笔為你收集整理的linux的chmod与chown的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开发微领地小蜜系统APP平台
- 下一篇: linux 其他常用命令