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

提供: ArchWiki
ナビゲーションに移動 検索に移動
(ページの作成:「Category:ファイルシステム en:File permissions and attributes {{Related articles start}} {{Related|ユーザーとグループ}} {{Related|umask}} {{Relate...」)
 
(TranslationStatus)
 
(5人の利用者による、間の35版が非表示)
1行目: 1行目:
 
[[Category:ファイルシステム]]
 
[[Category:ファイルシステム]]
  +
[[Category:コマンドライン]]
 
[[en:File permissions and attributes]]
 
[[en:File permissions and attributes]]
  +
[[es:File permissions and attributes]]
  +
[[pt:File permissions and attributes]]
  +
[[ru:File permissions and attributes]]
  +
[[zh-hans:File permissions and attributes]]
 
{{Related articles start}}
 
{{Related articles start}}
 
{{Related|ユーザーとグループ}}
 
{{Related|ユーザーとグループ}}
 
{{Related|umask}}
 
{{Related|umask}}
{{Related3|Access Control Lists|アクセス制御リスト}}
+
{{Related|アクセス制御リスト}}
{{Related3|Capabilities|ケイパビリティ}}
+
{{Related|ケイパビリティ}}
  +
{{Related|拡張属性}}
 
{{Related articles end}}
 
{{Related articles end}}
   
  +
[[ファイルシステム]]は、[[Wikipedia:ja:ファイルパーミッション|パーミッション]]と[[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
 
   
  +
[[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 archie 4096 Jul 5 21:03 Desktop
drwxr-xr-x 2 ben users 4096 Jul 5 21:03 Desktop
+
drwxr-xr-x 6 archie archie 4096 Jul 5 17:37 Documents
drwxr-xr-x 6 ben users 4096 Jul 5 17:37 Documents
+
drwxr-xr-x 2 archie archie 4096 Jul 5 13:45 Downloads
  +
-rw-rw-r-- 1 archie archie 5120 Jun 27 08:28 customers.ods
drwxr-xr-x 2 ben users 4096 Jul 5 13:45 Downloads
 
drwxr-xr-x 2 ben users 4096 Jun 24 03:36 Movies
+
-rw-r--r-- 1 archie archie 3339 Jun 27 08:28 todo
drwxr-xr-x 2 ben users 4096 Jun 24 03:38 Music
+
-rwxr-xr-x 1 archie archie 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~
 
 
}}
 
}}
   
  +
1番目の列に注目してください。例えば {{ic|drwxrwxrwx+}} という値であれば、それぞれの文字の意味は以下の様のようになります:
=== カラムの意味 ===
 
   
  +
{| class="wikitable"
The first column is the type of each file:
 
  +
|- style="text-align:center;"
* '''-''' denotes a normal file.
 
  +
| {{ic|d}}
* '''d''' denotes a directory, i.e. a folder containing other files or folders.
 
  +
| {{ic|rwx}}
* '''p''' denotes a named pipe (aka FIFO).
 
  +
| {{ic|rwx}}
* '''l''' denotes a symbolic link.
 
  +
| {{ic|rwx}}
  +
| {{ic|+}}
  +
|-
  +
| ファイルの種類。技術的にはパーミッションの一部ではありません。ここに入りうる値の説明は {{ic|info ls -n "What information is listed"}} を参照してください。
  +
| このファイルに対して所有者が持っているパーミッション。以下で説明されています。
  +
| このファイルに対してグループが持っているパーミッション。以下で説明されています。
  +
| このファイルに対してその他のユーザが持っているパーミッション。以下で説明されています。
  +
| 代替のアクセス手法がこのファイルに対して適用されているかどうかを示す単一の文字。この文字が空白ならば、代替のアクセス手法は適用されていません。{{ic|.}} 文字の場合は、ファイルにセキュリティコンテキストが存在しているが、代替のアクセス手法は適用されていないことを示します。代替のアクセス手法がその他の組み合わせで適用されているファイルでは、{{ic|+}} 文字で表されます。例えば、[[アクセス制御リスト]]の場合です。
  +
|}
   
  +
3つの文字からなるパーミッション (上記の例では {{ic|rwx}}) には、以下の文字のどれかが入ります:
The letters after that are the permissions, this first column is what we will be most interested in. The second one is how many links there are in a file, we can safely ignore it. The third column has two values/names: The first one (in my example 'ben') is the name of the user that owns the file. The second value ('users' in the example) is the '''group''' that the owner belongs to (Read more about [[groups]]).
 
   
  +
{| 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;" | 書き込みパーミッション (2番目の文字)
  +
| style="text-align:center;" | {{ic|-}}
  +
| ファイルは変更できません。
  +
| ディレクトリの中身は変更できません。
  +
|-
  +
| style="text-align:center;" | {{ic|w}}
  +
| ファイルを変更できます。
  +
| ディレクトリの中身を変更できます (ここでの変更とは、新しいファイルやディレクトリの作成、既存のファイルやディレクトリの名称変更や削除です)。ただし、実行パーミッションも設定されていなければなりません。さもないと、このパーミッションは効果を持ちません。
  +
|-
  +
! rowspan="6" style="text-align:left;" | 実行パーミッション (3番目の文字)
  +
| style="text-align:center;" | {{ic|-}}
  +
| ファイルは実行できません。
  +
| ディレクトリは [[cd]] でアクセスできません。
  +
|-
  +
| style="text-align:center;" | {{ic|x}}
  +
| ファイルを実行できます。
  +
| ディレクトリに [[cd]] でアクセスできます。これは、親ディレクトリから事実上 "継承している" とみなすことのできる唯一のパーミッションビットです。事実、親ディレクトリの''どれか1つでも'' {{ic|x}} が設定されていないと、パスの最後のファイルやディレクトリにも (それのパーミッションに依らず) アクセスできません。詳細は {{man|7|path_resolution}} を参照してください。
  +
|-
  +
| style="text-align:center;" | {{ic|s}}
  +
| colspan="2" | ユーザ ('''u'''ser) のパーミッションにある場合は [[w:ja: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" | [[w: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}} を見てください。
=== パーミッションの意味 ===
 
   
  +
{{Tip|{{ic|namei -l ''path''}} を使えば、パスと一緒にパーミッションを表示できます。}}
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.
 
   
==== フォ ====
+
=== サンプル ===
   
  +
いくつか例を見て、理解を深めましょう:
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.
 
   
  +
'''drwx------''' 6 archie archie 4096 Jul 5 17:37 Documents
Let's see some examples to clarify, taking one directory from above:
 
   
  +
上記の場合、Archie は {{ic|Documents}} ディレクトリへの完全なアクセス権を持っています。Archie は、Documents 内のファイルを (そのファイルのパーミッションに依らず) 一覧表示、名称変更、削除することができ、さらにファイルを 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
 
   
  +
'''dr-x------''' 6 archie archie 4096 Jul 5 17:37 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
 
   
  +
Archie はファイルの作成、名称変更、削除ができませんが、それ以外なら可能です。Archie は Documents 内のファイルを一覧表示することができ、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
 
   
  +
'''d-wx------''' 6 archie archie 4096 Jul 5 17:37 Documents
# 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
 
}}
 
   
  +
Archie は {{ic|Documents}} ディレクトリ内で {{ic|ls}} を実行することができませんが、ディレクトリ内の特定のファイルの名前を知っているならば、そのファイルを一覧表示、名称変更、削除することができ、さらに (そのファイルのパーミッションで許可されているならば) アクセスすることができます。また、新しいファイルを作成することもできます。
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'''.}}
 
   
  +
'''d--x------''' 6 archie archie 4096 Jul 5 17:37 Documents
==== ファイル ====
 
   
  +
Archie は、{{ic|Documents}} ディレクトリ内のファイルのうち、名前を知っているものにアクセスすることしかできません (ただし、そのファイルのパーミッションで許可されている場合に限る)。ディレクトリ内のファイルを一覧表示することはできず、ファイルの作成、名称変更、削除もできません。
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)
 
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.
 
   
  +
ここで説明しているのはディレクトリのパーミッションであり、個々のファイルのパーミッションとは関係がありません。新しくファイルを作成したとき、変更があるのはディレクトリです。したがってディレクトリの書き込み権限が必要となります。
== chmod コマンドでパーミッションを変更 ==
 
   
  +
今度はディレクトリではなくファイルの例を見てみましょう:
[[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.
 
   
  +
'''-rw-r--r--''' 1 archie web 5120 Jun 27 08:28 foobar
=== テキストを使う方法 ===
 
   
  +
上記の例では最初の文字が {{ic|d}} ではなく {{ic|-}} になっていることが確認できます。なので、これはディレクトリではなくファイルであることを知ることができます。次に所有者のパーミッションは {{ic|rw-}} なので所有者はファイルの読み書きができますが実行することはできません。所有者が3つのパーミッション全てを得ていないことはおかしく思えるかもしれませんが、{{ic|x}} パーミッションは Gedit や [[Emacs]] などのテキストエディタから読むテキストファイル、あるいは [[R]] などのソフトウェアから読み込むデータファイルでは必要ないパーミッションです (python のコードなどが入っている場合は実行権限が与えられることがあります)。グループのパーミッションは {{ic|r--}} に設定されており、所有グループはファイルを読むことはできても編集することができません。何かを読み取り専用に設定することは重要です。他のユーザーのパーミッションはグループと同じに設定されています。
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''
 
   
  +
[[Wikipedia:ja:chmod|chmod]] は Linux などの Unix ライクなオペレーティングシステムで使われるコマンドで、ファイルやディレクトリのパーミッション (アクセスモード (''mod''e) とも) を変更 (''ch''ange) することができます。
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:
 
   
  +
=== 文字を使う方法 ===
u - The '''u'''ser that own the file.
 
g - The '''g'''roup the file belongs to.
 
o - The '''o'''ther users i.e. everyone else.
 
a - '''a'''ll of the above - use this instead of having to type '''ugo'''.
 
   
  +
ファイルのパーミッション (''アクセスモード'' とも) を変更するにはターミナルで ''chmod'' コマンドを使います。以下はコマンドの一般的な使い方です:
The permissions are the same as already discussed ('''r, w,''' and '''x''').
 
   
  +
chmod ''who''=''パーミッション'' ''ファイル名''
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:
 
   
  +
{{ic|''who''}} は、パーミッションを与える対象を指定する文字です。以下のどれかになります:
Before: drwxr-xr-x 6 ben users 4096 Jul 5 17:37 Documents
 
   
  +
* {{ic|u}}: そのファイルを所有する[[ユーザー]]。
Command 1: chmod g= Documents
 
  +
* {{ic|g}}: そのファイルが所属する[[ユーザーグループ]]。
Command 2: chmod o= Documents
 
  +
* {{ic|o}}: その他 ('''o'''ther) のユーザー。つまり、その他全員。
  +
* {{ic|a}}: 上記全て。{{ic|ugo}} と入力する代わりとして使えます。
   
  +
ここでのパーミッションとは、[[#パーミッションの表示]] で説明しているのと同じです ({{ic|r}}、{{ic|w}}、{{ic|x}})。
After: drwx------ 6 ben users 4096 Jul 6 17:32 Documents
 
   
  +
それではコマンドを使用例を見てみましょう。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.
 
   
  +
前: {{ic|drwxr-xr-x 6 archie web 4096 Jul 5 17:37 Documents}}
This can be reverted with:
 
   
Before: drwx------ 6 ben users 4096 Jul 6 17:32 Documents
+
$ chmod g= Documents
  +
$ chmod o= Documents
   
  +
後: {{ic|drwx------ 6 archie web 4096 Jul 6 17:32 Documents}}
Command 1: chmod g=rx Documents
 
Command 2: chmod o=rx Documents
 
   
  +
上記ではパーミッションを取り去るために、{{ic|1==}} の後に何も文字を入力していません。所有者のパーミッションだけ {{ic|rwx}} となり、他のパーミッションは全て {{ic|-}} に変更されたことが確認できます。
After: drwxr-xr-x 6 ben 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.
 
   
  +
前: {{ic|drwx------ 6 archie web 4096 Jul 6 17:32 Documents}}
You can simplify this to put more than one '''who''' letter in the same command e.g:
 
   
chmod go=rx Documents
+
$ chmod g=rx Documents
  +
$ chmod o=rx Documents
   
  +
後: {{ic|drwxr-xr-x 6 archie web 4096 Jul 6 17:32 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.}}
 
   
  +
次の例では、グループとその他のユーザに対して読み込みと実行のパーミッションを与えてみます。つまり、{{ic|1==}} の後にパーミッションの文字 ({{ic|r}} と {{ic|x}}) を入れます (スペース無しで)。
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:
 
   
  +
{{ic|''who''}} に複数の文字を入力することで、これを行うことができます。例えば:
Before: -rw-r--r-- 1 ben users 5120 Jun 27 08:28 data
 
   
Command1: chmod g=rw data
+
$ chmod go=rx Documents
   
  +
{{Note|{{ic|chmod}} コマンドで {{ic|''who''}} に入力する文字やパーミッション文字の順番は特に意味をなしません: {{ic|1=chmod go=rx file}} でも {{ic|1=chmod og=xr file}} でも全く同じ意味になります。}}
After: -rw-rw-r-- 1 ben users 5120 Jun 27 08:28 data
 
   
  +
次に、2番目の例を考えてみましょう。たとえば、{{ic|foobar}} ファイルを変更して、読み取りと書き込みのアクセス許可を与え、{{ic|web}} グループの同僚が作業している可能性があると仮定します。{{ic|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).
 
   
  +
前: {{ic|-rw-r--r-- 1 archie web 5120 Jun 27 08:28 foobar}}
==== テキストを使う方法のショートカット ====
 
   
  +
$ chmod g=rw foobar
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|-rw-rw-r-- 1 archie web 5120 Jun 27 08:28 foobar}}
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
 
  +
  +
==== 文字を使う方法のショートカット ====
  +
  +
''chmod'' コマンドでは {{ic|1==}} の代わりに {{ic|+}} または {{ic|-}} を使うことでパーミッションを追加したり削除することができます。上記のコマンドのようにパーミッションを完全に書き換えることがありません (例えば、パーミッションを {{ic|r--}} から {{ic|rw-}} に変更する場合でも上記のコマンドを使うときは ''chmod'' コマンドの {{ic|1==}} の後に {{ic|w}} だけでなく {{ic|r}} も必要です。{{ic|r}} を指定しなかった場合、{{ic|r}} のパーミッションが消えてしまいます。{{ic|+}} と {{ic|-}} を使うことで現在のパーミッションから一部のパーミッションを追加したり削除することができるようになります)。
  +
  +
前記のグループに書き込みパーミッションを与える例を {{ic|+}} と {{ic|-}} で試してみましょう:
  +
  +
前: {{ic|-rw-r--r-- 1 archie web 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 web 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 web 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 web 5120 Jun 27 08:28 foobar}}
  +
  +
別のショートカットは特殊な {{ic|X}} モードです。これは実際のファイルモードではありませんが、多くの場合、{{ic|-R}} オプションと組み合わせて使用され、ディレクトリに対してのみ実行可能ビットを設定し、通常のファイルに対しては変更しないでおきます。たとえば、次のようになります:
  +
  +
$ chmod -R a+rX ./data/
   
 
==== パーミッションのコピー ====
 
==== パーミッションのコピー ====
   
  +
''chmod'' で、あるクラスから別のクラスにパーミッションをコピーすることも可能です (例えば、所有者のパーミッションをグループにコピーする)。そうするには、{{ic|1==}} の後に、{{ic|r}} や {{ic|w}}、{{ic|x}} ではなく、もう一つの ''who'' 文字を入れてください。例えば:
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:
 
   
Before: -rw-r--r-- 1 ben users 5120 Jun 27 08:28 data
+
: {{ic|-rw-r--r-- 1 archie web 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
+
: {{ic|-rw-rw-r-- 1 archie web 5120 Jun 27 08:28 foobar}}
   
  +
このコマンドは基本的に、"グループのアクセス許可を変更 ({{ic|1=g=}}) して、所有ユーザー ({{ic|1==u}}) と同じにします。ただし、パーミッションのセットをコピーして、なおかつ新しいパーミッションを付与するというようなことはできません。例えば:
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 data
+
$ chmod g=wu foobar
   
  +
この場合、''chmod'' はエラーを吐きます。
In that case, {{ic|chmod}} will have a small fit and throw you an error.
 
   
 
=== 数字を使う方法 ===
 
=== 数字を使う方法 ===
   
  +
''chmod'' では数字を使ってパーミッションを設定することもできます。
chmod can also set permissions using numbers.
 
   
  +
数字を使えば、所有者、グループ、そしてその他のユーザのパーミッション全て、さらに setuid、setgid、そしてスティッキービットも一度に設定できます。基本的な使い方は以下のとおりです:
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'' ''ファイル名''
   
  +
ここで、{{ic|'''xxx'''}} は3桁の数字で、各桁は0から7までのどれかです。最初の桁は所有者のパーミッション、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.
 
   
  +
この数字表記では、{{ic|r}}、{{ic|w}}、そして {{ic|x}} はそれぞれ独自の数字を持ちます:
In this number notation, the values r, w, and x have their own number value:
 
   
 
r=4
 
r=4
211行目: 242行目:
 
x=1
 
x=1
   
  +
3桁の数字を計算するには、所有者、グループ、そしてユーザーにどのようなアクセス許可を付与するかを検討し、それらの値を合計する必要があります。例えば、ディレクトリの所有者には読み書きと実行のパーミッション、グループとその他のユーザには読み込みと実行のパーミッションのみを与える場合、数字は以下のようになります:
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:
 
   
Owner: rwx = 4+2+1=7
+
* 所有者: {{ic|rwx}}=4+2+1=7
Group: r-x = 4+0+1=5 (or just 4+1=5)
+
* グループ: {{ic|r-x}}=4+0+1=5
Other: r-x = 4+0+1=5 (or just 4+1=5)
+
* その他: {{ic|r-x}}=4+0+1=5
   
  +
$ chmod 755 ''ファイル名''
Final number = 755
 
   
  +
これは、以下の2つのコマンドを実行したときと等価です:
Command: ''chmod 755 filename''
 
   
  +
$ chmod u=rwx ''ファイル名''
This is the equivalent of using the following:
 
  +
$ chmod go=rx ''ファイル名''
   
  +
ファイルやディレクトリのパーミッションを数字形式で見るには、{{man|1|stat}} コマンドを使ってください:
chmod u=rwx filename
 
chmod go=rx filename
 
   
  +
$ stat -c %a ''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.
 
   
  +
%a オプションは出力を数字形式にするという意味です。
To see this in action with examples consider the previous example I've been using but with this numerical method applied instead:
 
   
  +
ほとんどのフォルダとディレクトリは {{ic|755}} に設定され、所有者には読み取り、書き込み、実行を許可しますが、それ以外のすべてのユーザーには書き込みを拒否します。ファイルは通常 {{ic|644}} で、所有者には読み取りと書き込みを許可しますが、他のすべてのユーザーには読み取りだけを許可します。実行可能ファイル以外では {{ic|x}} パーミッションがないという最後のノートを参照してください。ここでも同じです。
Before: -rw-r--r-- 1 ben users 5120 Jun 27 08:28 data
 
   
  +
理解するために、数字形式を使って前回の例と同じことをしてみましょう:
Command: chmod 664 data
 
   
After: -rw-rw-r-- 1 ben users 5120 Jun 27 08:28 data
+
: {{ic|-rw-r--r-- 1 archie web 5120 Jun 27 08:28 foobar}}
   
  +
$ chmod 664 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.
 
   
  +
後: {{ic|-rw-rw-r-- 1 archie web 5120 Jun 27 08:28 foobar}}
Consider this permission set:
 
   
  +
所有者とグループに実行パーミッションを与えたい場合、数字は {{ic|774}} になります。全員に読み込みパーミッションのみを与えたい場合は、{{ic|444}} になります。{{ic|chmod ''xxx'' ''filename''}} で使うための数字を計算する最も簡単な方法は、{{ic|r}} を 4、{{ic|w}} を 2、{{ic|x}} を 1 として扱うことでしょう。しかし、それぞれのパーミッションを2進数として扱い、その後で数字に変換する方法もあります。この方法は少し複雑ですが、完全を期すために言及しておきましょう。
'''- rwx r-x r--'''
 
   
  +
以下のパーミッション組を考えてみましょう:
If you put a 1 under each permission granted, and a 0 for every one not granted, the result would be something like this:
 
   
  +
-rwxr-xr--
'''- rwx rwx r-x'''
 
  +
'''111 111 101'''
 
  +
与えるパーミッションに1を、与えないパーミッションに0を置くと、以下のようになります:
 
  +
You can then convert these binary numbers:
 
  +
-rwxrwxr-x
  +
111111101
  +
  +
そして、これらの2進数を変換します:
   
 
000=0 100=4
 
000=0 100=4
254行目: 289行目:
 
011=3 111=7
 
011=3 111=7
   
  +
よって、結果は 775 となります。
The value of the above would therefore be '''775'''.
 
  +
  +
グループから書き込みパーミッションを取り除いてみましょう:
  +
  +
-rwxr-xr-x
  +
111101101
  +
  +
よって、結果は 755 となり、書き込みパーミッションを取り除くには {{ic|chmod 755 ''filename''}} を実行すれば良いことになります。どの方法をとっても同じ3桁の数字が得られます。文字を使うか数字を使うかは個人の好みと入力速度に依ります。ディレクトリやファイルのパーミッションをデフォルトに戻したい場合 (例えば、所有者に読み書き (と実行権限) を与え、それ以外には書き込み権限を与えない)、{{ic|chmod 755/644 ''filename''}} を使うほうが速いかもしれません。しかし、通常とは異なるパーミッションに変更する場合は、パーミッションを数字に変換するよりも、文字を使う方法をとったほうがシンプルで速いかもしれません (それに、数字を使う方法は間違いを犯す可能性があります)。たまにしか ''chmod'' を使わないユーザにとっては、どちらの方法をとったとしても対して速度に違いはないという主張もあるでしょう。
   
  +
数字を使えば、4桁で {{ic|setuid}} ビット、{{ic|setgid}} ビット、そして {{ic|sticky}} ビットも設定できます。
Consider we wanted to remove the writable permission from group:
 
   
  +
setuid=4
'''- rwz r-x r-x'''
 
  +
setgid=2
'''111 101 101'''
 
  +
sticky=1
   
  +
例えば、{{ic|chmod 2777 ''filename''}} は、全員に対して読み/書き/実行ビットを設定し、さらに {{ic|setgid}} ビットも有効化します。
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.
 
   
 
=== まとめて 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 に変更するには:
To chmod only directories to 755:
 
   
$ find ''directory'' -type d -exec chmod 755 {} +
+
$ find ''ディレクトリ'' -type d -exec chmod 755 {} +
   
  +
ファイルのパーミッションだけを 644 に変更するには:
To chmod only files to 644:
 
   
$ find ''directory'' -type f -exec chmod 644 {} +
+
$ find ''ディレクトリ'' -type f -exec chmod 644 {} +
   
== chown コマンドで所有者変更 ==
+
== 所有者変更 ==
   
  +
[[Wikipedia:ja: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|/dev}} 内のデバイスは root によって所有されており、それはマウントポイント ({{ic|/media/Backup}}) でも然りです。以下を実行することで、マウントポイントの所有者を変更することができます:
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}}
   
  +
これで、パーミッションを変更することなく、新しい所有者である archie がこのパーティションにデータを書き込めるようになりました (所有者のパーミッションは {{ic|rwx}} であるため)。
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'''.
 
   
  +
{{Note|
== リファレンス表 ==
 
  +
* {{ic|chown}} は常に setui と setgid のビットを消去します。
  +
* root 以外のユーザは {{ic|chown}} を使って自身のファイルを他のユーザに「渡す」ことはできません。
  +
}}
   
  +
== アクセス制御リスト ==
{| class="wikitable"
 
! Who !! Permission !! Numbers
 
|-
 
| u - owning user || r - read || 4 - read
 
|-
 
| g - group || w - write || 2 - write
 
|-
 
| o - others || x - execute/search || 1 - execute
 
|-
 
| a - all || ||
 
|-
 
|}
 
   
  +
[[アクセス制御リスト]]は、任意のユーザやグループに対するパーミッションをファイルに設定できるようにすることで、ファイルシステムに柔軟なパーミッション機構を追加します。
== ファイルの属性 ==
 
   
  +
== Umask ==
Apart from the file mode bits that control [[Users and groups|user and group]] read, write and execute permissions, several [[File systems|file systems]] support file attributes that enable further customization of allowable file operations. This section describes some of these attributes and how to work with them.
 
   
  +
[[umask]] ユーティリティを使うことでファイル作成モードマスクを制御することができ、新しくファイルを作成したときのファイルパーミッションの初期値を決めることができます。
{{Warning|By default, file attributes are not preserved by cp, rsync, and probably others.}}
 
   
  +
== ファイルの属性 ==
=== 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:
 
   
  +
{{Warning|デフォルトでは、[[cp]]、[[rsync]]、そしてその他の似たようなプログラムはファイルの属性を保持しません。}}
*a : append only
 
*c : compressed
 
*d : no dump
 
*e : extent format
 
*i : immutable
 
*j : data journalling
 
*s : secure deletion
 
*t : no tail-merging
 
*u : undeletable
 
*A : no atime updates
 
*C : no copy on write
 
*D : synchronous directory updates
 
*S : synchronous updates
 
*T : top of directory hierarchy
 
   
  +
{{Pkg|e2fsprogs}} パッケージには、{{man|1|lsattr}} と {{man|1|chattr}} というプログラムが含まれています。前者はファイルの属性を一覧表示し、後者は属性を変更します。
For example, if you want to set the immutable bit on some file, use the following command:
 
   
  +
いくつか便利な属性があります。全てのファイルシステムが以下の全てをサポートしているわけではありません。
# chattr +i ''/path/to/file''
 
   
  +
* {{ic|a}} - 追加のみ: ファイルは追加 (append) 専用でしか開けなくなります。
To remove an attribute on a file just change {{ic|+}} to {{ic|-}}.
 
  +
* {{ic|c}} - 圧縮済み: ファイルに対してファイルシステムレベルでの圧縮を有効化します。
  +
* {{ic|i}} - 変更不可: 変更、削除、名称変更が禁止され、さらにそのファイルにリンクを張ることができなくなります。root のみがこれを設定できます。
  +
* {{ic|j}} - データのジャーナリング: ファイルデータの書き込みとメタデータに対して[[ファイルシステム#ジャーナリング|ジャーナル]]を使用します。
  +
* {{ic|m}} - 圧縮をしない: ファイルに対してファイルシステムレベルでの圧縮を無効化します。
  +
* {{ic|A}} - atime を更新しない: ファイルの atime が変更されなくなります。
  +
* {{ic|C}} - コピーオンライトをしない: コピーオンライトを無効化します (ファイルシステムがコピーオンライトをサポートしている場合)。
   
  +
属性の完全なリストとそれぞれの属性の詳細は {{man|1|chattr}} を参照してください。
== 拡張属性 ==
 
   
  +
例えば、とあるファイルに変更不可のビットを設定したい場合は、以下のコマンドを使用してください:
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.
 
   
  +
# chattr +i ''/path/to/file''
{{Warning|By default, extended attributes are not preserved by cp, rsync, and probably others.}}
 
   
  +
ファイルから属性を取り除くには、{{ic|+}} を {{ic|-}} に変更してください。
=== ユーザー拡張属性 ===
 
   
  +
== 拡張属性 ==
User extended attributes can be used to store arbitrary information about a file. To create one:
 
   
  +
[[拡張属性]] を見てください。
$ setfattr -n user.checksum -v "3baf9ebce4c664ca8d9e5f6314fb47fb" foo.bar
 
   
  +
== ヒントとテクニック ==
Use getfattr to display extended attributes:
 
   
  +
=== Preserve root ===
$ getfattr -d foo.bar
 
  +
# file: foo.bar
 
  +
{{ic|--preserve-root}} フラグを使うことで {{ic|chmod}} を {{ic|/}} で再帰的に実行してしまうことを防ぐことができます。システム全体から実行可能属性を取り除いてシステムが破壊されてしまう恐れがなくなります。フラグを毎回使いたい場合、[[エイリアス]]で設定すると良いでしょう。[https://www.reddit.com/r/linux/comments/4ni3xe/tifu_sudo_chmod_644/] も参照。
user.checksum="3baf9ebce4c664ca8d9e5f6314fb47fb"
 
   
 
== 参照 ==
 
== 参照 ==
   
* [[wikipedia:Chattr]]
+
* [[Wikipedia:Chattr]]
* [http://www.hackinglinuxexposed.com/articles/20030417.html Linux File Permission Confusion]
+
* [https://www.hackinglinuxexposed.com/articles/20030417.html Linux File Permission Confusion]
* [http://www.hackinglinuxexposed.com/articles/20030424.html Linux File Permission Confusion part 2]
+
* [https://www.hackinglinuxexposed.com/articles/20030424.html Linux File Permission Confusion part 2]
  +
* [https://legacy-documentation.concrete5.org/tutorials/backup-and-restore-file-permissions-in-linux Backup and restore file permissions in Linux]
* [[wikipedia:Extended_file_attributes#Linux]]
 
  +
* [https://serverfault.com/questions/364677/why-is-chmod-r-777-destructive Why is "chmod -R 777 /" destructive?]
* [http://www.lesbonscomptes.com/pages/extattrs.html Extended attributes: the good, the not so good, the bad.]
 
  +
* [https://web.archive.org/web/20210724233134/https://security.ias.edu/node/441 The How and Why of User Private Groups in Unix]
* [http://www.concrete5.org/documentation/how-tos/designers/backup-and-restore-file-permissions-in-linux/ Backup and restore file permissions in Linux]
 
  +
  +
{{TranslationStatus|File permissions and attributes|2023-08-15|785253}}

2023年8月15日 (火) 10:03時点における最新版

関連記事

ファイルシステムは、パーミッション属性を使って、どの程度のレベルでシステムのプロセスがファイルやディレクトリとやり取りできるかを規定します。

警告: セキュリティ目的で使用する場合、パーミッションと属性は、起動済みのシステムからの攻撃しか防御しません。マシンへ物理的にアクセスできる攻撃者からデータを保護するには、ディスク暗号化も施さなければなりません。

パーミッションの表示

ls コマンドの -l オプションを使えば、ディレクトリのコンテンツのパーミッション (ファイルのモード とも) を確認できます。例:

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

1番目の列に注目してください。例えば drwxrwxrwx+ という値であれば、それぞれの文字の意味は以下の様のようになります:

d rwx rwx rwx +
ファイルの種類。技術的にはパーミッションの一部ではありません。ここに入りうる値の説明は info ls -n "What information is listed" を参照してください。 このファイルに対して所有者が持っているパーミッション。以下で説明されています。 このファイルに対してグループが持っているパーミッション。以下で説明されています。 このファイルに対してその他のユーザが持っているパーミッション。以下で説明されています。 代替のアクセス手法がこのファイルに対して適用されているかどうかを示す単一の文字。この文字が空白ならば、代替のアクセス手法は適用されていません。. 文字の場合は、ファイルにセキュリティコンテキストが存在しているが、代替のアクセス手法は適用されていないことを示します。代替のアクセス手法がその他の組み合わせで適用されているファイルでは、+ 文字で表されます。例えば、アクセス制御リストの場合です。

3つの文字からなるパーミッション (上記の例では rwx) には、以下の文字のどれかが入ります:

文字 ファイルに対する効果 ディレクトリに対する効果
読み取りパーミッション (最初の文字) - ファイルは読めません。 ディレクトリの中身は表示できません。
r ファイルを読むことができます。 ディレクトリの中身を表示できます。
書き込みパーミッション (2番目の文字) - ファイルは変更できません。 ディレクトリの中身は変更できません。
w ファイルを変更できます。 ディレクトリの中身を変更できます (ここでの変更とは、新しいファイルやディレクトリの作成、既存のファイルやディレクトリの名称変更や削除です)。ただし、実行パーミッションも設定されていなければなりません。さもないと、このパーミッションは効果を持ちません。
実行パーミッション (3番目の文字) - ファイルは実行できません。 ディレクトリは cd でアクセスできません。
x ファイルを実行できます。 ディレクトリに cd でアクセスできます。これは、親ディレクトリから事実上 "継承している" とみなすことのできる唯一のパーミッションビットです。事実、親ディレクトリのどれか1つでも 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) を見てください。

ヒント: namei -l path を使えば、パスと一緒にパーミッションを表示できます。

サンプル

いくつか例を見て、理解を深めましょう:

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

上記の場合、Archie は Documents ディレクトリへの完全なアクセス権を持っています。Archie は、Documents 内のファイルを (そのファイルのパーミッションに依らず) 一覧表示、名称変更、削除することができ、さらにファイルを Documents 内に作成することができます。しかし、ファイル自体にアクセス (つまり、読み書き実行) できるかどうかは、そのファイルのパーミッションに依ります。

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

Archie はファイルの作成、名称変更、削除ができませんが、それ以外なら可能です。Archie は Documents 内のファイルを一覧表示することができ、Documents 内のファイルに (そのファイルのパーミッションで許可されているならば) アクセスできます。

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

Archie は Documents ディレクトリ内で ls を実行することができませんが、ディレクトリ内の特定のファイルの名前を知っているならば、そのファイルを一覧表示、名称変更、削除することができ、さらに (そのファイルのパーミッションで許可されているならば) アクセスすることができます。また、新しいファイルを作成することもできます。

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

Archie は、Documents ディレクトリ内のファイルのうち、名前を知っているものにアクセスすることしかできません (ただし、そのファイルのパーミッションで許可されている場合に限る)。ディレクトリ内のファイルを一覧表示することはできず、ファイルの作成、名称変更、削除もできません。

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

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

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

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

パーミッションの変更

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

文字を使う方法

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

chmod who=パーミッション ファイル名

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

  • u: そのファイルを所有するユーザー
  • g: そのファイルが所属するユーザーグループ
  • o: その他 (other) のユーザー。つまり、その他全員。
  • a: 上記全て。ugo と入力する代わりとして使えます。

ここでのパーミッションとは、#パーミッションの表示 で説明しているのと同じです (rwx)。

それではコマンドを使用例を見てみましょう。Documents ドキュメントを守るために、このディレクトリであなた以外の全員が読み書きと実行 (この場合は検索ともいえます) を行えないようにするには:

前: drwxr-xr-x 6 archie web 4096 Jul 5 17:37 Documents

$ chmod g= Documents
$ chmod o= Documents

後: drwx------ 6 archie web 4096 Jul 6 17:32 Documents

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

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

前: drwx------ 6 archie web 4096 Jul 6 17:32 Documents

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

後: drwxr-xr-x 6 archie web 4096 Jul 6 17:32 Documents

次の例では、グループとその他のユーザに対して読み込みと実行のパーミッションを与えてみます。つまり、= の後にパーミッションの文字 (rx) を入れます (スペース無しで)。

who に複数の文字を入力することで、これを行うことができます。例えば:

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

次に、2番目の例を考えてみましょう。たとえば、foobar ファイルを変更して、読み取りと書き込みのアクセス許可を与え、web グループの同僚が作業している可能性があると仮定します。foobar は読み取りと書き込みもできますが、他のユーザーは読み取りしかできません:

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

$ chmod g=rw foobar

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

これは最初の例とまったく同じですが、ディレクトリではなくファイルを使用し、書き込み権限を付与します (権限を付与する例を全て示すため)。

文字を使う方法のショートカット

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

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

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

$ chmod g+w foobar

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

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

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

$ chmod a-w foobar

後: -r--r--r-- 1 archie web 5120 Jun 27 08:28 foobar

別のショートカットは特殊な X モードです。これは実際のファイルモードではありませんが、多くの場合、-R オプションと組み合わせて使用され、ディレクトリに対してのみ実行可能ビットを設定し、通常のファイルに対しては変更しないでおきます。たとえば、次のようになります:

$ chmod -R a+rX ./data/

パーミッションのコピー

chmod で、あるクラスから別のクラスにパーミッションをコピーすることも可能です (例えば、所有者のパーミッションをグループにコピーする)。そうするには、= の後に、rwx ではなく、もう一つの who 文字を入れてください。例えば:

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

$ chmod g=u foobar

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

このコマンドは基本的に、"グループのアクセス許可を変更 (g=) して、所有ユーザー (=u) と同じにします。ただし、パーミッションのセットをコピーして、なおかつ新しいパーミッションを付与するというようなことはできません。例えば:

$ chmod g=wu foobar

この場合、chmod はエラーを吐きます。

数字を使う方法

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

数字を使えば、所有者、グループ、そしてその他のユーザのパーミッション全て、さらに setuid、setgid、そしてスティッキービットも一度に設定できます。基本的な使い方は以下のとおりです:

$ chmod xxx ファイル名

ここで、xxx は3桁の数字で、各桁は0から7までのどれかです。最初の桁は所有者のパーミッション、2桁目はグループのパーミッション、そして3桁目はその他のユーザのパーミッションです。

この数字表記では、rw、そして x はそれぞれ独自の数字を持ちます:

r=4
w=2
x=1

3桁の数字を計算するには、所有者、グループ、そしてユーザーにどのようなアクセス許可を付与するかを検討し、それらの値を合計する必要があります。例えば、ディレクトリの所有者には読み書きと実行のパーミッション、グループとその他のユーザには読み込みと実行のパーミッションのみを与える場合、数字は以下のようになります:

  • 所有者: rwx=4+2+1=7
  • グループ: r-x=4+0+1=5
  • その他: r-x=4+0+1=5
$ chmod 755 ファイル名

これは、以下の2つのコマンドを実行したときと等価です:

$ chmod u=rwx ファイル名
$ chmod go=rx ファイル名

ファイルやディレクトリのパーミッションを数字形式で見るには、stat(1) コマンドを使ってください:

$ stat -c %a filename

%a オプションは出力を数字形式にするという意味です。

ほとんどのフォルダとディレクトリは 755 に設定され、所有者には読み取り、書き込み、実行を許可しますが、それ以外のすべてのユーザーには書き込みを拒否します。ファイルは通常 644 で、所有者には読み取りと書き込みを許可しますが、他のすべてのユーザーには読み取りだけを許可します。実行可能ファイル以外では x パーミッションがないという最後のノートを参照してください。ここでも同じです。

理解するために、数字形式を使って前回の例と同じことをしてみましょう:

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

$ chmod 664 foobar

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

所有者とグループに実行パーミッションを与えたい場合、数字は 774 になります。全員に読み込みパーミッションのみを与えたい場合は、444 になります。chmod xxx filename で使うための数字を計算する最も簡単な方法は、r を 4、w を 2、x を 1 として扱うことでしょう。しかし、それぞれのパーミッションを2進数として扱い、その後で数字に変換する方法もあります。この方法は少し複雑ですが、完全を期すために言及しておきましょう。

以下のパーミッション組を考えてみましょう:

-rwxr-xr--

与えるパーミッションに1を、与えないパーミッションに0を置くと、以下のようになります:

-rwxrwxr-x
 111111101

そして、これらの2進数を変換します:

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

よって、結果は 775 となります。

グループから書き込みパーミッションを取り除いてみましょう:

-rwxr-xr-x
 111101101

よって、結果は 755 となり、書き込みパーミッションを取り除くには chmod 755 filename を実行すれば良いことになります。どの方法をとっても同じ3桁の数字が得られます。文字を使うか数字を使うかは個人の好みと入力速度に依ります。ディレクトリやファイルのパーミッションをデフォルトに戻したい場合 (例えば、所有者に読み書き (と実行権限) を与え、それ以外には書き込み権限を与えない)、chmod 755/644 filename を使うほうが速いかもしれません。しかし、通常とは異なるパーミッションに変更する場合は、パーミッションを数字に変換するよりも、文字を使う方法をとったほうがシンプルで速いかもしれません (それに、数字を使う方法は間違いを犯す可能性があります)。たまにしか chmod を使わないユーザにとっては、どちらの方法をとったとしても対して速度に違いはないという主張もあるでしょう。

数字を使えば、4桁で setuid ビット、setgid ビット、そして sticky ビットも設定できます。

setuid=4
setgid=2
sticky=1

例えば、chmod 2777 filename は、全員に対して読み/書き/実行ビットを設定し、さらに setgid ビットも有効化します。

まとめて chmod

一般に、ディレクトリとファイルの両方に同じパーミッションを設定することはありません。ディレクトリツリー内のパーミッションを一括で変更する必要がある場合は、find を使って選択的に修正してください。

ディレクトリのパーミッションだけを 755 に変更するには:

$ find ディレクトリ -type d -exec chmod 755 {} +

ファイルのパーミッションだけを 644 に変更するには:

$ find ディレクトリ -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

見ての通り、/dev 内のデバイスは root によって所有されており、それはマウントポイント (/media/Backup) でも然りです。以下を実行することで、マウントポイントの所有者を変更することができます:

前: 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

これで、パーミッションを変更することなく、新しい所有者である archie がこのパーティションにデータを書き込めるようになりました (所有者のパーミッションは rwx であるため)。

ノート:
  • chown は常に setui と setgid のビットを消去します。
  • root 以外のユーザは chown を使って自身のファイルを他のユーザに「渡す」ことはできません。

アクセス制御リスト

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

Umask

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

ファイルの属性

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

警告: デフォルトでは、cprsync、そしてその他の似たようなプログラムはファイルの属性を保持しません。

e2fsprogs パッケージには、lsattr(1)chattr(1) というプログラムが含まれています。前者はファイルの属性を一覧表示し、後者は属性を変更します。

いくつか便利な属性があります。全てのファイルシステムが以下の全てをサポートしているわけではありません。

  • a - 追加のみ: ファイルは追加 (append) 専用でしか開けなくなります。
  • c - 圧縮済み: ファイルに対してファイルシステムレベルでの圧縮を有効化します。
  • i - 変更不可: 変更、削除、名称変更が禁止され、さらにそのファイルにリンクを張ることができなくなります。root のみがこれを設定できます。
  • j - データのジャーナリング: ファイルデータの書き込みとメタデータに対してジャーナルを使用します。
  • m - 圧縮をしない: ファイルに対してファイルシステムレベルでの圧縮を無効化します。
  • A - atime を更新しない: ファイルの atime が変更されなくなります。
  • C - コピーオンライトをしない: コピーオンライトを無効化します (ファイルシステムがコピーオンライトをサポートしている場合)。

属性の完全なリストとそれぞれの属性の詳細は chattr(1) を参照してください。

例えば、とあるファイルに変更不可のビットを設定したい場合は、以下のコマンドを使用してください:

# chattr +i /path/to/file

ファイルから属性を取り除くには、+- に変更してください。

拡張属性

拡張属性 を見てください。

ヒントとテクニック

Preserve root

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

参照

翻訳ステータス: このページは en:File permissions and attributes の翻訳バージョンです。最後の翻訳日は 2023-08-15 です。もし英語版に 変更 があれば、翻訳の同期を手伝うことができます。