Linux: ファイル権限 — chmod / chown / umask詳解
Linuxはマルチユーザーオペレーティングシステムです。ファイル権限はセキュリティモデルの基盤であり、各ファイルとディレクトリに対して誰が読み取り、書き込み、実行できるかを制御します。
📋 前提条件:まず以下を完了していること
- レッスン4:ファイルシステムのナビゲーション
- レッスン5:ファイルとディレクトリの操作
1. このレッスンで学ぶこと
- rwx権限モデルの解説
- chmod数値記法(755/644/700)
- chmodシンボリック記法(u/g/o/a +-=)
- chownによる所有者とグループの変更
- umaskデフォルト権限
- 特殊権限:SUID / SGID / Sticky Bit
2. 「Permission denied」ストーリー
(1) 悩み:スクリプトが実行できない
小明はdeploy.shスクリプトを書きましたが、実行しようとすると:
./deploy.sh
# bash: ./deploy.sh: Permission denied
ファイル権限を確認しました:
ls -l deploy.sh
# -rw-r--r-- 1 charlie charlie 50 Jul 7 10:23 deploy.sh
(2) 実行権限の追加
ボブが言いました。「スクリプトには読み取りと書き込みの権限はあるけど、実行権限がない。+xを追加するだけ。」
chmod +x deploy.sh
ls -l deploy.sh
# -rwxr-xr-x 1 charlie charlie 50 Jul 7 10:23 deploy.sh
./deploy.sh # It works now!
(3) 得られるもの:Linux権限システムの理解
小明はchmod +xの意味を理解しました。さらに重要なのは、Linuxがなぜ安全なのかを理解し始めたこと — すべてのファイルとユーザーに明確な権限境界があるからです。
3. 知識ポイント
(1) rwx権限モデル
ls -l出力の最初の列が権限情報です。例:-rwxr-xr-x:
- rwx r-x r-x
│ └┬─┘ └┬─┘ └┬─┘
│ │ │ └── Others (other) permissions
│ │ └─────── Group (group) permissions
│ └──────────── File owner (user) permissions
└──────────────── File type (- regular file / d directory / l link)
3つの権限ビットの意味:
| 権限 | 文字 | 数値 | ファイルの場合 | ディレクトリの場合 |
|---|---|---|---|---|
| 読み取り | r | 4 | ファイル内容の閲覧 | ディレクトリ内容の一覧表示(ls) |
| 書き込み | w | 2 | ファイル内容の修正 | ディレクトリ内でのファイル作成/削除 |
| 実行 | x | 1 | ファイルの実行(スクリプト/プログラム) | ディレクトリへの进入(cd) |
(2) chmod数値記法
3桁の数字がユーザー/グループ/その他の権限に対応します:
| 数字 | バイナリ | 権限 |
|---|---|---|
| 7 | 111 | rwx(読み取り+書き込み+実行) |
| 6 | 110 | rw-(読み取り+書き込み) |
| 5 | 101 | r-x(読み取り+実行) |
| 4 | 100 | r--(読み取り専用) |
| 3 | 011 | -wx(書き込み+実行) |
| 2 | 010 | -w-(書き込み専用) |
| 1 | 001 | --x(実行専用) |
| 0 | 000 | ---(権限なし) |
よく使う権限の組み合わせ:
chmod 755 script.sh # rwxr-xr-x Owner can read/write/execute, others can read/execute
chmod 644 file.txt # rw-r--r-- Owner can read/write, others can read only
> 💡 ヒント:最もよく使う2つの権限組み合わせを覚えましょう — 755はスクリプトの標準権限(所有者は実行可能、他者は読み取り/実行可能)、644は通常ファイルの標準権限(所有者は読み取り/書き込み、他者は読み取り専用)。755と644の唯一の違いは「実行」ビットです。
chmod 700 private.sh # rwx------ Only the owner can operate
chmod 600 secret.txt # rw------- Only the owner can read/write
chmod 777 public/ # rwxrwxrwx Everyone can operate (⚠️ insecure)
chmod 777は全ユーザーに読み取り、書き込み、実行を許可します。サーバーでは極めて危険 — いずれかのアカウントが侵害されると、攻撃者がそのファイルを改ざんできます。本番環境では777を使わず、グループ権限(例:775)やACLで共有アクセスを実現してください。
(3) chmodシンボリック記法
# Syntax: chmod [who][operation][permission] file
# Who: u (owner) / g (group) / o (others) / a (all)
# Operation: + (add) / - (remove) / = (set exactly)
chmod u+x script.sh # Add execute permission for owner
chmod g-w file.txt # Remove write permission for group
chmod o-r private.txt # Remove read permission for others
chmod a+x script.sh # Add execute permission for everyone
chmod u=rwx,g=rx,o=r # Equivalent to 754
(4) chown — 所有者とグループの変更
# Change file owner
sudo chown alice file.txt
# Change file group
sudo chown :developers file.txt
# Change both owner and group
sudo chown alice:developers file.txt
# Recursively change directory and its contents
sudo chown -R alice:developers /srv/project/
(5) umask — デフォルト権限マスク
新しいファイルやディレクトリを作成する際、Linuxはumask値を減算してデフォルト権限を決定します。
# View current umask
umask
# Output: 0022
# Default permission calculation:
# File maximum: 666 (rw-rw-rw-)
# Directory maximum: 777 (rwxrwxrwx)
# Subtract umask 022:
# File default: 666 - 022 = 644 (rw-r--r--)
# Directory default: 777 - 022 = 755 (rwxr-xr-x)
# Change umask
umask 007 # Stricter default permissions
umask 000 # All users can write (insecure)
(6) 特殊権限
| 特殊権限 | 数値 | ファイルへの効果 | ディレクトリへの効果 |
|---|---|---|---|
| SUID | 4xxx | ファイル所有者として実行 | (なし) |
| SGID | 2xxx | ファイルグループとして実行 | 新規ファイルがディレクトリのグループを継承 |
| Sticky Bit | 1xxx | (なし) | ファイル所有者のみ削除可能 |
/usr/bin/passwdなど)にのみ設定し、定期的にSUIDファイルを監査してください:find / -perm -4000 -type f 2>/dev/null
# Set SUID (4)
chmod u+s /usr/bin/passwd # passwd runs as root
chmod 4755 /usr/bin/program # Numeric notation for SUID
# Set SGID (2)
chmod g+s /srv/shared # Files created in this directory inherit the group
chmod 2775 /srv/shared # Numeric notation for SGID
# Set Sticky Bit (1)
chmod +t /tmp # Only file owner can delete
chmod 1777 /tmp # Standard permissions for /tmp
(7) ▶ サンプル:ファイル権限の確認
# View permissions in long format
ls -l /etc/passwd
# -rw-r--r-- 1 root root 2845 Jul 7 10:23 /etc/passwd
# View directory permissions
ls -ld /tmp
# drwxrwxrwt 1 root root 4096 Jul 7 10:23 /tmp
# The trailing t indicates Sticky Bit
# Recursively view directory permissions
ls -laR /srv/project | head -20
出力:
TEXT/srv/project: total 16 drwxr-xr-x 3 alice developers 4096 Jul 7 10:23 . drwxr-xr-x 4 root root 4096 Jul 7 09:00 .. -rw-r--r-- 1 alice developers 256 Jul 7 10:23 config.yaml -rwxr-xr-x 1 alice developers 512 Jul 7 10:23 deploy.sh drwxr-xr-x 2 alice developers 4096 Jul 7 10:23 src
(8) ▶ サンプル:chmod数値記法
# 755 — standard script permissions
chmod 755 deploy.sh
ls -l deploy.sh
# -rwxr-xr-x
# 644 — standard file permissions
chmod 644 index.html
ls -l index.html
# -rw-r--r--
# 600 — private file
chmod 600 private.key
ls -l private.key
# -rw-------
出力:
TEXT-rwxr-xr-x 1 alice developers 512 Jul 7 10:23 deploy.sh -rw-r--r-- 1 alice developers 256 Jul 7 10:23 index.html -rw------- 1 alice developers 128 Jul 7 10:23 private.key
(9) ▶ サンプル:chmodシンボリック記法
# Add execute permission
chmod +x script.sh # Add +x for everyone (equivalent to a+x)
# Add execute permission for owner only
chmod u+x script.sh
# Remove write permission for group and others
chmod go-w config.txt
# Set standard permissions for a shell script
chmod u=rwx,g=rx,o=r script.sh
# Equivalent to chmod 755 script.sh
出力:
TEXT-rwxr-xr-- 1 alice developers 512 Jul 7 10:23 script.sh
(10) ▶ サンプル:chownで所有者を変更
# Change file owner
sudo chown www-data index.html
# Change group
sudo chown :www-data /srv/www/
# Change both
sudo chown alice:developers project/
# Recursively change entire directory tree
sudo chown -R alice:alice ~alice/
出力:
TEXT-rw-r--r-- 1 www-data www-data 256 Jul 7 10:23 index.html drwxr-xr-x 2 www-data www-data 4096 Jul 7 10:23 /srv/www/ drwxr-xr-x 3 alice developers 4096 Jul 7 10:23 project/
(11) ▶ サンプル:umaskの実践
# View current umask
umask
# Output: 0022
# Create a new file to test
touch test.txt
ls -l test.txt
# -rw-r--r-- ← 644 (666 - 022)
mkdir testdir
ls -ld testdir
# drwxr-xr-x ← 755 (777 - 022)
# Create after changing umask
umask 0077
touch secret.txt
ls -l secret.txt
# -rw------- ← 600 (666 - 077)
出力:
TEXT0022 -rw-r--r-- 1 alice alice 0 Jul 7 10:23 test.txt drwxr-xr-x 2 alice alice 4096 Jul 7 10:23 testdir -rw------- 1 alice alice 0 Jul 7 10:23 secret.txt
(12) ▶ 総合例:Webアプリケーションの正しい権限設定
# Assume /var/www/myapp is the web application directory
# 1. Set owner and group
sudo chown -R www-data:www-data /var/www/myapp
# 2. Directory permissions: 755 (owner read/write/execute, others read/execute)
find /var/www/myapp -type d -exec chmod 755 {} \;
# 3. File permissions: 644 (owner read/write, others read only)
find /var/www/myapp -type f -exec chmod 644 {} \;
# 4. Executable scripts: 755
chmod 755 /var/www/myapp/deploy.sh
# 5. Sensitive files: 600
chmod 600 /var/www/myapp/.env
# 6. Upload directory: writable directory
chmod 775 /var/www/myapp/uploads
❓ よくある質問
Q: なぜ
chmod 777は安全ではないのですか? A:chmod 777は全ユーザーに読み取り、書き込み、実行を許可します。いずれかのアカウントが侵害されると、攻撃者がファイルを改ざん・削除でき、権限昇格のリスクが生じます。本番環境では777の代わりに、グループ権限(例:775)やACLで共有アクセスを実現してください。
Q: 新規作成ファイルはなぜ755ではなく644がデフォルトですか? A: セキュリティ原則のため — 新しいファイルにはデフォルトで実行権限が付与されません。Linuxは大部分のファイルに実行権限が不要であると想定しており、必要な場合は
chmod +xで明示的に追加します。
Q: SSHログイン後に
/etcに書き込めないのはなぜですか? A:/etcはroot所有で、一般ユーザーには読み取り権限(r-x)しかありません。/etc下のファイルを修正するにはsudoで権限昇格する必要があります。これはシステムを保護するセキュリティ設計です。
Q: SUIDプログラムのリスクは? A: SUIDプログラムはファイル所有者(通常root)の権限で実行されます。SUIDプログラムに脆弱性がある場合、一般ユーザーがrootに権限昇格できる可能性があります。そのため、SUIDは必要なプログラム(
passwd、pingなど)にのみ設定し、定期的に監査してください。
Q:
chmod -R 755でディレクトリを再帰的に変更するのは安全ですか? A: 状況によります。ディレクトリにスクリプトファイルが含まれる場合、755は合理的です。しかしfindでファイル(644)とディレクトリ(755)に異なる権限を設定する方が安全で正確です。
📖 まとめ
ls -l最初の列:-rwxr-xr-x= ファイルタイプ + 所有者/グループ/その他 × rwx- chmod数値記法:
755はスクリプト、644はファイル、600はプライベートファイル - chmodシンボリック記法:
u+xは所有者に実行権限追加、go-wはグループとその他の書き込み権限削除 - chownは所有者を変更(
alice:developers)、-Rで再帰的 - umaskはデフォルト権限を制御、一般的な値:022(644/755)、077(600/700)
- 特殊権限:SUID(4)は所有者を継承、SGID(2)はグループを継承、Sticky Bit(1)は削除を防止
📝 練習問題
- 基本(難易度 ⭐):スクリプトを作成し、
chmod +xで実行権限を追加して実行し、数値記法でファイルを644、ディレクトリを755に設定する - 中級(難易度 ⭐⭐):シンボリック記法でファイルのグループとその他の権限をすべて削除し、テストユーザー(
sudo useradd testuser)を作成し、chownでファイルの所有権をそのユーザーに移す - 上級(難易度 ⭐⭐⭐):共有ディレクトリを作成し、SGID(
chmod g+s)で新規ファイルがグループを継承するように設定し、find+chmodでWebプロジェクトのファイルを644、ディレクトリを755に一括設定する