「ファイルのパーミッションと属性」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(文字列「Tips and tricks」を「ヒントとテクニック」に置換)
(他の1人の利用者による、間の3版が非表示)
5行目: 5行目:
 
{{Related|umask}}
 
{{Related|umask}}
 
{{Related|アクセス制御リスト}}
 
{{Related|アクセス制御リスト}}
{{Related3|Capabilities|ケイパビリティ}}
+
{{Related|ケイパビリティ}}
 
{{Related articles end}}
 
{{Related articles end}}
   
  +
[[ファイルシステム]]は[[wikipedia:File system permissions|パーミッション]]や[[wikipedia:File attribute|属性]]を使ってシステムプロセスが処理できるファイルやディレクトリのレベルを規定します。
== パーミッションの表示 ==
 
   
  +
{{Warning|パーミッションや属性のセキュリティについて言えば、対抗できるのはシステムが起動した後の攻撃にすぎません。マシンに物理的にアクセスできる攻撃者からデータを保護するには、[[ディスク暗号化]]を施す必要があります。}}
In order to use chmod to change permissions of a file or directory, you will first need to know what the current mode of access is. You can view the contents of a directory in the terminal by "cd" to that directory and then using:
 
   
  +
== パーミッションの表示 ==
$ ls -l
 
   
  +
