セキュリティの観点で、ファイルやディレクトリの「権限(パーミッション)」はとても重要です。権限によって、どのユーザーがファイルを読む、書く、実行できるかが決まります。権限を設定・変更するコマンドが chmod です。この記事では、chmod の基本から、実際に動かして権限の効き方を確かめるところまでを順に見ていきます。
Linux のパーミッションの概要は 【Linux】アクセス権(パーミッション)の概要 を参照してください。
- 8進数表現とシンボリック表現による、
chmodの権限の指定方法 - ファイルとディレクトリで、権限がどう効くかの違い
-Rオプションで、権限を中身ごと変更する方法
chmodの基本
権限は、ファイルやディレクトリがどのように扱われるかを決めます。ユーザーがファイルを読む、書く、実行できるかどうかは、この権限で制御されます。
権限を変更できるのは、ファイルの所有者と、スーパーユーザー(root など、特権を持つユーザー)だけです。任意のユーザーが他人のファイルの権限を変えることはできません。
chmod の構文は次のとおりです。
chmod [パーミッション表現] [ファイル名(もしくはディレクトリ名)]
パーミッション表現には、「8進数表現」と「シンボリック表現」の2種類があります。どちらも最終的な目的は同じで、指定のしかたが違います。
パーミッション表現
8進数表現
8進数表現は、数字で権限を直接指定する方法です。ファイルやディレクトリに対する操作は、読み取り(r)、書き込み(w)、実行(x)の3つで、ファイルとディレクトリで次のように意味が変わります。
| 権限 | ファイルの場合 | ディレクトリの場合 |
|---|---|---|
| 読み取り(r) | ファイルの内容を読める | ディレクトリ内のファイルやサブディレクトリの一覧を表示できる |
| 書き込み(w) | ファイルの内容を変更できる | ディレクトリ内にファイルやサブディレクトリを追加・削除できる |
| 実行(x) | ファイルを実行できる | ディレクトリ内のファイルやサブディレクトリにアクセスできる |
8進数表現では、この3つの操作をそれぞれ次の数値で表します。
これらを足し合わせると、0 から 7 までの数値を作れます。たとえば、読み取りと書き込みを許可する rw- は、4 + 2 = 6 です。
rw- は 4 + 2 で 6組み合わせは次のとおりです。
| 8進数 | 権限 | 説明 |
|---|---|---|
| 0 | — | すべての権限を持たない |
| 1 | –x | 実行権限のみ |
| 2 | -w- | 書き込み権限のみ |
| 3 | -wx | 書き込みと実行権限 |
| 4 | r– | 読み取り権限のみ |
| 5 | r-x | 読み取りと実行権限 |
| 6 | rw- | 読み取りと書き込み権限 |
| 7 | rwx | 読み取り、書き込み、実行のすべての権限 |
8進数表現は 3桁の数字で書き、左から順に、所有者、グループ、その他のユーザーの権限を表します。ls -l の左端に表示される権限は、先頭の1文字がファイルの種類(ファイルは -、ディレクトリは d)で、続く9文字が3文字ずつ、所有者、グループ、その他の権限です。たとえば -rw-rw-r-- は、rw-(6)、rw-(6)、r--(4)なので、8進数表現では 664 になります。
ls -l の権限表示と、8進数表現の対応シンボリック表現
シンボリック表現は、文字で権限を指定する方法です。権限の対象(u, g, o, a)、操作(+, -, =)、権限の種類(r, w, x)の3つの部分で構成します。たとえば g-w は、グループ(g)から、書き込み権限(w)を削除(-)する指定です。
権限の対象
| 記号 | 対象 | 意味 |
|---|---|---|
| u | User | ファイルやディレクトリの所有者 |
| g | Group | ファイルやディレクトリのグループ |
| o | Others | 所有者でもグループに属してもいない、その他のユーザー |
| a | All | すべてのユーザー(所有者、グループ、その他のすべて) |
操作
| 記号 | 操作 | 例 |
|---|---|---|
| + | 権限を追加する | u+x:所有者に実行権限を追加する |
| – | 権限を削除する | g-w:グループの書き込み権限を削除する |
| = | 権限を設定する。既存の権限は上書きされる | o=r:その他のユーザーの権限を、読み取りのみにする |
chmod +x file や chmod -w file のように対象(u, g, o, a)を省略すると、a を指定したものとして扱われます。ただし、umask で設定されているビットは変更されません。この記事の検証環境のように、新規ファイルが rw-rw-r-- で作られる環境では、書き込み権限を削除する -w は、すべての対象から書き込み権限を削除します。
動作確認(ファイル)
ここからは、chmod で変更した権限が実際にどう動作するかを確認します。ユーザー名「hirotano」でログインしているものとします。
8進数表現による権限確認
テストファイルの準備
まず、テスト用に test_text_oct.txt という名前のテキストファイルを作成します。
hirotano@ubuntu-1:~$ echo "This is a sample text file for testing octal permissions." > test_text_oct.txt
作成されたファイルの権限を確認します。
hirotano@ubuntu-1:~$ ls -l
-rw-rw-r-- 1 hirotano hirotano 58 Sep 18 13:06 test_text_oct.txt
所有者には、読み取りと書き込みの権限が付与されています。cat コマンドで内容を読み取れることを確認します。
hirotano@ubuntu-1:~$ cat test_text_oct.txt This is a sample text file for testing octal permissions
ファイルの読み取り権限を削除
chmod コマンドで、ファイルの読み取り権限を削除します。200 を指定して、所有者にだけ書き込み権限を設定します。
hirotano@ubuntu-1:~$ chmod 200 test_text_oct.txt
hirotano@ubuntu-1:~$ ls -l
--w------- 1 hirotano hirotano 58 Sep 18 13:06 test_text_oct.txt
ファイルの内容を cat コマンドで読み取ろうとすると、Permission denied のエラーメッセージが表示されます。
hirotano@ubuntu-1:~$ cat test_text_oct.txt
cat: test_text_oct.txt: Permission denied
ファイルの書き込み権限を削除
次に、ファイルの書き込み権限を削除します。400 を指定して、所有者にだけ読み取り権限を設定します。
hirotano@ubuntu-1:~$ chmod 400 test_text_oct.txt
hirotano@ubuntu-1:~$ ls -l
-r-------- 1 hirotano hirotano 58 Sep 18 13:06 test_text_oct.txt
echo コマンドでファイルにテキストを追加しようとすると、Permission denied のエラーメッセージが表示されます。
hirotano@ubuntu-1:~$ echo "Trying to add this line." >> test_text_oct.txt
-bash: test_text_oct.txt: Permission denied
テストスクリプトの準備
テスト用に、test_script_oct.sh という名前の bash スクリプトを作成します。
hirotano@ubuntu-1:~$ echo 'echo "Hello from test_script_oct.sh!"' > test_script_oct.sh
hirotano@ubuntu-1:~$ ls -l
-rw-rw-r-- 1 hirotano hirotano 38 Sep 18 13:17 test_script_oct.sh
作成したばかりのファイルには、所有者の実行権限が付いていません。この状態でスクリプトを実行すると、Permission denied のエラーになります。
ファイルの実行権限の付与
chmod コマンドで、スクリプトに実行権限を追加します。500 を指定して、所有者に読み取りと実行の権限だけを設定します。
hirotano@ubuntu-1:~$ chmod 500 test_script_oct.sh
hirotano@ubuntu-1:~$ ls -l
-r-x------ 1 hirotano hirotano 38 Sep 18 13:17 test_script_oct.sh
スクリプトがエラーなく実行できることを確認します。
hirotano@ubuntu-1:~$ ./test_script_oct.sh Hello from test_script_oct.sh!
bash スクリプトを ./test_script_oct.sh のように直接実行するときは、実行権限(x)に加えて、読み取り権限(r)も必要です。bash がスクリプトの中身を読み込んで実行するためです。500 は読み取りと実行の両方を含むので、実行できます。
シンボリック表現による権限確認
テストファイルの準備
まず、テスト用に test_text_sym.txt という名前のテキストファイルを作成します。
hirotano@ubuntu-1:~$ echo "This is a sample text file for testing symbolic permissions." > test_text_sym.txt hirotano@ubuntu-1:~$ ls -l -rw-rw-r-- 1 hirotano hirotano 61 Sep 18 13:27 test_text_sym.txt
ファイルの読み取り権限を削除
chmod コマンドで、所有者の読み取り権限を削除します。
hirotano@ubuntu-1:~$ chmod u-r test_text_sym.txt
hirotano@ubuntu-1:~$ ls -l
--w-rw-r-- 1 hirotano hirotano 61 Sep 18 13:27 test_text_sym.txt
ファイルの内容を cat コマンドで読み取ろうとすると、Permission denied のエラーメッセージが表示されます。
hirotano@ubuntu-1:~$ cat test_text_sym.txt
cat: test_text_sym.txt: Permission denied
所有者に読み取り権限を付与して、元の権限に戻します。cat コマンドで、ファイルの内容を読み取れるようになります。
hirotano@ubuntu-1:~$ chmod u+r test_text_sym.txt
hirotano@ubuntu-1:~$ cat test_text_sym.txt
This is a sample text file for testing symbolic permissions.
hirotano@ubuntu-1:~$ ls -l
-rw-rw-r-- 1 hirotano hirotano 61 Sep 18 13:27 test_text_sym.txt
ファイルの書き込み権限を削除
chmod コマンドで、ファイルの書き込み権限を削除します。対象を省略しているので、所有者、グループ、その他のすべてから書き込み権限が削除されます。
hirotano@ubuntu-1:~$ chmod -w test_text_sym.txt
hirotano@ubuntu-1:~$ ls -l
-r--r--r-- 1 hirotano hirotano 61 Sep 18 13:27 test_text_sym.txt
echo コマンドでファイルにテキストを追加しようとすると、Permission denied のエラーメッセージが表示されます。
hirotano@ubuntu-1:~$ echo "Trying to add this line." >> test_text_sym.txt
-bash: test_text_sym.txt: Permission denied
スクリプトの実行権限の追加
テスト用に、test_script_sym.sh という名前の bash スクリプトを作成します。
hirotano@ubuntu-1:~$ echo 'echo "Hello from test_script_sym.sh!"' > test_script_sym.sh hirotano@ubuntu-1:~$ ls -l -rw-rw-r-- 1 hirotano hirotano 38 Sep 18 13:38 test_script_sym.sh
このスクリプトを実行すると、Permission denied のエラーメッセージが表示されます。
hirotano@ubuntu-1:~$ ./test_script_sym.sh
-bash: ./test_script_sym.sh: Permission denied
スクリプトの実行権限を追加
chmod コマンドで、スクリプトに実行権限を追加します。
hirotano@ubuntu-1:~$ chmod +x test_script_sym.sh
hirotano@ubuntu-1:~$ ls -l
-rwxrwxr-x 1 hirotano hirotano 38 Sep 18 13:38 test_script_sym.sh
スクリプトがエラーなく実行できることを確認します。
hirotano@ubuntu-1:~$ ./test_script_sym.sh Hello from test_script_sym.sh!
動作確認(ディレクトリ)
ディレクトリの権限は、ファイルの権限と考え方が少し違うので、注意が必要です。
テストディレクトリとファイルの準備
まず、test_dir というディレクトリと、その中に sample.txt というテキストファイルを作成します。
hirotano@ubuntu-1:~$ mkdir test_dir hirotano@ubuntu-1:~$ echo "This is a sample text inside the directory." > test_dir/sample.txt
ディレクトリの権限を確認します。
hirotano@ubuntu-1:~$ ls -l
drwxrwxr-x 2 hirotano hirotano 4096 Sep 18 13:58 test_dir
所有者には、読み取り、書き込み、実行の権限が設定されています。先頭の d は、ディレクトリを表します。
ディレクトリの実行権限を削除
ディレクトリから実行権限を削除すると、そのディレクトリに cd で入れなくなります。600 を指定して、所有者の実行権限を外します。
hirotano@ubuntu-1:~$ chmod 600 test_dir
hirotano@ubuntu-1:~$ cd test_dir
-bash: cd: test_dir: Permission denied
hirotano@ubuntu-1:~$ ls -l
drw------- 2 hirotano hirotano 4096 Sep 18 13:58 test_dir
ディレクトリの書き込み権限を削除
次に、ディレクトリの書き込み権限を削除します。500 を指定すると、所有者の書き込み権限が外れ、ディレクトリ内のファイルを削除できなくなります。
hirotano@ubuntu-1:~$ chmod 500 test_dir
hirotano@ubuntu-1:~$ rm test_dir/sample.txt
rm: cannot remove 'test_dir/sample.txt': Permission denied
hirotano@ubuntu-1:~$ ls -l
dr-x------ 2 hirotano hirotano 4096 Sep 18 13:58 test_dir
ファイルを削除できるかどうかは、ファイル自体の権限ではなく、そのファイルがあるディレクトリの権限で決まります。ディレクトリ内のファイルを削除するには、そのディレクトリの書き込み権限(w)と実行権限(x)の両方が必要です。
ディレクトリの権限を再帰的に変更
chmod の -R オプションを使うと、ディレクトリと、その中のすべてのサブディレクトリ・ファイルの権限を、まとめて変更できます。
テスト用に、test_dir というディレクトリと、その中に sub_dir というサブディレクトリ、さらにその中に sample.txt というテキストファイルを作成します。
hirotano@ubuntu-1:~$ mkdir -p test_dir/sub_dir hirotano@ubuntu-1:~$ echo "This is a sample text inside the subdirectory." > test_dir/sub_dir/sample.txt
権限を確認します。
hirotano@ubuntu-1:~$ ls -l drwxrwxr-x 3 hirotano hirotano 4096 Sep 18 14:16 test_dir hirotano@ubuntu-1:~$ ls -l test_dir drwxrwxr-x 2 hirotano hirotano 4096 Sep 18 14:16 sub_dir hirotano@ubuntu-1:~$ ls -l test_dir/sub_dir/ -rw-rw-r-- 1 hirotano hirotano 47 Sep 18 14:16 sample.txt
-Rオプションなしでの権限変更
まず、-R オプションを付けずに、test_dir の権限を変更します。
hirotano@ubuntu-1:~$ chmod 700 test_dir
変更されるのは test_dir の権限だけです。sub_dir と、その中の sample.txt の権限は変わりません。
hirotano@ubuntu-1:~$ ls -l drwx------ 3 hirotano hirotano 4096 Sep 18 14:16 test_dir hirotano@ubuntu-1:~$ ls -l test_dir/ drwxrwxr-x 2 hirotano hirotano 4096 Sep 18 14:16 sub_dir hirotano@ubuntu-1:~$ ls -l test_dir/sub_dir/ -rw-rw-r-- 1 hirotano hirotano 47 Sep 18 14:16 sample.txt
-Rオプションありでの権限変更
次に、-R オプションを付けて、test_dir と、その中のすべてのサブディレクトリ・ファイルの権限を変更します。
hirotano@ubuntu-1:~$ chmod -R 700 test_dir
今度は、test_dir だけでなく、sub_dir と sample.txt の権限も変わります。
hirotano@ubuntu-1:~$ ls -l
drwx------ 3 hirotano hirotano 4096 Sep 18 14:16 test_dir
hirotano@ubuntu-1:~$ ls -l test_dir/
drwx------ 2 hirotano hirotano 4096 Sep 18 14:16 sub_dir
hirotano@ubuntu-1:~$ ls -l test_dir/sub_dir/
-rwx------ 1 hirotano hirotano 47 Sep 18 14:16 sample.txt
-R 700 は、ディレクトリだけでなくファイルにも同じ権限を設定するため、上の sample.txt のように、ファイルにも実行権限が付きます。ファイルには実行権限を付けず、ディレクトリにだけ付けたいときは、シンボリック表現の X(大文字)を使い、chmod -R u=rwX,go= test_dir のように指定します。X は、対象がディレクトリの場合か、すでにいずれかの対象に実行権限がある場合にだけ、実行権限を設定します。
まとめ
chmod は、8進数表現とシンボリック表現のどちらでも、同じ権限を設定できます。ファイルの動作確認では、読み取りと書き込みの権限を外すと cat や echo が Permission denied になり、実行権限を付けるとスクリプトを実行できることを確認しました。ディレクトリでは、実行権限がないと cd できず、書き込み権限がないと中のファイルを削除できません。
| 指定 | 意味 |
|---|---|
chmod 664 file | 8進数表現。所有者は rw-、グループは rw-、その他は r– |
chmod u+x file | シンボリック表現。所有者に実行権限を追加する |
chmod g-w file | シンボリック表現。グループの書き込み権限を削除する |
chmod o=r file | シンボリック表現。その他のユーザーを読み取りのみにする |
chmod -R 700 dir | ディレクトリと、その中のすべてを、所有者だけが使える権限にする |
コメント