user-permissions について
Linux 管理者向けに、ユーザー、グループ、権限の管理方法を示すスキルです。sudo、ACL、特殊権限のコマンドと設定例を扱います。
主な機能
- 01ユーザーアカウントの確認と管理
- 02グループの作成と管理
- 03sudo と sudoers の設定
- 04ACL の確認と設定
- 05SUID、SGID、スティッキービットの管理
活用シーン
- 01グループとシェルを指定した開発者アカウントの作成
- 02共有ディレクトリのグループ権限の設定
- 03sudo で実行可能なコマンドの制限
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ chaterm/ terminal-skills --skill user-permissionsインストール後、Claude Code で/user-permissionsを入力して読み込まれたか確認します。
詳細
ユーザーと権限の管理
概要
Linux のユーザー管理、グループ管理、sudo の設定、ACL 権限などのスキルです。
ユーザー管理
ユーザーの表示
# Current user
whoami
id
# User information
id username
finger username
# All users
cat /etc/passwd
getent passwd
# Logged in users
who
w
last # Login history
ユーザーの操作
# Create user
useradd username
useradd -m -s /bin/bash username # Create home directory, specify shell
useradd -G group1,group2 username # Specify supplementary groups
# Modify user
usermod -aG groupname username # Add to group
usermod -s /bin/zsh username # Change shell
usermod -L username # Lock user
usermod -U username # Unlock user
# Delete user
userdel username
userdel -r username # Also delete home directory
# Change password
passwd username
passwd -l username # Lock password
passwd -u username # Unlock password
chage -l username # View password policy
グループ管理
グループの表示
# User's groups
groups username
id -Gn username
# All groups
cat /etc/group
getent group
# Group members
getent group groupname
グループの操作
# Create group
groupadd groupname
groupadd -g 1001 groupname # Specify GID
# Modify group
groupmod -n newname oldname # Rename
# Delete group
groupdel groupname
# Manage group members
gpasswd -a username groupname # Add user
gpasswd -d username groupname # Remove user
gpasswd -M user1,user2 groupname # Set member list
sudo の設定
基本的な使い方
# Execute as root
sudo command
sudo -i # Switch to root shell
sudo -u username command # Execute as another user
# View permissions
sudo -l
sudoers の設定
# Edit sudoers (recommended method)
visudo
# Or edit files under /etc/sudoers.d/
visudo -f /etc/sudoers.d/username
よくある設定例
# /etc/sudoers.d/username
# Full privileges
username ALL=(ALL:ALL) ALL
# No password required
username ALL=(ALL) NOPASSWD: ALL
# Specific commands
username ALL=(ALL) /usr/bin/systemctl restart nginx
# Specific commands without password
username ALL=(ALL) NOPASSWD: /usr/bin/docker
# Group privileges
%groupname ALL=(ALL) ALL
ACL 権限
ACL の表示
getfacl file
getfacl -R dir # Recursive view
ACL の設定
# Set user permissions
setfacl -m u:username:rwx file
setfacl -m u:username:rx dir
# Set group permissions
setfacl -m g:groupname:rx file
# Set default ACL (new files inherit)
setfacl -d -m u:username:rwx dir
# Recursive set
setfacl -R -m u:username:rx dir
# Remove ACL
setfacl -x u:username file # Remove specific
setfacl -b file # Remove all
特殊な権限
SUID/SGID/Sticky
# SUID (4) - Execute as file owner
chmod u+s file
chmod 4755 file
# SGID (2) - Execute as file group/directory inherits group
chmod g+s file
chmod 2755 dir
# Sticky (1) - Only owner can delete
chmod +t dir
chmod 1777 dir
# View
ls -la
# -rwsr-xr-x SUID
# -rwxr-sr-x SGID
# drwxrwxrwt Sticky
よくあるシナリオ
シナリオ 1:開発者ユーザーの作成
# Create user and group
groupadd developers
useradd -m -s /bin/bash -G developers devuser
# Set password
passwd devuser
# Configure sudo
echo "devuser ALL=(ALL) NOPASSWD: /usr/bin/docker, /usr/bin/systemctl" > /etc/sudoers.d/devuser
chmod 440 /etc/sudoers.d/devuser
シナリオ 2:共有ディレクトリの権限
# Create shared directory
mkdir /shared
groupadd shared
chown root:shared /shared
chmod 2775 /shared # SGID ensures new files inherit group
# Add users to group
usermod -aG shared user1
usermod -aG shared user2
シナリオ 3:特定のコマンドのみをユーザーに許可する
# /etc/sudoers.d/limited-user
limited ALL=(ALL) NOPASSWD: /usr/bin/systemctl status *, /usr/bin/journalctl
トラブルシューティング
| 問題 | 解決方法 |
|---|---|
| sudo 権限が拒否される | /etc/sudoers.d/ の設定を確認します |
| ユーザーがログインできない | シェルとパスワードのロック状態を確認します |
| グループ権限が機能しない | 再ログインするか、newgrp groupname を実行します |
| ACL が機能しない | ファイルシステムが ACL をサポートしているか確認します |
コメント
Markdown 対応