Chmod計算機
Unixファイル権限を計算。8進数と記号表記(rwx)を相互変換。
| 読み取り(r) | 書き込み(w) | 実行(x) | 8進数 | 記号表記 | |
|---|---|---|---|---|---|
| オーナー | 6 | rw- | |||
| グループ | 4 | r-- | |||
| その他 | 4 | r-- |
8進数
644
記号表記
rw-r--r--
chmodコマンド
chmod 644 filename
Chmod計算機は、Unix/Linuxのファイル権限設定を理解し、生成するのに役立ちます。Unixベースのシステムでは、すべてのファイルとディレクトリには、所有者、グループ、その他のユーザーが読み取り、書き込み、実行できるかどうかを制御する権限ビットのセットがあります。`chmod`コマンドは、シンボリック表記(rwx)または8進数(例:755)を使用して権限を設定します。
所有者、グループ、その他の列の読み取り、書き込み、実行のチェックボックスをトグルすると、ツールは自動的に同等の8進数値とシンボリック文字列を計算します。逆に644などの8進数値を入力すると、各ユーザークラスでどの権限が有効かが表示されます。
一般的な権限パターンには755(所有者はすべて可能; グループと他者は読み取りと実行のみ)、644(所有者は読み書き; その他は読み取りのみ)、600(所有者のみ読み書き可能 — SSHキーに典型的)などがあります。
よくある質問
コード実装
import os
import stat
# Set permissions using octal notation
os.chmod('script.sh', 0o755) # rwxr-xr-x (owner: rwx, group: r-x, others: r-x)
os.chmod('data.txt', 0o644) # rw-r--r-- (owner: rw-, group: r--, others: r--)
os.chmod('private.key', 0o600) # rw------- (owner: rw-, no access for others)
# Read current permissions
file_stat = os.stat('script.sh')
mode = file_stat.st_mode
print(oct(stat.S_IMODE(mode))) # e.g. '0o755'
# Check specific permissions using stat constants
is_owner_executable = bool(mode & stat.S_IXUSR)
is_group_readable = bool(mode & stat.S_IRGRP)
is_others_writable = bool(mode & stat.S_IWOTH)
print(f"Owner executable: {is_owner_executable}")
# Build permissions programmatically
perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH # 644
os.chmod('config.ini', perms)
# Recursively set permissions on a directory tree
import pathlib
for path in pathlib.Path('/var/www/html').rglob('*'):
if path.is_dir():
os.chmod(path, 0o755)
else:
os.chmod(path, 0o644)Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.