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

提供: ArchWiki
ナビゲーションに移動 検索に移動
(同期)
(同期)
(2人の利用者による、間の3版が非表示)
1行目: 1行目:
 
[[Category:ファイルシステム]]
 
[[Category:ファイルシステム]]
  +
[[Category:コマンドライン]]
 
[[en:File permissions and attributes]]
 
[[en:File permissions and attributes]]
  +
[[ru:File permissions and attributes]]
 
{{Related articles start}}
 
{{Related articles start}}
 
{{Related|ユーザーとグループ}}
 
{{Related|ユーザーとグループ}}
76行目: 78行目:
 
| style="text-align:center;" | {{ic|x}}
 
| style="text-align:center;" | {{ic|x}}
 
| ファイルを実行できます。
 
| ファイルを実行できます。
| ディレクトリを [[Core Utilities|cd]] で開くことができます。親のディレクトリから継承を行う唯一のパーミッションビットです。パス内の全てのフォルダに {{ic|x}} ビットが設定されていない場合、最後のファイルやディレクトリはパーミッションと関係なく、開くことができません。詳しくは {{ic|man 7 path_resolution}} を参照。
+
| ディレクトリを [[Core Utilities|cd]] で開くことができます。親のディレクトリから継承を行う唯一のパーミッションビットです。パス内の全てのフォルダに {{ic|x}} ビットが設定されていない場合、最後のファイルやディレクトリはパーミッションと関係なく、開くことができません。詳しくは {{man|7|path_resolution}} を参照。
 
|-
 
|-
 
| style="text-align:center;" | {{ic|s}}
 
| style="text-align:center;" | {{ic|s}}
91行目: 93行目:
 
|}
 
|}
   
