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

提供: ArchWiki
ナビゲーションに移動 検索に移動
(同期)
(2人の利用者による、間の5版が非表示)
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|ユーザーとグループ}}
8行目: 10行目:
 
{{Related articles end}}
 
{{Related articles end}}
   
  +
[[ファイルシステム]]は[[wikipedia:File system permissions|パーミッション]]や[[wikipedia:File attribute|属性]]を使ってシステムプロセスが処理できるファイルやディレクトリのレベルを規定します。
== パーミッションの表示 ==
 
   
  +
{{Warning|パーミッションや属性のセキュリティについて言えば、対抗できるのはシステムが起動した後の攻撃にすぎません。マシンに物理的にアクセスできる攻撃者からデータを保護するには、[[ディスク暗号化]]を施す必要があります。}}
chmod を使ってファイルやディレクトリのパーミッションを変更するときは、まず現在のアクセスモードを知る必要があります。知りたいディレクトリに "cd" してから次のコマンドを実行することでディレクトリの中身が確認できます:
 
   
  +
== パーミッションの表示 ==
$ ls -l
 
 
{{ic|-l}} スイッチを付けずに ''ls'' を実行するとディレクトリ内のファイルやフォルダの名前だけが表示されます。
 
   
  +
[[Core Utilities#ls|ls]] コマンドの {{ic|-l}} オプションを使ってディレクトリの中身のパーミッション (あるいは'''ファイルモード''') を確認できます。例:
以下はホームディレクトリで {{ic|ls -l}} を実行した場合の例です:
 
   
{{hc|$ ls -l|
+
{{hc|$ ls -l /path/to/directory|
 
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"
その次のカラムはファイルやディレクトリの容量をバイト数で示しており、その後にファイルやディレクトリを最後に更新した日付・時間が続き、そして最後にファイルやディレクトリの名前が表示されています。
 
  +
! 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}} ビットが設定されていない場合、最後のファイルやディレクトリはパーミッションと関係なく、開くことができません。詳しくは {{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"}} や {{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.
 
   
  +
いくつか例を見てみましょう:
==== フォルダ ====
 
   
  +
'''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 は Documents ディレクトリの完全なアクセス権を持っています。Archie は Documents に入っているファイルを確認したりファイルを作成・削除することが可能です。ファイルのパーミッションは関係ありません。ファイルにアクセスできるかどうかはファイルのパーミッションによって決まります。
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
 
   
  +
Archie はファイルを作成・削除したり名前を変更することができません。Documents の中にどんなファイルが入っているのか確認したり、(ファイルの読み込み権限があれば) 既存のファイルにアクセスすることは可能です。
# Ben 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.
 
'''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
 
   
  +
Archie は Documents の中を 'ls' することはできませんが、ファイルの名前がわかっていれば名前を変更したり削除したり (ファイルの読み込み権限があれば) アクセスすることはできます。また、新しいファイルを作ることも同様にできます。
# Ben 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.
 
'''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 a new file it is the directory that 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'''.}}
 
   
  +
Documents の中のファイルで Archie がアクセスできるのは名前を知っているファイルだけです (ファイルの読み込み権限が必要)。中に入っているファイルを一覧したり作成・削除することはできません。
==== ファイル ====
 
   
  +
ここで説明しているのはディレクトリのパーミッションであり、個々のファイルのパーミッションとは関係がありません。新しくファイルを作成したとき、変更があるのはディレクトリです。したがってディレクトリの書き込み権限が必要となります。
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
 
  +
今度はディレクトリではなくファイルの例を見てみましょう:
 
  +
'''- 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.
 
  +
  +
上記の例では最初の文字が {{ic|d}} ではなく {{ic|-}} になっていることが確認できます。最初の文字からディレクトリではなくファイルであることを知ることができます。次に所有者のパーミッションは {{ic|rw-}} なので所有者はファイルの読み書きができますが実行することはできません。所有者が3つのパーミッション全てを得ていないことはおかしく思えるかもしれませんが、{{ic|x}} パーミッションは Gedit や [[Emacs]] などのテキストエディタから読むテキストファイル、あるいは [[R]] などのソフトウェアから読み込むデータファイルでは必要ないパーミッションです (python のコードなどが入っている場合は実行権限が与えられることがあります)。グループのパーミッションは {{ic|r--}} に設定されており、所有グループはファイルを読むことはできても編集することができません。何かを読み取り専用に設定することは重要です。他のユーザーのパーミッションはグループと同じに設定されています。
   
== chmod コマンドでパーミッションを変更 ==
+
== パーミッションを変更 ==
   
 
[[Wikipedia:chmod|chmod]] は Linux などの Unix ライクなオペレーティングシステムで使われるコマンドで、ファイルやディレクトリのパーミッション (もしくはアクセスモード) を変更することができます (''ch''ange ''mod''e)。
 
[[Wikipedia:chmod|chmod]] は Linux などの Unix ライクなオペレーティングシステムで使われるコマンドで、ファイルやディレクトリのパーミッション (もしくはアクセスモード) を変更することができます (''ch''ange ''mod''e)。
108行目: 129行目:
 
=== テキストを使う方法 ===
 
=== テキストを使う方法 ===
   
  +
ファイルのパーミッション (あるいは''アクセスモード'') を変更するにはターミナルで ''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''').
 
   
  +
それではコマンドを使用例を見てみましょう。Documents ディレクトリを守るためにあなた以外のユーザーが読み書きや実行 (この場合は検索) ができないようにしたい場合:
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:
 
   
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}}
   
  +
上記ではパーミッションを取り去るために、{{ic|1==}} の後に何も文字を入力していません。所有者のパーミッションだけ {{ic|rwx}} となり、他のパーミッションは全て {{ic|-}} に変更されたことが確認できます。
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.
 
   
  +
上記の設定は以下のコマンドで元に戻すことができます:
This can be reverted with:
 
   
Before: drwx------ 6 ben users 4096 Jul 6 17:32 Documents
+
ビフォー: {{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
+
アフター: {{ic|drwxr-xr-x 6 archie users 4096 Jul 6 17:32 Documents}}
   
  +
グループと他のユーザーに読み込み・実行権限を与えるために、{{ic|1==}} の後にパーミッションの文字 ({{ic|r}} と {{ic|x}}) を記入しています。空白は入れません。
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.
 
   
  +
{{ic|''who''}} に複数の文字を入力することでコマンドをひとつにまとめることができます。例:
You can simplify this to put more than one '''who''' letter in the same command e.g:
 
   
chmod go=rx Documents
+
$ chmod go=rx Documents
   
  +
{{Note|1={{ic|chmod}} コマンドで {{ic|''who''}} に入力する文字やパーミッション文字の順番は特に意味をなしません: {{ic|1=chmod go=rx file}} でも {{ic|1=chmod og=xr file}} でも全く同じ意味になります。}}
{{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.}}
 
   
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).
   
 
==== テキストを使う方法のショートカット ====
 
==== テキストを使う方法のショートカット ====
   
  +
''chmod'' コマンドでは {{ic|1==}} の代わりに {{ic|+}} または {{ic|-}} を使うことでパーミッションを追加したり削除することができます。上記のコマンドのようにパーミッションを完全に書き換えることがありません (例えば、パーミッションを {{ic|r--}} から {{ic|rw-}} に変更する場合でも上記のコマンドを使うときは ''chmod'' コマンドの {{ic|1==}} の後に {{ic|w}} だけでなく {{ic|r}} も必要です。{{ic|r}} を指定しなかった場合、{{ic|r}} のパーミッションが消えてしまいます。{{ic|+}} と {{ic|-}} を使うことで現在のパーミッションから一部のパーミッションを追加したり削除することができるようになります)。
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).
 
   
  +
前記のグループに書き込みパーミッションを与える例を {{ic|+}} と {{ic|-}} で試してみましょう:
Let's try this '''+''' and '''-''' 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
+
ビフォー: {{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
+
アフター: {{ic|-rw-rw-r-- 1 archie users 5120 Jun 27 08:28 foobar}}
   
  +
また、全てのユーザー ('''a''') から書き込み権限を奪いたい場合:
Another example, denying write permissions to all ('''a'''):
 
   
Before: -rw-rw-r-- 1 ben users 5120 Jun 27 08:28 data
+
ビフォー: {{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
+
アフター: {{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行目: 236行目:
 
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
+
ビフォー: {{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
+
アフター: {{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行目: 277行目:
 
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'''
+
-rwxr-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.
  +
  +
数字を使う方法であれば数字を4個使って {{ic|setuid}}, {{ic|setgid}}, {{ic|sticky}} ビットも設定できます:
  +
  +
setuid=4
  +
setgid=2
  +
sticky=1
  +
  +
例えば {{ic|chmod 2777 ''filename''}} であれば誰でも読み書き実行ができ、{{ic|setgid}} ビットも有効になります。
   
 
=== まとめて chmod ===
 
=== まとめて chmod ===
275行目: 306行目:
 
$ 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: {{ic|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: {{ic|chown ben Backup (cd'd to /media first)}}
 
* After: {{ic|drwxr-xr-x 5 ben root 4096 Jul 6 16:01 Backup}}
 
   
  +
# chown archie /media/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'''.
 
  +
  +
アフター: {{ic|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 {{ic|rwx}} permissions).
  +
  +
{{Note|
  +
* {{ic|chown}} は常に setuid と setgid ビットを消去します。
  +
* root 以外のユーザーは {{ic|chown}} を使ってファイルの所有権を他のユーザーに変えることができません。
  +
}}
   
 
== アクセス制御リスト ==
 
== アクセス制御リスト ==
297行目: 335行目:
   
 
アクセス制御リストがあるかどうかは [[アクセス制御リスト#ls コマンドの出力|ls コマンドの出力]]にプラス記号 ({{ic|+}}) があるかで確認できます。
 
アクセス制御リストがあるかどうかは [[アクセス制御リスト#ls コマンドの出力|ls コマンドの出力]]にプラス記号 ({{ic|+}}) があるかで確認できます。
  +
  +
== Umask ==
  +
  +
[[umask]] ユーティリティを使うことでファイル作成モードマスクを制御することができ、新しくファイルを作成したときのファイルパーミッションの初期値を決めることができます。
   
 
== ファイルの属性 ==
 
== ファイルの属性 ==
302行目: 344行目:
 
[[ユーザーとグループ|ユーザーやグループ]]の読み書きや実行権限を制御するファイルモードビットとは別に、ファイル操作の可否を細かくカスタマイズするためのファイル属性を[[ファイルシステム]]がサポートしている場合があります。このセクションではそれらの属性を使用する方法を説明します。
 
[[ユーザーとグループ|ユーザーやグループ]]の読み書きや実行権限を制御するファイルモードビットとは別に、ファイル操作の可否を細かくカスタマイズするためのファイル属性を[[ファイルシステム]]がサポートしている場合があります。このセクションではそれらの属性を使用する方法を説明します。
   
  +
{{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.
+
{{man|5|attr}} より: "拡張属性とはファイルやディレクトリに関連付けられる 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:
 
  +
  +
{{hc|$ getfattr -d foo.bar|2=
  +
# file: foo.bar
  +
user.checksum="3baf9ebce4c664ca8d9e5f6314fb47fb"}}
  +
  +
=== ケイパビリティ ===
  +
  +
拡張属性は[[ケイパビリティ]]を設定するのにも使われます。
  +
  +
== ヒントとテクニック ==
  +
  +
=== ルートの保護 ===
   
  +
{{ic|--preserve-root}} フラグを使うことで {{ic|chmod}} を {{ic|/}} で再帰的に実行してしまうことを防ぐことができます。システム全体から実行可能属性を取り除いてシステムが破壊されてしまう恐れがなくなります。フラグを毎回使いたい場合、[[エイリアス]]で設定すると良いでしょう。[https://www.reddit.com/r/linux/comments/4ni3xe/tifu_sudo_chmod_644/] も参照。
$ getfattr -d foo.bar
 
# file: foo.bar
 
user.checksum="3baf9ebce4c664ca8d9e5f6314fb47fb"
 
   
 
== 参照 ==
 
== 参照 ==
352行目: 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] も参照。

参照