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

提供: ArchWiki
ナビゲーションに移動 検索に移動
(同期)
(文字列「Tips and tricks」を「ヒントとテクニック」に置換)
374行目: 374行目:
 
拡張属性は[[ケイパビリティ]]を設定するのにも使われます。
 
拡張属性は[[ケイパビリティ]]を設定するのにも使われます。
   
  +
== ヒントとテクニック ==
== Tips and tricks ==
 
   
 
=== ルートの保護 ===
 
=== ルートの保護 ===

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

関連記事

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

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

パーミッションの表示

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

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

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

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

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

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

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

サンプル

Let us see some examples to clarify:

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

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

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

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

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

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

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

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

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

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

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

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

パーミッションを変更

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

テキストを使う方法

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

chmod who=permissions filename

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

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

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

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

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

$ chmod g= Documents
$ chmod o= Documents

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

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

This can be reverted with:

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

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

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

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

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

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

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

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

$ chmod g=rw foobar

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

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

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

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

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

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

$ chmod g+w foobar

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

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

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

$ chmod a-w foobar

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

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

$ chmod -R a+rX ./data/

パーミッションのコピー

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

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

$ chmod g=u foobar

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

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

$ chmod g=wu foobar

In that case chmod throw an error.

数字を使う方法

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

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

$ chmod xxx filename

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

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

r=4
w=2
x=1

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

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

This is the equivalent of using the following:

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

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

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

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

$ chmod 664 foobar

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

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

Consider this permission set:

-rwxr-xr--

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

-rwxrwxr-x
 111111101

You can then convert these binary numbers:

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

The value of the above would therefore be 775.

Consider we wanted to remove the writable permission from group:

-rwzr-xr-x
 111101101

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

まとめて chmod

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

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

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

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

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

所有者を変更

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

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

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

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

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

# chown archie /media/Backup

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

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

アクセス制御リスト

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

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

ファイルの属性

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

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

chattr と lsattr

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

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

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

# chattr +i /path/to/file

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

拡張属性

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

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

ユーザー拡張属性

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

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

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

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

ケイパビリティ

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

ヒントとテクニック

ルートの保護

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

参照