Linuxファイルパーミッションはシステムセキュリティの根幹です。Unix系システムのすべてのファイルとディレクトリには、誰が読み取り、書き込み、実行できるかを決定するメタデータがあります。Webアプリのデプロイ、サーバーの強化、デプロイスクリプトのデバッグなど、chmodコマンドとファイルパーミッション番号の理解は必須です。この包括的なchmod計算機ガイドでは、基本概念から特殊ビットまですべてをカバーします。
インタラクティブなchmod計算機でパーミッションを即座に視覚化できます。
Chmod Calculator →1. Linuxファイルパーミッションとは?
Linuxのすべてのファイルとディレクトリには3つのユーザーカテゴリがあります。所有者は通常ファイルの作成者です。グループはアクセス権を共有するユーザーの集合です。その他はシステム上の他のすべてのユーザーを指します。ls -lコマンドでパーミッション文字列を確認できます。
各カテゴリには3種類のアクセス権を付与できます。読み取り (r) はファイル内容の表示やディレクトリの一覧表示を許可します。書き込み (w) はファイルの変更やディレクトリ内のエントリの作成・削除を許可します。実行 (x) はプログラムとしての実行やディレクトリへの移動を許可します。
典型的な <code>ls -l</code> 出力の例:
-rwxr-xr-- 1 alice developers 4096 Feb 20 deploy.sh
所有者: rwx (7) | グループ: r-x (5) | その他: r-- (4)
パーミッション番号: 7542. パーミッション番号の理解(8進数表記)
Linuxは各パーミッションセットを0から7の8進数で表します:読み取り = 4、書き込み = 2、実行 = 1。755のような3桁の数字は3つのカテゴリすべてをエンコードします。
8進数リファレンステーブル:
| 8進数 | シンボル | 意味 |
|---|---|---|
0 | --- | No permission |
1 | --x | Execute only |
2 | -w- | Write only |
3 | -wx | Write + Execute |
4 | r-- | Read only |
5 | r-x | Read + Execute |
6 | rw- | Read + Write |
7 | rwx | Read + Write + Execute |
完全なパーミッション番号を計算するには、各桁を個別に計算します。例:所有者 読み取り+書き込み (4+2=6)、グループ 読み取り (4)、その他 なし (0) で 640。
3. chmodパーミッションリファレンステーブル
最もよく使われるchmodパーミッションの包括的なリファレンステーブルです。
| 8進数 | シンボル | 意味 | 典型的な用途 |
|---|---|---|---|
777 | rwxrwxrwx | 全員にフルアクセス | 一時的なデバッグのみ;本番環境では絶対に使用しない |
755 | rwxr-xr-x | 所有者フル;グループ/その他 読み取り+実行 | ディレクトリ、実行スクリプト、Webサーバーのドキュメントルート |
750 | rwxr-x--- | 所有者フル;グループ 読み取り+実行;その他 なし | 共有プロジェクトディレクトリ |
700 | rwx------ | 所有者のみフルアクセス | ~/.sshディレクトリ、プライベートスクリプト |
666 | rw-rw-rw- | 全員に読み取り+書き込み;実行なし | まれに使用;一時的な共有ファイル |
644 | rw-r--r-- | 所有者 読み取り+書き込み;グループ/その他 読み取り | HTML、CSS、PHPファイル、設定ファイル |
640 | rw-r----- | 所有者 読み取り+書き込み;グループ 読み取り;その他 なし | ログファイル、機密設定 |
600 | rw------- | 所有者のみ読み取り+書き込み | SSH秘密鍵、.envファイル、データベース資格情報 |
400 | r-------- | 所有者のみ読み取り | SSL証明書、読み取り専用シークレット |
775 | rwxrwxr-x | 所有者フル;グループフル;その他 読み取り+実行 | 共有アップロードディレクトリ |
4. chmodコマンドの使い方
chmodコマンドはファイルとディレクトリのパーミッションを変更します。数値(8進数)モードとシンボリックモードの2つをサポートしています。
数値モード
数値モードでは3桁または4桁の8進数を指定します:
chmod 755 script.sh # rwxr-xr-x
chmod 644 config.json # rw-r--r--
chmod 600 id_rsa # rw-------
chmod 400 cert.pem # r--------
chmod -R 755 /var/www/html # Recursive on directoryシンボリックモード
シンボリックモードは chmod [ugoa][+-=][rwxXst] の構文を使用します:
- 対象: u (所有者)、g (グループ)、o (その他)、a (全員)
- 演算子: + (追加)、- (削除)、= (正確に設定)
- パーミッション: r (読み取り)、w (書き込み)、x (実行)
chmod u+x script.sh # Add execute for owner
chmod g-w file.txt # Remove write from group
chmod o+r document.pdf # Add read for others
chmod a+x run.sh # Add execute for all
chmod u=rwx,go=rx dir/ # Owner=rwx, group+others=rx (755)
chmod go-rwx secret.key # Remove all from group+others再帰的chmod
chmod -Rで再帰的に変更します:
# Set directories to 755 and files to 644 (web server standard)
find /var/www/html -type d -exec chmod 755 {} \;
find /var/www/html -type f -exec chmod 644 {} \;
# Make all .sh files executable
find /opt/scripts -name "*.sh" -exec chmod 755 {} \;chmod計算機を使って正確なコマンドを生成:
5. 開発者向け一般的なchmodパターン
ファイルタイプごとに適切なパーミッション設定が異なります。ここでは必須のchmodパターンを紹介します。
Webサーバーファイル
標準:ファイルは644、ディレクトリは755:
# Web server document root
chmod 755 /var/www/html
chmod 644 /var/www/html/index.html
chmod 644 /var/www/html/style.css
chmod 644 /var/www/html/.htaccess
# PHP / application files
chmod 644 /var/www/html/app/*.php
chmod 755 /var/www/html/app/storageスクリプトと実行ファイル
共有スクリプトは755、プライベートは700:
# Shared executable scripts
chmod 755 deploy.sh
chmod 755 /usr/local/bin/my-tool
# Private scripts (owner only)
chmod 700 backup-db.sh
chmod 700 cleanup.shSSH鍵と設定
SSHはパーミッションに非常に厳格です:
chmod 700 ~/.ssh # SSH directory
chmod 600 ~/.ssh/id_rsa # Private key
chmod 600 ~/.ssh/id_ed25519 # Private key (Ed25519)
chmod 644 ~/.ssh/id_rsa.pub # Public key
chmod 600 ~/.ssh/authorized_keys # Authorized keys
chmod 600 ~/.ssh/config # SSH config file環境ファイルとシークレット
APIキーを含むファイルは所有者のみアクセス:
chmod 600 .env # Environment variables
chmod 600 .env.production # Production secrets
chmod 600 database.yml # Database credentials
chmod 600 credentials.json # API credentials
chmod 400 ssl-cert.pem # SSL certificate (read-only)アップロードディレクトリ
アップロードディレクトリにはグループ書き込み権限が必要:
chmod 775 /var/www/html/uploads
chmod 775 /var/www/html/tmp
chmod 775 /var/www/html/cache
# Ensure web server group owns the directory
# chown www-data:www-data /var/www/html/uploads6. 特殊パーミッション:setuid、setgid、スティッキービット
標準の9ビットに加え、Linuxは実行動作を変更する3つの特殊ビットをサポートします。
プロセスはファイル所有者の権限で実行されます。例:/usr/bin/passwd。
ディレクトリでは、新しいファイルが自動的にディレクトリのグループを継承します。
ファイル所有者またはrootのみが削除可能。例:/tmp (1777)。
特殊パーミッションの設定:
# SUID: run as file owner
chmod 4755 /usr/bin/passwd
chmod u+s /usr/bin/my-tool
# SGID: inherit group on directory
chmod 2755 /shared/project
chmod g+s /shared/project
# Sticky Bit: prevent deletion by others
chmod 1777 /tmp
chmod +t /shared/uploads
# Combine: SGID + standard permissions
chmod 2775 /team/shared-dir
# Find files with special permissions
find / -perm -4000 -type f 2>/dev/null # Find SUID files
find / -perm -2000 -type f 2>/dev/null # Find SGID files
find / -perm -1000 -type d 2>/dev/null # Find sticky dirs7. ファイルパーミッションのベストプラクティス
ファイルパーミッションは重要な防御層です。
最小権限の原則に従う。必要最小限のパーミッションのみ付与する。
本番環境でchmod 777は絶対に使わない。全ユーザーにフルアクセスを与えてしまう。
定期的にパーミッションを監査する。find / -perm -777 -type fを使用する。
world-readableの代わりにグループを使う。
再帰的chmodに注意する。パスを必ず確認する。
パーミッション要件を文書化する。
umaskで安全なデフォルト値を設定する。umask 027を設定する。
chmod計算機ツールで素早く正確なパーミッションを決定できます。
8. よくある質問
chmod 755の意味は?
chmod 755は所有者にフルアクセス(rwx = 7)、グループとその他に読み取りと実行のアクセス(r-x = 5)を設定します。実行スクリプトとディレクトリの標準パーミッションです。
chmod 755と644の違いは?
chmod 755 (rwxr-xr-x) は全カテゴリに実行を許可し、ディレクトリとスクリプト向けです。chmod 644 (rw-r--r--) は実行を除去し書き込みを所有者のみに制限、通常ファイル向けです。
Linuxでファイルパーミッションを確認する方法は?
ls -laコマンドで詳細情報を表示します。statコマンドでさらに詳しい情報を確認できます。
最も安全なファイルパーミッションは?
実用的には400(所有者読み取りのみ)または600(所有者読み書きのみ)が最も制限的です。
MacでChmodは使える?
はい。macOSはBSD Unixベースで、Linuxと同じ構文でchmodを完全サポートしています。
無料オンラインchmod計算機で正確な値を計算。
Chmod Calculator →