[[Core Utilities#ls|ls]] コマンドの {{ic|-l}} オプションを使ってディレクトリの中身のパーミッション (あるいは'''ファイルモード''') を確認できます。例:
The {{ic|-l}} switch is important because using ''ls'' without it will only display the names of files or folders in the directory.
 
   
Below is an example of using {{ic|ls -l}} on my home directory:
+
{{hc|$ ls -l /path/to/directory|
 
{{hc|$ ls -l|
 
 
total 128
 
total 128
-rw-r--r-- 1 ben users 832 Jul 6 17:22 #chmodwiki#
+
drwxr-xr-x 2 archie users 4096 Jul 5 21:03 Desktop
drwxr-xr-x 2 ben users 4096 Jul 5 21:03 Desktop
+
drwxr-xr-x 6 archie users 4096 Jul 5 17:37 Documents
drwxr-xr-x 6 ben users 4096 Jul 5 17:37 Documents
+
drwxr-xr-x 2 archie users 4096 Jul 5 13:45 Downloads
drwxr-xr-x 2 ben users 4096 Jul 5 13:45 Downloads
+
-rw-rw-r-- 1 archie users 5120 Jun 27 08:28 customers.ods
drwxr-xr-x 2 ben users 4096 Jun 24 03:36 Movies
+
-rw-r--r-- 1 archie users 3339 Jun 27 08:28 todo
drwxr-xr-x 2 ben users 4096 Jun 24 03:38 Music
+
-rwxr-xr-x 1 archie users 2048 Jul 6 12:56 myscript.sh
-rw-r--r-- 1 ben users 57047 Jun 24 13:57 Namoroka_wallpaper.png
 
drwxr-xr-x 2 ben users 4096 Jun 26 00:09 Pictures
 
drwxr-xr-x 3 ben users 4096 Jun 24 05:03 R
 
-rw-r--r-- 1 ben users 354 Jul 6 17:15 chmodwiki
 
-rw-r--r-- 1 ben users 5120 Jun 27 08:28 data
 
-rw-r--r-- 1 ben users 3339 Jun 27 08:28 datadesign
 
-rw-r--r-- 1 ben users 2048 Jul 6 12:56 dustprac
 
-rw-r--r-- 1 ben users 1568 Jun 27 14:11 dustpracdesign
 
-rw-r--r-- 1 ben users 1532 Jun 27 14:07 dustpracdesign~
 
-rw-r--r-- 1 ben users 229 Jun 27 14:01 ireland.R
 
-rw-r--r-- 1 ben users 570 Jun 27 17:02 noattach.R
 
-rw-r--r-- 1 ben users 588 Jun 5 15:35 noattach.R~
 
 
}}
 
}}
   
  +
最初のカラムが一番重要です。例えば {{ic|drwxrwxrwx+}} という値であれば、それぞれの文字の意味は以下の表のようになります:
=== カラムの意味 ===
 
   
  +
{| class="wikitable"
最初のカラムはファイルのタイプを示しています:
 
  +
|- style="text-align:center;"
* '''-''' は通常のファイルです。
 
  +
| {{ic|d}}
* '''d''' はディレクトリ (他のファイルやフォルダを含むフォルダ) です。
 
  +
| {{ic|rwx}}
* '''p''' は名前付きパイプ (別名 FIFO) です。
 
  +
| {{ic|rwx}}
* '''l''' はシンボリックリンクです。
 
  +
| {{ic|rwx}}
  +
| {{ic|+}}
  +
|-
  +
| ファイルのタイプ。技術的にはパーミッションではありません。値については {{ic|info ls -n "What information is listed"}} を参照してください。
  +
| ファイルの所有者のパーミッション。下を参照。
  +
| ファイルのグループのパーミッション。下を参照。
  +
| ファイルの所有者でないユーザーのパーミッション。下を参照。
  +
| 他のアクセスメソッドがファイルに適用されているかどうか表す文字。この文字が空白のとき、他のアクセスメソッドは存在しません。{{ic|.}} という文字はファイルにセキュリティコンテキストが付与されていることを表します。他のアクセスメソッドが使われていると、{{ic|+}} の文字で表されます ([[アクセス制御リスト]]の場合)。
  +
|}
   
  +
三組のパーミッション文字列 (上記の例なら {{ic|rwx}}) は以下の文字によって構成されます:
パーミッションの後の文字列、最初のカラムが一番重要です。二番目のカラムはファイルに存在するリンクの数を示しています。無視してかまいません。三番目のカラムは二つの値・名前を含んでいます: 最初の値 (上記の例なら 'ben') はファイルを所有しているユーザーの名前です。二番目の値 (上記の例なら 'users') は所有者が属しているグループです (詳しくは[[グループ]]を読んで下さい)。
 
   
  +
{| class="wikitable"
The next column is the size of the file or directory in bytes and information after that are the dates and times the file or directory was last modified, and of course the name of the file or directory.
 
  +
! scope="col" style="width: 10%;" |
  +
! scope="col" style="width: 10%;" | 文字
  +
! scope="col" style="width: 30%;" | ファイルの場合
  +
! scope="col" style="width: 50%;" | ディレクトリの場合
  +
|-
  +
! rowspan="2" style="text-align:left;" | 読み取り権限 (最初の文字)
  +
| style="text-align:center;" | {{ic|-}}
  +
| ファイルを読み込むことができません。
  +
| ディレクトリの中身を表示することができません。
  +
|-
  +
| style="text-align:center;" | {{ic|r}}
  +
| ファイルを読み込むことができます。
  +
| ディレクトリの中身を表示することができます。
  +
|-
  +
! rowspan="2" style="text-align:left;" | 書き込み権限 (二番目の文字)
  +
| style="text-align:center;" | {{ic|-}}
  +
| ファイルを変更することができません。
  +
| ディレクトリの中身を変更することができません。
  +
|-
  +
| style="text-align:center;" | {{ic|w}}
  +
| ファイルを変更することができます。
  +
| ディレクトリの中身を変更することができます (新しいファイルやフォルダを作成したり、既存のファイルやフォルダの名前を変更したり削除するなど)。実行権限も設定されていないと、このパーミッションは反映されません。
  +
|-
  +
! rowspan="6" style="text-align:left;" | 実行権限 (三番目の文字)
  +
| style="text-align:center;" | {{ic|-}}
  +
| ファイルを実行することができません。
  +
| ディレクトリを [[Core Utilities|cd]] で開くことができます。
  +
|-
  +
| style="text-align:center;" | {{ic|x}}
  +
| ファイルを実行できます。
  +
| ディレクトリを [[Core Utilities|cd]] で開くことができます。親のディレクトリから継承を行う唯一のパーミッションビットです。パス内の全てのフォルダに {{ic|x}} ビットが設定されていない場合、最後のファイルやディレクトリはパーミッションと関係なく、開くことができません。詳しくは {{ic|man 7 path_resolution}} を参照。
  +
|-
  +
| style="text-align:center;" | {{ic|s}}
  +
| colspan="2" | '''u'''ser の組の場合 [[Wikipedia:setuid|setuid]] ビット。'''g'''roup の組の場合 '''setgid''' ビット。'''o'''thers の組には存在しません。{{ic|x}} が設定されていることも示します。
  +
|-
  +
| style="text-align:center;" | {{ic|S}}
  +
| colspan="2" | {{ic|s}} と同じですが、{{ic|x}} は設定されていません。通常のファイルで使われていることは滅多になく、フォルダでは意味がありません。
  +
|-
  +
| style="text-align:center;" | {{ic|t}}
  +
| colspan="2" | [[Wikipedia:ja:スティッキービット|スティッキービット]]。'''o'''thers の組でしか存在しません。{{ic|x}} が設定されていることも示します。
  +
|-
  +
| style="text-align:center;" | {{ic|T}}
  +
| colspan="2" | {{ic|t}} と同じですが、{{ic|x}} は設定されていません。通常のファイルで使われていることは滅多になく、フォルダでは意味がありません。
  +
|}
   
  +
詳しくは {{ic|info Coreutils -n "Mode Structure"}} や {{ic|man 1 chmod}} を参照。
=== パーミッションの意味 ===
 
   
  +
==== サンプル ====
The first three letters, after the first '''-''' or '''d''', are the permissions the owner has. The next three letters are permissions that apply to the group. The final three letters are the permissions that apply to everyone else. Each set of three letters is made up of '''r w''' and '''x'''. '''r''' is always in the first position, '''w''' is always in the second position, and '''x''' is always in the third position. '''r''' is the read permission, '''w''' is the write permission, and '''x''' is the execute permission. If there is a hyphen ('''-''') in the place of one of these letters it means the permission is not granted, and if the letter is present then it is granted.
 
   
  +
Let us see some examples to clarify:
==== フォルダ ====
 
   
  +
'''drwx------''' 6 archie users 4096 Jul 5 17:37 Documents
In case of folders the mode bits can be interpreted as follows:
 
* '''r''' (read) stands for the ability to read the table of contents of the given directory,
 
* '''w''' (write) stands for the ability to write the table of contents of the given directory (create new files, folders; rename, delete existing files, folders) '''if and only if''' execute bit is set. Otherwise this permission is meaningless.
 
* '''x''' (execute) stands for the ability to enter the given directory with command {{ic|cd}} and access files, folders in that directory.
 
   
  +
Archie has full access to the Documents directory. He can list, create files and rename, delete any file in Documents, regardless of file permissions. His ability to access a file depends on the file's permission.
Let's see some examples to clarify, taking one directory from above:
 
   
  +
'''dr-x------''' 6 archie users 4096 Jul 5 17:37 Documents
{{bc|
 
# Ben has full access to the Documents directory.
 
# He can list, create files and rename, delete any file in Documents,
 
# regardless of file permissions.
 
# His ability to access a file depends on the file's permission.
 
'''drwx------ 6''' ben users 4096 Jul 5 17:37 Documents
 
   
# Ben has full access except he can not create, rename, delete
+
Archie has full access except he can not create, rename, delete any file. He can list the files and (if file's permission empowers) may access an existing file in Documents.
# any file.
 
# He can list the files and (if file's permission empowers)
 
# may access an existing file in Documents.
 
'''dr-x------ 6''' ben users 4096 Jul 5 17:37 Documents
 
   
  +
'''d-wx------''' 6 archie users 4096 Jul 5 17:37 Documents
# Ben can not do 'ls' in Documents but if he knows
 
# the name of an existing file then he may list, rename, delete or
 
# (if file's permission empowers him) access it.
 
# Also, he is able to create new files.
 
'''d-wx------ 6''' ben users 4096 Jul 5 17:37 Documents
 
   
# Ben is only capable of (if file's permission empowers him)
+
Archie can not do 'ls' in Documents but if he knows the name of an existing file then he may list, rename, delete or (if file's permission empowers him) access it. Also, he is able to create new files.
# access those files in Documents which he knows of.
 
# He can not list already existing files or create, rename,
 
# delete any of them.
 
'''d--x------ 6''' ben users 4096 Jul 5 17:37 Documents
 
}}
 
   
  +
'''d--x------''' 6 archie users 4096 Jul 5 17:37 Documents
You should keep in mind that we elaborate on directory permissions and it has nothing to do with the individual file permissions. When you create new file it is the directory what changes, that is why you need write permission to the directory.
 
{{Note|To keep out graphical file managers, you ought to remove '''r''', not '''x'''.}}
 
   
  +
Archie is only capable of (if file's permission empowers him) access those files in Documents which he knows of. He can not list already existing files or create, rename, delete any of them.
==== ファイル ====
 
   
  +
You should keep in mind that we elaborate on directory permissions and it has nothing to do with the individual file permissions. When you create a new file it is the directory that changes. That is why you need write permission to the directory.
Let's look at another example, this time of a file, not a directory:
 
  +
'''-rw-r--r--''' 1 ben users 5120 Jun 27 08:28 data
 
  +
Let us look at another example, this time of a file, not a directory:
 
  +
'''- rw- r-- r--''' 1 ben users 5120 Jun 27 08:28 data (Split the permissions coloumn again for easier interpretation)
 
  +
'''-rw-r--r--''' 1 archie users 5120 Jun 27 08:28 foobar
Here we can see the first letter is not '''d''' but '''-'''. So we know it is a file, not a directory. Next the owners permissions are '''rw-''' so the owner has the ability to read and write but not execute. This may seem odd that the owner does not have all three permissions, but the x permission is not needed as it is a text/data file, to be read by a text editor such as Gedit, EMACS, or software like R, and not an executable in it's own right (if it contained something like python programming code then it very well could be). The group's permssions are set to '''r--''', so the group has the ability to read the file but not write/edit it in any way - it is essentially like setting something to Read-Only. We can see that the same permissions apply to everyone else as well.
 
  +
  +
Here we can see the first letter is not {{ic|d}} but {{ic|-}}. So we know it is a file, not a directory. Next the owner's permissions are {{ic|rw-}} so the owner has the ability to read and write but not execute. This may seem odd that the owner does not have all three permissions, but the {{ic|x}} permission is not needed as it is a text/data file, to be read by a text editor such as Gedit, EMACS, or software like R, and not an executable in its own right (if it contained something like python programming code then it very well could be). The group's permssions are set to {{ic|r--}}, so the group has the ability to read the file but not write/edit it in any way — it is essentially like setting something to read-only. We can see that the same permissions apply to everyone else as well.
   
== chmod コマンドでパーミッションを変更 ==
+
== パーミッションを変更 ==
   
  +
[[Wikipedia:chmod|chmod]] は Linux などの Unix ライクなオペレーティングシステムで使われるコマンドで、ファイルやディレクトリのパーミッション (もしくはアクセスモード) を変更することができます (''ch''ange ''mod''e)。
[[Wikipedia:chmod|chmod]] is a command in Linux and other Unix-like operating systems. It allows you to ''ch''ange the permissions (or access ''mod''e) of a file or directory.
 
   
 
=== テキストを使う方法 ===
 
=== テキストを使う方法 ===
   
  +
ファイルのパーミッション (あるいは''アクセスモード'') を変更するにはターミナルで ''chmod'' コマンドを使います。以下はコマンドの一般的な使い方です:
To change the permissions-or ''access mode''-of a file, we use the {{ic|chmod}} command in a terminal. Below is the command's general structure:
 
   
 
chmod ''who''=''permissions'' ''filename''
 
chmod ''who''=''permissions'' ''filename''
   
  +
{{ic|''who''}} はパーミッションを与える対象を指定する文字です。以下のどれかになります:
Where ''Who'' is any from a range of letters, and each signifies who you are going to give the permission to. They are as follows:
 
   
  +
* {{ic|u}}: ファイルを所有する[[ユーザーとグループ|ユーザー]]。
u - The '''u'''ser that own the file.
 
  +
* {{ic|g}}: ファイルが所属する[[ユーザーとグループ|グループ]]。
g - The '''g'''roup the file belongs to.
 
  +
* {{ic|o}}: 他 ('''o'''ther) のユーザー。つまり、全てのユーザー。
o - The '''o'''ther users i.e. everyone else.
 
  +
* {{ic|a}}: 上記の全て ('''a'''ll)。{{ic|ugo}} と入力する代わりに {{ic|a}} で指定できます。
a - '''a'''ll of the above - use this instead of having to type '''ugo'''.
 
   
  +
設定するパーミッションは [[#パーミッションの表示]] で説明しているのと同じです ({{ic|r}}, {{ic|w}}, {{ic|x}})。
The permissions are the same as already discussed ('''r, w,''' and '''x''').
 
   
Let's have a look at some examples now using this command. Suppose we became very protective of the Documents directory and wanted to deny everybody but ourselves, permissions to read, write, and execute (or in this case search/look) in it:
+
Now have a look at some examples using this command. Suppose you became very protective of the Documents directory and wanted to deny everybody but yourself, permissions to read, write, and execute (or in this case search/look) in it:
   
Before: drwxr-xr-x 6 ben users 4096 Jul 5 17:37 Documents
+
ビフォー: {{ic|drwxr-xr-x 6 archie users 4096 Jul 5 17:37 Documents}}
   
Command 1: chmod g= Documents
+
$ chmod g= Documents
Command 2: chmod o= Documents
+
$ chmod o= Documents
   
After: drwx------ 6 ben users 4096 Jul 6 17:32 Documents
+
アフター: {{ic|drwx------ 6 archie users 4096 Jul 6 17:32 Documents}}
   
Here, because we want to deny permissions, we do not put any letter after the '''=''' where permissions would be entered. Now you can see that only the owner's permissions are '''rwx''' and all other permissions are '''-''''s.
+
Here, because you want to deny permissions, you do not put any letters after the {{ic|1==}} where permissions would be entered. Now you can see that only the owner's permissions are {{ic|rwx}} and all other permissions are {{ic|-}}.
   
 
This can be reverted with:
 
This can be reverted with:
   
Before: drwx------ 6 ben users 4096 Jul 6 17:32 Documents
+
Before: {{ic|drwx------ 6 archie users 4096 Jul 6 17:32 Documents}}
   
Command 1: chmod g=rx Documents
+
$ chmod g=rx Documents
Command 2: chmod o=rx Documents
+
$ chmod o=rx Documents
   
After: drwxr-xr-x 6 ben users 4096 Jul 6 17:32 Documents
+
After: {{ic|drwxr-xr-x 6 archie users 4096 Jul 6 17:32 Documents}}
   
In the next example, we want to grant read and execute permissions to the group, and other users, so we put the letters for the permissions ('''r''' and '''x''') after the '''=''', with no spaces.
+
In the next example, you want to grant read and execute permissions to the group, and other users, so you put the letters for the permissions ({{ic|r}} and {{ic|x}}) after the {{ic|1==}}, with no spaces.
   
You can simplify this to put more than one '''who''' letter in the same command e.g:
+
You can simplify this to put more than one {{ic|''who''}} letter in the same command, e.g:
   
chmod go=rx Documents
+
$ chmod go=rx Documents
   
{{Note|1=It does not matter which order you put the who letters or the permission letters in a {{ic|chmod}} command: you could have {{ic|1=chmod go=rx File}} or {{ic|1=chmod og=xr File}}. It is all the same.}}
+
{{Note|1=It does not matter in which order you put the {{ic|''who''}} letters or the permission letters in a {{ic|chmod}} command: you could have {{ic|1=chmod go=rx file}} or {{ic|1=chmod og=xr file}}. It is all the same.}}
   
Now let's consider a second example, say we want to change our data file so that we have read and write permissions, and fellow users in our group '''users''' who may be colleagues working with us on '''data''', can also read and write to it, but other users can only read it:
+
Now let us consider a second example, suppose you want to change a {{ic|foobar}} file so that you have read and write permissions, and fellow users in the group {{ic|users}} who may be colleagues working on {{ic|foobar}}, can also read and write to it, but other users can only read it:
   
Before: -rw-r--r-- 1 ben users 5120 Jun 27 08:28 data
+
Before: {{ic|-rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar}}
   
Command1: chmod g=rw data
+
$ chmod g=rw foobar
   
After: -rw-rw-r-- 1 ben users 5120 Jun 27 08:28 data
+
After: {{ic|-rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar}}
   
This is exactly like the first example, but with a data file, not a directory, and we grant a write permission (just so as to give an example of granting every permission).
+
This is exactly like the first example, but with a file, not a directory, and you grant write permission (just so as to give an example of granting every permission).
   
 
==== テキストを使う方法のショートカット ====
 
==== テキストを使う方法のショートカット ====
   
The {{ic|chmod}} command lets us add and subtract permissions from an existing set using '''+''' or '''-''' instead of '''='''. This is different to the above commands, which essentially re-write the permissions (i.e. to change a permission from '''r--''' to '''rw-''', you still need to include '''r''' as well as '''w''' after the '''=''' in the {{ic|chmod}} command. If you missed out '''r''', it would take away the '''r''' permission as they are being re-written with the '''='''. Using '''+''' and '''-''' avoid this by adding or taking away from the ''current'' set of permissions).
+
The ''chmod'' command lets add and subtract permissions from an existing set using {{ic|+}} or {{ic|-}} instead of {{ic|1==}}. This is different from the above commands, which essentially re-write the permissions (e.g. to change a permission from {{ic|r--}} to {{ic|rw-}}, you still need to include {{ic|r}} as well as {{ic|w}} after the {{ic|1==}} in the ''chmod'' command invocation. If you missed out {{ic|r}}, it would take away the {{ic|r}} permission as they are being re-written with the {{ic|1==}}. Using {{ic|+}} and {{ic|-}} avoids this by adding or taking away from the ''current'' set of permissions).
   
Let's try this '''+''' and '''-''' method with the previous example of adding write permissions to the group:
+
Let us try this {{ic|+}} and {{ic|-}} method with the previous example of adding write permissions to the group:
   
Before: -rw-r--r-- 1 ben users 5120 Jun 27 08:28 data
+
Before: {{ic|-rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar}}
 
 
Command: chmod g+w data
+
$ chmod g+w foobar
   
After: -rw-rw-r-- 1 ben users 5120 Jun 27 08:28 data
+
After: {{ic|-rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar}}
   
 
Another example, denying write permissions to all ('''a'''):
 
Another example, denying write permissions to all ('''a'''):
   
Before: -rw-rw-r-- 1 ben users 5120 Jun 27 08:28 data
+
Before: {{ic|-rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar}}
 
 
Command: chmod a-w data
+
$ chmod a-w foobar
   
After: -r--r--r-- 1 ben users 5120 Jun 27 08:28 data
+
After: {{ic|-r--r--r-- 1 archie users 5120 Jun 27 08:28 foobar}}
  +
  +
A different shortcut is the special {{ic|X}} mode: this is not an actual file mode, but it is often used in conjunction with the {{ic|-R}} option to set the executable bit only for directories, and leave it unchanged for regular files, for example:
  +
  +
$ chmod -R a+rX ./data/
   
 
==== パーミッションのコピー ====
 
==== パーミッションのコピー ====
   
It is possible to tell {{ic|chmod}} to copy the permissions from one class, say the owner, and give those same permissions to group or even all. To do this, instead of putting '''r''', '''w''', or '''x''' after the '''=''', we put another '''who''' letter. e.g:
+
It is possible to tell ''chmod'' to copy the permissions from one class, say the owner, and give those same permissions to group or even all. To do this, instead of putting {{ic|r}}, {{ic|w}}, or {{ic|x}} after the {{ic|1==}}, put another ''who'' letter. e.g:
   
Before: -rw-r--r-- 1 ben users 5120 Jun 27 08:28 data
+
Before: {{ic|-rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar}}
   
Command: chmod g=u data
+
$ chmod g=u foobar
   
After: -rw-rw-r-- 1 ben users 5120 Jun 27 08:28 data
+
After: {{ic|-rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar}}
   
This command essentially translates to "change the permissions of group ('''g='''), to be the same as the owning user ('''=u'''). Note that you cannot copy a set of permissions as well as grant new ones e.g.:
+
This command essentially translates to "change the permissions of group ({{ic|1=g=}}), to be the same as the owning user ({{ic|1==u}}). Note that you cannot copy a set of permissions as well as grant new ones e.g.:
   
chmod g=wu data
+
$ chmod g=wu foobar
   
In that case, {{ic|chmod}} will have a small fit and throw you an error.
+
In that case ''chmod'' throw an error.
   
 
=== 数字を使う方法 ===
 
=== 数字を使う方法 ===
   
  +
''chmod'' では数字を使ってパーミッションを設定することもできます。
chmod can also set permissions using numbers.
 
   
  +
数字を使用する場合、所有者とグループ、その他のユーザーのパーミッションを同時に編集することができます。コマンドは以下のようになります:
Using numbers is another method which allows you to edit the permissions for all three owner, group, and others at the same time. This basic structure of the code is this:
 
   
''chmod xxx file/directory''
+
$ chmod ''xxx'' ''filename''
   
  +
{{ic|'''xxx'''}} は3桁の数字になります。各桁の数字は0から7までのどれかになります。1番目の数字は所有者のパーミッション、2番目の数字は所有グループのパーミッション、3番目の数字は他のユーザーのパーミッションになります。
Where '''xxx''' is a 3 digit number where each digit can be anything from 1 to 7. The first digit applies to permissions for owner, the second digit applies to permissions for group, and the third digit applies to permissions for all others.
 
   
In this number notation, the values r, w, and x have their own number value:
+
In this number notation, the values {{ic|r}}, {{ic|w}}, and {{ic|x}} have their own number value:
   
 
r=4
 
r=4
211行目: 234行目:
 
x=1
 
x=1
   
To come up with a three digit number you need to consider what permissions you want owner, group, and user to have, and then total their values up. For example, say I wanted to grant the owner of a directory read write and execution permissions, and I wanted group and everyone else to have just read and execute permissions. I would come up with the numerical values like so:
+
To come up with a 3-digit number you need to consider what permissions you want owner, group, and user to have, and then total their values up. For example, if you want to grant the owner of a directory read write and execution permissions, and you want group and everyone else to have just read and execute permissions, you would come up with the numerical values like so:
   
Owner: rwx = 4+2+1=7
+
* Owner: {{ic|rwx}}=4+2+1=7
Group: r-x = 4+0+1=5 (or just 4+1=5)
+
* Group: {{ic|r-x}}=4+0+1=5
Other: r-x = 4+0+1=5 (or just 4+1=5)
+
* Other: {{ic|r-x}}=4+0+1=5
   
  +
$ chmod 755 ''filename''
Final number = 755
 
 
Command: ''chmod 755 filename''
 
   
 
This is the equivalent of using the following:
 
This is the equivalent of using the following:
   
chmod u=rwx filename
+
$ chmod u=rwx ''filename''
chmod go=rx filename
+
$ chmod go=rx ''filename''
   
Most folders/directories are set to '''755''' to allow reading and writing and execution to the owner, but deny writing to everyone else, and files are normally '''644''' to allow reading and writing for the owner but just reading for everyone else, refer to the last note on the lack of '''x''' permissions with non executable files - its the same deal here.
+
Most folders and directories are set to {{ic|755}} to allow reading, writing and execution to the owner, but deny writing to everyone else, and files are normally {{ic|644}} to allow reading and writing for the owner but just reading for everyone else; refer to the last note on the lack of {{ic|x}} permissions with non executable files: it is the same thing here.
   
To see this in action with examples consider the previous example I've been using but with this numerical method applied instead:
+
To see this in action with examples consider the previous example that has been used but with this numerical method applied instead:
   
Before: -rw-r--r-- 1 ben users 5120 Jun 27 08:28 data
+
Before: {{ic|-rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar}}
   
Command: chmod 664 data
+
$ chmod 664 foobar
   
After: -rw-rw-r-- 1 ben users 5120 Jun 27 08:28 data
+
After: {{ic|-rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar}}
   
If this were an executable the number would be '''774''' if I wanted to grant executable permission to the owner and group. Alternatively if I wanted everyone to only have read permission the number would be '''444'''. Treating '''r''' as '''4''', '''w''' as '''2''', and '''x''' as '''1''' is probably the easiest way to work out the numerical values for using '''chmod xxx filename''', but there is also a binary method, where each permission has a binary number, and then that is in turn converted to a number. It is a bit more convoluted, but I include it for completeness.
+
If this were an executable the number would be {{ic|774}} if you wanted to grant executable permission to the owner and group. Alternatively if you wanted everyone to only have read permission the number would be {{ic|444}}. Treating {{ic|r}} as 4, {{ic|w}} as 2, and {{ic|x}} as 1 is probably the easiest way to work out the numerical values for using {{ic|chmod ''xxx'' ''filename''}}, but there is also a binary method, where each permission has a binary number, and then that is in turn converted to a number. It is a bit more convoluted, but here included for completeness.
   
 
Consider this permission set:
 
Consider this permission set:
   
'''- rwx r-x r--'''
+
-rwxr-xr--
   
 
If you put a 1 under each permission granted, and a 0 for every one not granted, the result would be something like this:
 
If you put a 1 under each permission granted, and a 0 for every one not granted, the result would be something like this:
   
  +
-rwxrwxr-x
'''- rwx rwx r-x'''
 
  +
111111101
'''111 111 101'''
 
   
 
You can then convert these binary numbers:
 
You can then convert these binary numbers:
254行目: 275行目:
 
011=3 111=7
 
011=3 111=7
   
The value of the above would therefore be '''775'''.
+
The value of the above would therefore be 775.
   
 
Consider we wanted to remove the writable permission from group:
 
Consider we wanted to remove the writable permission from group:
   
'''- rwz r-x r-x'''
+
-rwzr-xr-x
  +
111101101
'''111 101 101'''
 
   
The value would therefore be '''755''' and you would use '''chmod 755 filename''' to remove the writable permission. You will notice you get the same three digit number no matter which method you use. Whether you use text or numbers will depend on personal preference and typing speed. When you want to restore a directory or file to default permissions i.e. read and write (and execute) permission to the owner but deny write permission to everyone else, it may be faster to use '''chmod 755/644 directory/filename'''. But if you are changing the permissions to something out of the norm, it may be simpler and quicker to use the text method as opposed to trying to convert it to numbers, which may lead to a mistake. It could be argued that there isn't any real significant difference in the speed of either method for a user that only needs to use chmod on occasion.
+
The value would therefore be 755 and you would use {{ic|chmod 755 ''filename''}} to remove the writable permission. You will notice you get the same three digit number no matter which method you use. Whether you use text or numbers will depend on personal preference and typing speed. When you want to restore a directory or file to default permissions e.g. read and write (and execute) permission to the owner but deny write permission to everyone else, it may be faster to use {{ic|chmod 755/644 ''filename''}}. However if you are changing the permissions to something out of the norm, it may be simpler and quicker to use the text method as opposed to trying to convert it to numbers, which may lead to a mistake. It could be argued that there is not any real significant difference in the speed of either method for a user that only needs to use ''chmod'' on occasion.
   
 
=== まとめて chmod ===
 
=== まとめて chmod ===
   
  +
通常、ディレクトリとファイルは同じパーミッションにする必要はありません。一度にディレクトリツリーを修正しなくてはならない場合、''find'' を使って選択的に修正します。
Generally directories and files should not have the same permissions. If it is necessary to bulk modify a directory tree, use ''find'' to selectively modify one or the other.
 
   
  +
ディレクトリだけを 755 に chmod するには:
To chmod only directories to 755:
 
   
 
$ find ''directory'' -type d -exec chmod 755 {} +
 
$ find ''directory'' -type d -exec chmod 755 {} +
   
  +
ファイルだけを 644 に chmod するには:
To chmod only files to 644:
 
   
 
$ find ''directory'' -type f -exec chmod 644 {} +
 
$ find ''directory'' -type f -exec chmod 644 {} +
   
== chown コマンドで所有者を変更 ==
+
== 所有者を変更 ==
   
  +
[[Wikipedia:chown|chown]] はファイルやディレクトリの所有者を変更します。場合によってはパーミッションの変更よりも簡単です。
Whilst this is an article dedicated to chmod, [[Wikipedia:chown|chown]] deserves mention as well. Where chmod changes the access mode of a file or directory, chown changes the owner of a file or directory, which is quicker and easier than altering the permissions in some cases, but do be careful when you do so.
 
   
  +
以下の例では、[[GParted]] を使ってバックアップデータ用に新しいパーティションを作成しています。Gparted は全てを root で実行するためファイルの所有者が全て root になります。ユーザーがパーティションをマウントしてデータを書き込もうとすると、パーミッションによって拒否されてしまいます。
Consider the following example, making a new partition with GParted for backup data. Gparted does this all as root so everything belongs to root. This is all well and good but when it came to writing data to the mounted partition, permission was denied.
 
   
brw-rw---- 1 root disk 8, 9 Jul 6 16:02 sda9
+
brw-rw---- 1 root disk 8, 9 Jul 6 16:02 sda9
drwxr-xr-x 5 root root 4096 Jul 6 16:01 Backup
+
drwxr-xr-x 5 root root 4096 Jul 6 16:01 Backup
   
  +
マウントされているディレクトリ ({{ic|/media/Backup}}) と同じように、{{ic|/dev}} のデバイスの所有者も root になっていることが確認できます。マウントされているディレクトリの所有者を変更するには以下を実行:
As you can see the device in '''/dev''' is owned by '''root''', as is where it is mounted ('''/media/Backup'''). To change the owner of where it is mounted one can do the following:
 
   
Before: drwxr-xr-x 5 root root 4096 Jul 6 16:01 Backup
+
ビフォー: {{ic|drwxr-xr-x 5 root root 4096 Jul 6 16:01 Backup}}
   
Command: chown ben Backup (cd'd to /media first)
+
# chown archie /media/Backup
   
After drwxr-xr-x 5 ben root 4096 Jul 6 16:01 Backup
+
アフター: {{ic|drwxr-xr-x 5 archie root 4096 Jul 6 16:01 Backup}}
   
Now the partition can have backup data written to it as instead of altering the permissions, as the owner already has '''rwx''' permissions, the owner has been altered to the user ben. Alternatives would be to alter the permissions for everyone else (undesirable as it's a backup permission) or adding the user to the group '''root'''.
+
Now the partition can have backup data written to it as instead of altering the permissions, as the owner already has {{ic|rwx}} permissions, the owner has been altered to the user archie. Alternatives would be to alter the permissions for everyone else (undesirable as it is a backup permission) or adding the user to the group {{ic|root}}.
   
 
== アクセス制御リスト ==
 
== アクセス制御リスト ==
304行目: 325行目:
 
[[ユーザーとグループ|ユーザーやグループ]]の読み書きや実行権限を制御するファイルモードビットとは別に、ファイル操作の可否を細かくカスタマイズするためのファイル属性を[[ファイルシステム]]がサポートしている場合があります。このセクションではそれらの属性を使用する方法を説明します。
 
[[ユーザーとグループ|ユーザーやグループ]]の読み書きや実行権限を制御するファイルモードビットとは別に、ファイル操作の可否を細かくカスタマイズするためのファイル属性を[[ファイルシステム]]がサポートしている場合があります。このセクションではそれらの属性を使用する方法を説明します。
   
  +
{{Warning|デフォルトでは、[[Core Utilities|cp]] や [[rsync]] などのプログラムはファイルの属性までコピーしません。}}
{{Warning|By default, file attributes are not preserved by cp, rsync, and probably others.}}
 
   
 
=== chattr と lsattr ===
 
=== chattr と lsattr ===
   
For ext2 and [[ext3]] file systems, the {{Pkg|e2fsprogs}} package contains the programs [[Wikipedia:lsattr|lsattr]] and [[Wikipedia:chattr|chattr]] that list and change a file's attributes, respectively. Though some are not honored by all file systems, the available attributes are:
+
ext2 [[ext3]] ファイルシステムでは、{{Pkg|e2fsprogs}} パッケージに [[Wikipedia:lsattr|lsattr]] [[Wikipedia:chattr|chattr]] というプログラムが含まれており、ファイルの属性を確認したり変更することができます。全てのファイルシステムで反映されるわけではありませんが、以下の属性を使用することができます:
   
  +
* {{ic|a}}: 追記のみ
*a : append only
 
  +
* {{ic|c}}: 圧縮
*c : compressed
 
  +
* {{ic|d}}: ダンプしない
*d : no dump
 
  +
* {{ic|e}}: 拡張フォーマット
*e : extent format
 
  +
* {{ic|i}}: 変更不可
*i : immutable
 
  +
* {{ic|j}}: データのジャーナリング
*j : data journalling
 
  +
* {{ic|s}}: 安全な消去
*s : secure deletion
 
  +
* {{ic|t}}: 末尾のマージをしない
*t : no tail-merging
 
  +
* {{ic|u}}: 削除不可
*u : undeletable
 
  +
* {{ic|A}}: アクセス日時を更新しない
*A : no atime updates
 
  +
* {{ic|C}}: コピーオンライトしない
*C : no copy on write
 
  +
* {{ic|D}}: ディレクトリの同期更新
*D : synchronous directory updates
 
  +
* {{ic|S}}: 同期更新
*S : synchronous updates
 
  +
* {{ic|T}}: ディレクトリ階層のトップ
*T : top of directory hierarchy
 
   
  +
例えば、特定のファイルに変更不可ビットを設定したい場合、次のコマンドを使用:
For example, if you want to set the immutable bit on some file, use the following command:
 
   
 
# chattr +i ''/path/to/file''
 
# chattr +i ''/path/to/file''
   
  +
ファイルから属性を削除したいときは {{ic|+}} を {{ic|-}} に変更してください。
To remove an attribute on a file just change {{ic|+}} to {{ic|-}}.
 
   
 
== 拡張属性 ==
 
== 拡張属性 ==
   
From {{ic|attr(5)}}: "Extended attributes are name:value pairs associated permanently with files and directories". There are four extended attribute classes: security, system, trusted and user.
+
{{ic|attr(5)}} より: "拡張属性とはファイルやディレクトリに関連付けられる name:value のペアである。拡張属性には4つのクラスがある: security, system, trusted, user"。
   
  +
{{Warning|デフォルトでは、[[Core Utilities|cp]] や [[rsync]] などのプログラムは拡張属性までコピーしません。}}
{{Warning|By default, extended attributes are not preserved by cp, rsync, and probably others.}}
 
   
 
=== ユーザー拡張属性 ===
 
=== ユーザー拡張属性 ===
   
  +
ユーザー拡張属性を使うことでファイルに任意の情報を保存できます。ユーザー拡張属性を作成するには:
User extended attributes can be used to store arbitrary information about a file. To create one:
 
   
 
$ setfattr -n user.checksum -v "3baf9ebce4c664ca8d9e5f6314fb47fb" foo.bar
 
$ setfattr -n user.checksum -v "3baf9ebce4c664ca8d9e5f6314fb47fb" foo.bar
   
  +
拡張属性を表示するには getfattr を使用:
Use getfattr to display extended attributes:
 
   
 
$ getfattr -d foo.bar
 
$ getfattr -d foo.bar
 
# file: foo.bar
 
# file: foo.bar
 
user.checksum="3baf9ebce4c664ca8d9e5f6314fb47fb"
 
user.checksum="3baf9ebce4c664ca8d9e5f6314fb47fb"
  +
  +
=== ケイパビリティ ===
  +
  +
拡張属性は[[ケイパビリティ]]を設定するのにも使われます。
  +
  +
== ヒントとテクニック ==
  +
  +
=== ルートの保護 ===
  +
  +
{{ic|--preserve-root}} フラグを使うことで {{ic|chmod}} を {{ic|/}} で再帰的に実行してしまうことを防ぐことができます。システム全体から実行可能属性を取り除いてシステムが破壊されてしまう恐れがなくなります。フラグを毎回使いたい場合、[[エイリアス]]で設定すると良いでしょう。[https://www.reddit.com/r/linux/comments/4ni3xe/tifu_sudo_chmod_644/] も参照。
   
 
== 参照 ==
 
== 参照 ==

2017年10月12日 (木) 23:15時点における版

関連記事

ファイルシステムパーミッション属性を使ってシステムプロセスが処理できるファイルやディレクトリのレベルを規定します。

警告: パーミッションや属性のセキュリティについて言えば、対抗できるのはシステムが起動した後の攻撃にすぎません。マシンに物理的にアクセスできる攻撃者からデータを保護するには、ディスク暗号化を施す必要があります。

パーミッションの表示

ls コマンドの -l オプションを使ってディレクトリの中身のパーミッション (あるいはファイルモード) を確認できます。例:

$ ls -l /path/to/directory
total 128
drwxr-xr-x 2 archie users  4096 Jul  5 21:03 Desktop
drwxr-xr-x 6 archie users  4096 Jul  5 17:37 Documents
drwxr-xr-x 2 archie users  4096 Jul  5 13:45 Downloads
-rw-rw-r-- 1 archie users  5120 Jun 27 08:28 customers.ods
-rw-r--r-- 1 archie users  3339 Jun 27 08:28 todo
-rwxr-xr-x 1 archie users  2048 Jul  6 12:56 myscript.sh

最初のカラムが一番重要です。例えば drwxrwxrwx+ という値であれば、それぞれの文字の意味は以下の表のようになります:

d rwx rwx rwx +
ファイルのタイプ。技術的にはパーミッションではありません。値については info ls -n "What information is listed" を参照してください。 ファイルの所有者のパーミッション。下を参照。 ファイルのグループのパーミッション。下を参照。 ファイルの所有者でないユーザーのパーミッション。下を参照。 他のアクセスメソッドがファイルに適用されているかどうか表す文字。この文字が空白のとき、他のアクセスメソッドは存在しません。. という文字はファイルにセキュリティコンテキストが付与されていることを表します。他のアクセスメソッドが使われていると、+ の文字で表されます (アクセス制御リストの場合)。

三組のパーミッション文字列 (上記の例なら rwx) は以下の文字によって構成されます:

文字 ファイルの場合 ディレクトリの場合
読み取り権限 (最初の文字) - ファイルを読み込むことができません。 ディレクトリの中身を表示することができません。
r ファイルを読み込むことができます。 ディレクトリの中身を表示することができます。
書き込み権限 (二番目の文字) - ファイルを変更することができません。 ディレクトリの中身を変更することができません。
w ファイルを変更することができます。 ディレクトリの中身を変更することができます (新しいファイルやフォルダを作成したり、既存のファイルやフォルダの名前を変更したり削除するなど)。実行権限も設定されていないと、このパーミッションは反映されません。
実行権限 (三番目の文字) - ファイルを実行することができません。 ディレクトリを cd で開くことができます。
x ファイルを実行できます。 ディレクトリを cd で開くことができます。親のディレクトリから継承を行う唯一のパーミッションビットです。パス内の全てのフォルダに x ビットが設定されていない場合、最後のファイルやディレクトリはパーミッションと関係なく、開くことができません。詳しくは man 7 path_resolution を参照。
s user の組の場合 setuid ビット。group の組の場合 setgid ビット。others の組には存在しません。x が設定されていることも示します。
S s と同じですが、x は設定されていません。通常のファイルで使われていることは滅多になく、フォルダでは意味がありません。
t スティッキービットothers の組でしか存在しません。x が設定されていることも示します。
T t と同じですが、x は設定されていません。通常のファイルで使われていることは滅多になく、フォルダでは意味がありません。

詳しくは info Coreutils -n "Mode Structure"man 1 chmod を参照。

サンプル

Let us see some examples to clarify:

drwx------ 6 archie users  4096 Jul  5 17:37 Documents

Archie has full access to the Documents directory. He can list, create files and rename, delete any file in Documents, regardless of file permissions. His ability to access a file depends on the file's permission.

dr-x------ 6 archie users  4096 Jul  5 17:37 Documents

Archie has full access except he can not create, rename, delete any file. He can list the files and (if file's permission empowers) may access an existing file in Documents.

d-wx------ 6 archie users  4096 Jul  5 17:37 Documents

Archie can not do 'ls' in Documents but if he knows the name of an existing file then he may list, rename, delete or (if file's permission empowers him) access it. Also, he is able to create new files.

d--x------ 6 archie users  4096 Jul  5 17:37 Documents

Archie is only capable of (if file's permission empowers him) access those files in Documents which he knows of. He can not list already existing files or create, rename, delete any of them.

You should keep in mind that we elaborate on directory permissions and it has nothing to do with the individual file permissions. When you create a new file it is the directory that changes. That is why you need write permission to the directory.

Let us look at another example, this time of a file, not a directory:

-rw-r--r-- 1 archie users  5120 Jun 27 08:28 foobar

Here we can see the first letter is not d but -. So we know it is a file, not a directory. Next the owner's permissions are rw- so the owner has the ability to read and write but not execute. This may seem odd that the owner does not have all three permissions, but the x permission is not needed as it is a text/data file, to be read by a text editor such as Gedit, EMACS, or software like R, and not an executable in its own right (if it contained something like python programming code then it very well could be). The group's permssions are set to r--, so the group has the ability to read the file but not write/edit it in any way — it is essentially like setting something to read-only. We can see that the same permissions apply to everyone else as well.

パーミッションを変更

chmod は Linux などの Unix ライクなオペレーティングシステムで使われるコマンドで、ファイルやディレクトリのパーミッション (もしくはアクセスモード) を変更することができます (change mode)。

テキストを使う方法

ファイルのパーミッション (あるいはアクセスモード) を変更するにはターミナルで chmod コマンドを使います。以下はコマンドの一般的な使い方です:

chmod who=permissions filename

who はパーミッションを与える対象を指定する文字です。以下のどれかになります:

  • u: ファイルを所有するユーザー
  • g: ファイルが所属するグループ
  • o: 他 (other) のユーザー。つまり、全てのユーザー。
  • a: 上記の全て (all)。ugo と入力する代わりに a で指定できます。

設定するパーミッションは #パーミッションの表示 で説明しているのと同じです (r, w, x)。

Now have a look at some examples using this command. Suppose you became very protective of the Documents directory and wanted to deny everybody but yourself, permissions to read, write, and execute (or in this case search/look) in it:

ビフォー: drwxr-xr-x 6 archie users 4096 Jul 5 17:37 Documents

$ chmod g= Documents
$ chmod o= Documents

アフター: drwx------ 6 archie users 4096 Jul 6 17:32 Documents

Here, because you want to deny permissions, you do not put any letters after the = where permissions would be entered. Now you can see that only the owner's permissions are rwx and all other permissions are -.

This can be reverted with:

Before: drwx------ 6 archie users 4096 Jul 6 17:32 Documents

$ chmod g=rx Documents
$ chmod o=rx Documents

After: drwxr-xr-x 6 archie users 4096 Jul 6 17:32 Documents

In the next example, you want to grant read and execute permissions to the group, and other users, so you put the letters for the permissions (r and x) after the =, with no spaces.

You can simplify this to put more than one who letter in the same command, e.g:

$ chmod go=rx Documents
ノート: It does not matter in which order you put the who letters or the permission letters in a chmod command: you could have chmod go=rx file or chmod og=xr file. It is all the same.

Now let us consider a second example, suppose you want to change a foobar file so that you have read and write permissions, and fellow users in the group users who may be colleagues working on foobar, can also read and write to it, but other users can only read it:

Before: -rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar

$ chmod g=rw foobar

After: -rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar

This is exactly like the first example, but with a file, not a directory, and you grant write permission (just so as to give an example of granting every permission).

テキストを使う方法のショートカット

The chmod command lets add and subtract permissions from an existing set using + or - instead of =. This is different from the above commands, which essentially re-write the permissions (e.g. to change a permission from r-- to rw-, you still need to include r as well as w after the = in the chmod command invocation. If you missed out r, it would take away the r permission as they are being re-written with the =. Using + and - avoids this by adding or taking away from the current set of permissions).

Let us try this + and - method with the previous example of adding write permissions to the group:

Before: -rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar

$ chmod g+w foobar

After: -rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar

Another example, denying write permissions to all (a):

Before: -rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar

$ chmod a-w foobar

After: -r--r--r-- 1 archie users 5120 Jun 27 08:28 foobar

A different shortcut is the special X mode: this is not an actual file mode, but it is often used in conjunction with the -R option to set the executable bit only for directories, and leave it unchanged for regular files, for example:

$ chmod -R a+rX ./data/

パーミッションのコピー

It is possible to tell chmod to copy the permissions from one class, say the owner, and give those same permissions to group or even all. To do this, instead of putting r, w, or x after the =, put another who letter. e.g:

Before: -rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar

$ chmod g=u foobar

After: -rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar

This command essentially translates to "change the permissions of group (g=), to be the same as the owning user (=u). Note that you cannot copy a set of permissions as well as grant new ones e.g.:

$ chmod g=wu foobar

In that case chmod throw an error.

数字を使う方法

chmod では数字を使ってパーミッションを設定することもできます。

数字を使用する場合、所有者とグループ、その他のユーザーのパーミッションを同時に編集することができます。コマンドは以下のようになります:

$ chmod xxx filename

xxx は3桁の数字になります。各桁の数字は0から7までのどれかになります。1番目の数字は所有者のパーミッション、2番目の数字は所有グループのパーミッション、3番目の数字は他のユーザーのパーミッションになります。

In this number notation, the values r, w, and x have their own number value:

r=4
w=2
x=1

To come up with a 3-digit number you need to consider what permissions you want owner, group, and user to have, and then total their values up. For example, if you want to grant the owner of a directory read write and execution permissions, and you want group and everyone else to have just read and execute permissions, you would come up with the numerical values like so:

  • Owner: rwx=4+2+1=7
  • Group: r-x=4+0+1=5
  • Other: r-x=4+0+1=5
$ chmod 755 filename

This is the equivalent of using the following:

$ chmod u=rwx filename
$ chmod go=rx filename

Most folders and directories are set to 755 to allow reading, writing and execution to the owner, but deny writing to everyone else, and files are normally 644 to allow reading and writing for the owner but just reading for everyone else; refer to the last note on the lack of x permissions with non executable files: it is the same thing here.

To see this in action with examples consider the previous example that has been used but with this numerical method applied instead:

Before: -rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar

$ chmod 664 foobar

After: -rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar

If this were an executable the number would be 774 if you wanted to grant executable permission to the owner and group. Alternatively if you wanted everyone to only have read permission the number would be 444. Treating r as 4, w as 2, and x as 1 is probably the easiest way to work out the numerical values for using chmod xxx filename, but there is also a binary method, where each permission has a binary number, and then that is in turn converted to a number. It is a bit more convoluted, but here included for completeness.

Consider this permission set:

-rwxr-xr--

If you put a 1 under each permission granted, and a 0 for every one not granted, the result would be something like this:

-rwxrwxr-x
 111111101

You can then convert these binary numbers:

000=0	    100=4
001=1	    101=5
010=2	    110=6
011=3	    111=7

The value of the above would therefore be 775.

Consider we wanted to remove the writable permission from group:

-rwzr-xr-x
 111101101

The value would therefore be 755 and you would use chmod 755 filename to remove the writable permission. You will notice you get the same three digit number no matter which method you use. Whether you use text or numbers will depend on personal preference and typing speed. When you want to restore a directory or file to default permissions e.g. read and write (and execute) permission to the owner but deny write permission to everyone else, it may be faster to use chmod 755/644 filename. However if you are changing the permissions to something out of the norm, it may be simpler and quicker to use the text method as opposed to trying to convert it to numbers, which may lead to a mistake. It could be argued that there is not any real significant difference in the speed of either method for a user that only needs to use chmod on occasion.

まとめて chmod

通常、ディレクトリとファイルは同じパーミッションにする必要はありません。一度にディレクトリツリーを修正しなくてはならない場合、find を使って選択的に修正します。

ディレクトリだけを 755 に chmod するには:

$ find directory -type d -exec chmod 755 {} +

ファイルだけを 644 に chmod するには:

$ find directory -type f -exec chmod 644 {} +

所有者を変更

chown はファイルやディレクトリの所有者を変更します。場合によってはパーミッションの変更よりも簡単です。

以下の例では、GParted を使ってバックアップデータ用に新しいパーティションを作成しています。Gparted は全てを root で実行するためファイルの所有者が全て root になります。ユーザーがパーティションをマウントしてデータを書き込もうとすると、パーミッションによって拒否されてしまいます。

brw-rw---- 1 root disk 8,    9 Jul  6 16:02 sda9
drwxr-xr-x 5 root root    4096 Jul  6 16:01 Backup

マウントされているディレクトリ (/media/Backup) と同じように、/dev のデバイスの所有者も root になっていることが確認できます。マウントされているディレクトリの所有者を変更するには以下を実行:

ビフォー: drwxr-xr-x 5 root root 4096 Jul 6 16:01 Backup

# chown archie /media/Backup

アフター: drwxr-xr-x 5 archie root 4096 Jul 6 16:01 Backup

Now the partition can have backup data written to it as instead of altering the permissions, as the owner already has rwx permissions, the owner has been altered to the user archie. Alternatives would be to alter the permissions for everyone else (undesirable as it is a backup permission) or adding the user to the group root.

アクセス制御リスト

アクセス制御リストはファイルシステムに柔軟なパーミッション機構を追加し、あらゆるユーザーやグループにあらゆるファイルのパーミッションを設定することができます。

アクセス制御リストがあるかどうかは ls コマンドの出力にプラス記号 (+) があるかで確認できます。

ファイルの属性

ユーザーやグループの読み書きや実行権限を制御するファイルモードビットとは別に、ファイル操作の可否を細かくカスタマイズするためのファイル属性をファイルシステムがサポートしている場合があります。このセクションではそれらの属性を使用する方法を説明します。

警告: デフォルトでは、cprsync などのプログラムはファイルの属性までコピーしません。

chattr と lsattr

ext2 や ext3 ファイルシステムでは、e2fsprogs パッケージに lsattrchattr というプログラムが含まれており、ファイルの属性を確認したり変更することができます。全てのファイルシステムで反映されるわけではありませんが、以下の属性を使用することができます:

  • a: 追記のみ
  • c: 圧縮
  • d: ダンプしない
  • e: 拡張フォーマット
  • i: 変更不可
  • j: データのジャーナリング
  • s: 安全な消去
  • t: 末尾のマージをしない
  • u: 削除不可
  • A: アクセス日時を更新しない
  • C: コピーオンライトしない
  • D: ディレクトリの同期更新
  • S: 同期更新
  • T: ディレクトリ階層のトップ

例えば、特定のファイルに変更不可ビットを設定したい場合、次のコマンドを使用:

# chattr +i /path/to/file

ファイルから属性を削除したいときは +- に変更してください。

拡張属性

attr(5) より: "拡張属性とはファイルやディレクトリに関連付けられる name:value のペアである。拡張属性には4つのクラスがある: security, system, trusted, user"。

警告: デフォルトでは、cprsync などのプログラムは拡張属性までコピーしません。

ユーザー拡張属性

ユーザー拡張属性を使うことでファイルに任意の情報を保存できます。ユーザー拡張属性を作成するには:

$ setfattr -n user.checksum -v "3baf9ebce4c664ca8d9e5f6314fb47fb" foo.bar

拡張属性を表示するには getfattr を使用:

$ getfattr -d foo.bar
# file: foo.bar
user.checksum="3baf9ebce4c664ca8d9e5f6314fb47fb"

ケイパビリティ

拡張属性はケイパビリティを設定するのにも使われます。

ヒントとテクニック

ルートの保護

--preserve-root フラグを使うことで chmod/ で再帰的に実行してしまうことを防ぐことができます。システム全体から実行可能属性を取り除いてシステムが破壊されてしまう恐れがなくなります。フラグを毎回使いたい場合、エイリアスで設定すると良いでしょう。[1] も参照。

参照