詳しくは {{ic|info Coreutils -n "Mode Structure"}} や {{ic|man 1 chmod}} を参照。
+
詳しくは {{ic|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
 
'''drwx------''' 6 archie users 4096 Jul 5 17:37 Documents
   
  +
上記の場合 Archie は Documents ディレクトリの完全なアクセス権を持っています。Archie は 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
 
'''dr-x------''' 6 archie users 4096 Jul 5 17:37 Documents
   
  +
Archie はファイルを作成・削除したり名前を変更することができません。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
 
'''d-wx------''' 6 archie users 4096 Jul 5 17:37 Documents
   
  +
Archie は Documents の中を 'ls' することはできませんが、ファイルの名前がわかっていれば名前を変更したり削除したり (ファイルの読み込み権限があれば) アクセスすることはできます。また、新しいファイルを作ることも同様にできます。
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
 
'''d--x------''' 6 archie users 4096 Jul 5 17:37 Documents
   
  +
Documents の中のファイルで Archie がアクセスできるのは名前を知っているファイルだけです (ファイルの読み込み権限が必要)。中に入っているファイルを一覧したり作成・削除することはできません。
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
 
'''-rw-r--r--''' 1 archie users 5120 Jun 27 08:28 foobar
   
  +
上記の例では最初の文字が {{ic|d}} ではなく {{ic|-}} になっていることが確認できます。最初の文字からディレクトリではなくファイルであることを知ることができます。次に所有者のパーミッションは {{ic|rw-}} なので所有者はファイルの読み書きができますが実行することはできません。所有者が3つのパーミッション全てを得ていないことはおかしく思えるかもしれませんが、{{ic|x}} パーミッションは Gedit や [[Emacs]] などのテキストエディタから読むテキストファイル、あるいは [[R]] などのソフトウェアから読み込むデータファイルでは必要ないパーミッションです (python のコードなどが入っている場合は実行権限が与えられることがあります)。グループのパーミッションは {{ic|r--}} に設定されており、所有グループはファイルを読むことはできても編集することができません。何かを読み取り専用に設定することは重要です。他のユーザーのパーミッションはグループと同じに設定されています。
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.
 
   
 
== パーミッションを変更 ==
 
== パーミッションを変更 ==
140行目: 142行目:
 
設定するパーミッションは [[#パーミッションの表示]] で説明しているのと同じです ({{ic|r}}, {{ic|w}}, {{ic|x}})。
 
設定するパーミッションは [[#パーミッションの表示]] で説明しているのと同じです ({{ic|r}}, {{ic|w}}, {{ic|x}})。
   
  +
それではコマンドを使用例を見てみましょう。Documents ディレクトリを守るためにあなた以外のユーザーが読み書きや実行 (この場合は検索) ができないようにしたい場合:
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:
 
   
 
ビフォー: {{ic|drwxr-xr-x 6 archie users 4096 Jul 5 17:37 Documents}}
 
ビフォー: {{ic|drwxr-xr-x 6 archie users 4096 Jul 5 17:37 Documents}}
149行目: 151行目:
 
アフター: {{ic|drwx------ 6 archie users 4096 Jul 6 17:32 Documents}}
 
アフター: {{ic|drwx------ 6 archie users 4096 Jul 6 17:32 Documents}}
   
  +
上記ではパーミッションを取り去るために、{{ic|1==}} の後に何も文字を入力していません。所有者のパーミッションだけ {{ic|rwx}} となり、他のパーミッションは全て {{ic|-}} に変更されたことが確認できます。
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:
 
   
Before: {{ic|drwx------ 6 archie users 4096 Jul 6 17:32 Documents}}
+
ビフォー: {{ic|drwx------ 6 archie users 4096 Jul 6 17:32 Documents}}
   
 
$ chmod g=rx Documents
 
$ chmod g=rx Documents
 
$ chmod o=rx Documents
 
$ chmod o=rx Documents
   
After: {{ic|drwxr-xr-x 6 archie users 4096 Jul 6 17:32 Documents}}
+
アフター: {{ic|drwxr-xr-x 6 archie users 4096 Jul 6 17:32 Documents}}
   
  +
グループと他のユーザーに読み込み・実行権限を与えるために、{{ic|1==}} の後にパーミッションの文字 ({{ic|r}} と {{ic|x}}) を記入しています。空白は入れません。
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.
 
   
  +
{{ic|''who''}} に複数の文字を入力することでコマンドをひとつにまとめることができます。例:
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 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.}}
+
{{Note|1={{ic|chmod}} コマンドで {{ic|''who''}} に入力する文字やパーミッション文字の順番は特に意味をなしません: {{ic|1=chmod go=rx file}} でも {{ic|1=chmod og=xr file}} でも全く同じ意味になります。}}
   
 
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:
 
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:
180行目: 182行目:
 
==== テキストを使う方法のショートカット ====
 
==== テキストを使う方法のショートカット ====
   
  +
''chmod'' コマンドでは {{ic|1==}} の代わりに {{ic|+}} または {{ic|-}} を使うことでパーミッションを追加したり削除することができます。上記のコマンドのようにパーミッションを完全に書き換えることがありません (例えば、パーミッションを {{ic|r--}} から {{ic|rw-}} に変更する場合でも上記のコマンドを使うときは ''chmod'' コマンドの {{ic|1==}} の後に {{ic|w}} だけでなく {{ic|r}} も必要です。{{ic|r}} を指定しなかった場合、{{ic|r}} のパーミッションが消えてしまいます。{{ic|+}} と {{ic|-}} を使うことで現在のパーミッションから一部のパーミッションを追加したり削除することができるようになります)。
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).
 
   
  +
前記のグループに書き込みパーミッションを与える例を {{ic|+}} と {{ic|-}} で試してみましょう:
Let us try this {{ic|+}} and {{ic|-}} method with the previous example of adding write permissions to the group:
 
   
Before: {{ic|-rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar}}
+
ビフォー: {{ic|-rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar}}
 
 
 
$ chmod g+w foobar
 
$ chmod g+w foobar
   
After: {{ic|-rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar}}
+
アフター: {{ic|-rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar}}
   
  +
また、全てのユーザー ('''a''') から書き込み権限を奪いたい場合:
Another example, denying write permissions to all ('''a'''):
 
   
Before: {{ic|-rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar}}
+
ビフォー: {{ic|-rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar}}
 
 
 
$ chmod a-w foobar
 
$ chmod a-w foobar
   
After: {{ic|-r--r--r-- 1 archie users 5120 Jun 27 08:28 foobar}}
+
アフター: {{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:
 
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:
251行目: 253行目:
 
To see this in action with examples consider the previous example that has been used 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: {{ic|-rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar}}
+
ビフォー: {{ic|-rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar}}
   
 
$ chmod 664 foobar
 
$ chmod 664 foobar
   
After: {{ic|-rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar}}
+
アフター: {{ic|-rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar}}
   
 
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.
 
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.
279行目: 281行目:
 
Consider we wanted to remove the writable permission from group:
 
Consider we wanted to remove the writable permission from group:
   
-rwzr-xr-x
+
-rwxr-xr-x
 
111101101
 
111101101
   
 
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.
 
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.
  +
  +
数字を使う方法であれば数字を4個使って {{ic|setuid}}, {{ic|setgid}}, {{ic|sticky}} ビットも設定できます:
  +
  +
setuid=4
  +
setgid=2
  +
sticky=1
  +
  +
例えば {{ic|chmod 2777 ''filename''}} であれば誰でも読み書き実行ができ、{{ic|setgid}} ビットも有効になります。
   
 
=== まとめて chmod ===
 
=== まとめて chmod ===
300行目: 310行目:
 
[[Wikipedia:chown|chown]] はファイルやディレクトリの所有者を変更します。場合によってはパーミッションの変更よりも簡単です。
 
[[Wikipedia:chown|chown]] はファイルやディレクトリの所有者を変更します。場合によってはパーミッションの変更よりも簡単です。
   
以下の例では、[[GParted]] を使ってバックアップデータ用に新しいパーティションを作成しています。Gparted は全てを root で実行するためファイルの所有者が全て root になります。ユーザーがパーティションをマウントしてデータを書き込もうとすると、パーミッションによって拒否されてしまいます。
+
以下の例では、[[GParted]] を使ってバックアップデータ用に新しいパーティションを作成しています。Gparted は全てを root で実行するためファイルの所有者が全て root になります。通常ユーザーがパーティションをマウントしてデータを書き込もうとすると、パーミッションによって拒否されてしまいます。
   
 
brw-rw---- 1 root disk 8, 9 Jul 6 16:02 sda9
 
brw-rw---- 1 root disk 8, 9 Jul 6 16:02 sda9
313行目: 323行目:
 
アフター: {{ic|drwxr-xr-x 5 archie 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 {{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}}.
+
Now the partition can have data written to it by the new owner, archie, without altering the permissions (as the owner triad already had {{ic|rwx}} permissions).
  +
  +
{{Note|
  +
* {{ic|chown}} は常に setuid と setgid ビットを消去します。
  +
* root 以外のユーザーは {{ic|chown}} を使ってファイルの所有権を他のユーザーに変えることができません。
  +
}}
   
 
== アクセス制御リスト ==
 
== アクセス制御リスト ==
320行目: 335行目:
   
 
アクセス制御リストがあるかどうかは [[アクセス制御リスト#ls コマンドの出力|ls コマンドの出力]]にプラス記号 ({{ic|+}}) があるかで確認できます。
 
アクセス制御リストがあるかどうかは [[アクセス制御リスト#ls コマンドの出力|ls コマンドの出力]]にプラス記号 ({{ic|+}}) があるかで確認できます。
  +
  +
== Umask ==
  +
  +
[[umask]] ユーティリティを使うことでファイル作成モードマスクを制御することができ、新しくファイルを作成したときのファイルパーミッションの初期値を決めることができます。
   
 
== ファイルの属性 ==
 
== ファイルの属性 ==
354行目: 373行目:
 
== 拡張属性 ==
 
== 拡張属性 ==
   
{{ic|attr(5)}} より: "拡張属性とはファイルやディレクトリに関連付けられる name:value のペアである。拡張属性には4つのクラスがある: security, system, trusted, user"。
+
{{man|5|attr}} より: "拡張属性とはファイルやディレクトリに関連付けられる name:value のペアである。拡張属性には4つのクラスがある: security, system, trusted, user"。
   
 
{{Warning|デフォルトでは、[[Core Utilities|cp]] や [[rsync]] などのプログラムは拡張属性までコピーしません。}}
 
{{Warning|デフォルトでは、[[Core Utilities|cp]] や [[rsync]] などのプログラムは拡張属性までコピーしません。}}
366行目: 385行目:
 
拡張属性を表示するには getfattr を使用:
 
拡張属性を表示するには getfattr を使用:
   
$ getfattr -d foo.bar
+
{{hc|$ getfattr -d foo.bar|2=
# file: foo.bar
+
# file: foo.bar
user.checksum="3baf9ebce4c664ca8d9e5f6314fb47fb"
+
user.checksum="3baf9ebce4c664ca8d9e5f6314fb47fb"}}
   
 
=== ケイパビリティ ===
 
=== ケイパビリティ ===
374行目: 393行目:
 
拡張属性は[[ケイパビリティ]]を設定するのにも使われます。
 
拡張属性は[[ケイパビリティ]]を設定するのにも使われます。
   
  +
== ヒントとテクニック ==
== Tips and tricks ==
 
   
 
=== ルートの保護 ===
 
=== ルートの保護 ===
385行目: 404行目:
 
* [http://www.hackinglinuxexposed.com/articles/20030417.html Linux File Permission Confusion]
 
* [http://www.hackinglinuxexposed.com/articles/20030417.html Linux File Permission Confusion]
 
* [http://www.hackinglinuxexposed.com/articles/20030424.html Linux File Permission Confusion part 2]
 
* [http://www.hackinglinuxexposed.com/articles/20030424.html Linux File Permission Confusion part 2]
* [[wikipedia:Extended file attributes#Linux]]
+
* [[wikipedia:ja:拡張ファイル属性#Linux]]
 
* [http://www.lesbonscomptes.com/pages/extattrs.html Extended attributes: the good, the not so good, the bad.]
 
* [http://www.lesbonscomptes.com/pages/extattrs.html Extended attributes: the good, the not so good, the bad.]
 
* [http://www.concrete5.org/documentation/how-tos/designers/backup-and-restore-file-permissions-in-linux/ Backup and restore file permissions in Linux]
 
* [http://www.concrete5.org/documentation/how-tos/designers/backup-and-restore-file-permissions-in-linux/ Backup and restore file permissions in Linux]
  +
* [https://serverfault.com/questions/364677/why-is-chmod-r-777-destructive Why is “chmod -R 777 /” destructive?]

2018年5月1日 (火) 15:09時点における版

関連記事

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

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

パーミッションの表示

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 ビットが設定されていない場合、最後のファイルやディレクトリはパーミッションと関係なく、開くことができません。詳しくは path_resolution(7) を参照。
s user の組の場合 setuid ビット。group の組の場合 setgid ビット。others の組には存在しません。x が設定されていることも示します。
S s と同じですが、x は設定されていません。通常のファイルで使われていることは滅多になく、フォルダでは意味がありません。
t スティッキービットothers の組でしか存在しません。x が設定されていることも示します。
T t と同じですが、x は設定されていません。通常のファイルで使われていることは滅多になく、フォルダでは意味がありません。

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

サンプル

いくつか例を見てみましょう:

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

上記の場合 Archie は Documents ディレクトリの完全なアクセス権を持っています。Archie は Documents に入っているファイルを確認したりファイルを作成・削除することが可能です。ファイルのパーミッションは関係ありません。ファイルにアクセスできるかどうかはファイルのパーミッションによって決まります。

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

Archie はファイルを作成・削除したり名前を変更することができません。Documents の中にどんなファイルが入っているのか確認したり、(ファイルの読み込み権限があれば) 既存のファイルにアクセスすることは可能です。

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

Archie は Documents の中を 'ls' することはできませんが、ファイルの名前がわかっていれば名前を変更したり削除したり (ファイルの読み込み権限があれば) アクセスすることはできます。また、新しいファイルを作ることも同様にできます。

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

Documents の中のファイルで Archie がアクセスできるのは名前を知っているファイルだけです (ファイルの読み込み権限が必要)。中に入っているファイルを一覧したり作成・削除することはできません。

ここで説明しているのはディレクトリのパーミッションであり、個々のファイルのパーミッションとは関係がありません。新しくファイルを作成したとき、変更があるのはディレクトリです。したがってディレクトリの書き込み権限が必要となります。

今度はディレクトリではなくファイルの例を見てみましょう:

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

上記の例では最初の文字が d ではなく - になっていることが確認できます。最初の文字からディレクトリではなくファイルであることを知ることができます。次に所有者のパーミッションは rw- なので所有者はファイルの読み書きができますが実行することはできません。所有者が3つのパーミッション全てを得ていないことはおかしく思えるかもしれませんが、x パーミッションは Gedit や Emacs などのテキストエディタから読むテキストファイル、あるいは R などのソフトウェアから読み込むデータファイルでは必要ないパーミッションです (python のコードなどが入っている場合は実行権限が与えられることがあります)。グループのパーミッションは r-- に設定されており、所有グループはファイルを読むことはできても編集することができません。何かを読み取り専用に設定することは重要です。他のユーザーのパーミッションはグループと同じに設定されています。

パーミッションを変更

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

テキストを使う方法

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

chmod who=permissions filename

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

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

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

それではコマンドを使用例を見てみましょう。Documents ディレクトリを守るためにあなた以外のユーザーが読み書きや実行 (この場合は検索) ができないようにしたい場合:

ビフォー: 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

上記ではパーミッションを取り去るために、= の後に何も文字を入力していません。所有者のパーミッションだけ rwx となり、他のパーミッションは全て - に変更されたことが確認できます。

上記の設定は以下のコマンドで元に戻すことができます:

ビフォー: drwx------ 6 archie users 4096 Jul 6 17:32 Documents

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

アフター: drwxr-xr-x 6 archie users 4096 Jul 6 17:32 Documents

グループと他のユーザーに読み込み・実行権限を与えるために、= の後にパーミッションの文字 (rx) を記入しています。空白は入れません。

who に複数の文字を入力することでコマンドをひとつにまとめることができます。例:

$ chmod go=rx Documents
ノート: chmod コマンドで who に入力する文字やパーミッション文字の順番は特に意味をなしません: chmod go=rx file でも chmod og=xr file でも全く同じ意味になります。

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).

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

chmod コマンドでは = の代わりに + または - を使うことでパーミッションを追加したり削除することができます。上記のコマンドのようにパーミッションを完全に書き換えることがありません (例えば、パーミッションを r-- から rw- に変更する場合でも上記のコマンドを使うときは chmod コマンドの = の後に w だけでなく r も必要です。r を指定しなかった場合、r のパーミッションが消えてしまいます。+- を使うことで現在のパーミッションから一部のパーミッションを追加したり削除することができるようになります)。

前記のグループに書き込みパーミッションを与える例を +- で試してみましょう:

ビフォー: -rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar

$ chmod g+w foobar

アフター: -rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar

また、全てのユーザー (a) から書き込み権限を奪いたい場合:

ビフォー: -rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar

$ chmod a-w foobar

アフター: -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:

ビフォー: -rw-r--r-- 1 archie users 5120 Jun 27 08:28 foobar

$ chmod 664 foobar

アフター: -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:

-rwxr-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.

数字を使う方法であれば数字を4個使って setuid, setgid, sticky ビットも設定できます:

setuid=4
setgid=2
sticky=1

例えば chmod 2777 filename であれば誰でも読み書き実行ができ、setgid ビットも有効になります。

まとめて 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 data written to it by the new owner, archie, without altering the permissions (as the owner triad already had rwx permissions).

ノート:
  • chown は常に setuid と setgid ビットを消去します。
  • root 以外のユーザーは chown を使ってファイルの所有権を他のユーザーに変えることができません。

アクセス制御リスト

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

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

Umask

umask ユーティリティを使うことでファイル作成モードマスクを制御することができ、新しくファイルを作成したときのファイルパーミッションの初期値を決めることができます。

ファイルの属性

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

警告: デフォルトでは、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] も参照。

参照