許可ビット
-rwxr-xr-- 1 user group filename
├─┤├─┤├─┤
│ │ └── Other users (other)
│ └───── Group (group)
└───────── Owner (user/owner)| キャラクター | 番号 | 意味 |
|---|---|---|
r | 4 | 読む |
w | 2 | 書く |
x | 1 | 実行 (ディレクトリの場合は Enter) |
- | 0 | 許可がありません |
共通の許可番号
| 番号 | 許可 | 説明 |
|---|---|---|
777 | rwxrwxrwx | すべてのユーザーに完全な権限を与える (慎重に使用してください) |
755 | rwxr-xr-x | 所有者は完全、他の人は読み取り+実行 |
750 | rwxr-x--- | 所有者に対しては完全、グループに対しては読み取り+実行 |
700 | rwx------ | オーナー様専用完全予約制 |
644 | rwr—r— | 所有者は読み取り+書き込み、その他は読み取り専用 |
640 | ルルル----- | 所有者は読み取り+書き込み、グループは読み取り専用 |
600 | わー----- | 所有者のみが読み取り+書き込み可能 |
555 | r-xr-xr-x | 全員に読み取り+実行 |
444 | r—r—r— | 全員が読み取り専用 |
chmodコマンド
数値モード
chmod 755 filename
chmod 644 filename
chmod -R 755 directory # Recursiveシンボリックモード
# Add permissions
chmod u+x file # Add execute for owner
chmod g+w file # Add write for group
chmod o-r file # Remove read for others
chmod a+r file # Add read for everyone
# Set exact permissions
chmod u=rwx,g=rx,o=r file
# Common combinations
chmod +x script.sh # Add execute permission
chmod -w file.txt # Remove write permissionターゲット: u=所有者、g=グループ、o=その他、a=すべて
操作: +=追加、-=削除、==設定
chownコマンド
# Change owner
sudo chown username filename
# Change owner and group
sudo chown username:groupname filename
# Change group only
sudo chown :groupname filename
# Or use chgrp
chgrp groupname filename
# Recursive
sudo chown -R username:groupname directory特別な許可ビット
SUID (ユーザーIDの設定) - 4xxx
ファイルは、それを実行するユーザーではなく、所有者として実行されます。
chmod u+s file
chmod 4755 file
# Example: passwd command uses SUID
ls -la /usr/bin/passwd
# -rwsr-xr-x 1 root root ...SGID (グループ ID の設定) - 2xxx
ディレクトリ内に作成された新しいファイルは、ディレクトリのグループを継承します。
chmod g+s directory
chmod 2755 directoryスティッキービット - 1xxx
ファイルの所有者のみがディレクトリ (/tmp など) 内でそのファイルを削除できます。
chmod +t directory
chmod 1777 directory
ls -la /tmp
# drwxrwxrwt ... # Note the trailing tデフォルトの権限 (umask)
# Check current umask
umask
# Common value: 0022
# umask calculation:
# File default permissions = 666 - umask = 644
# Directory default permissions = 777 - umask = 755
# Temporary change
umask 027 # Files 640, directories 750
# Permanent change: add to ~/.bashrc
echo "umask 027" >> ~/.bashrcACL 拡張アクセス許可
基本的な権限が十分に柔軟ではない場合:
# Install ACL tools
sudo apt install acl
# Set permissions for a specific user
setfacl -m u:username:rwx filename
# Set permissions for a specific group
setfacl -m g:groupname:rx filename
# Set default ACL (new files auto-inherit)
setfacl -d -m u:username:rwx directory
# View ACL
getfacl filename
# Remove all ACL
setfacl -b filenameユーザーとグループの管理
# Check current user info
id
# Check user's groups
groups username
# Add user to a group
sudo usermod -aG groupname username
# Create a new group
sudo groupadd groupname
# Common system groups
# sudo - Administrator privileges
# docker - Docker operations
# video - Camera/GPU access
# audio - Audio devices
# dialout - Serial port devices
# plugdev - Pluggable devices共通シナリオ権限設定
# Web directory
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
# SSH keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
# Shared directory (group read+write)
sudo mkdir /shared
sudo chown :shared-group /shared
sudo chmod 2775 /shared
# Script files
chmod 755 script.sh # Everyone can execute
chmod 700 script.sh # Only owner can execute