mcpField
U
user-management

user-management

github.com/bagelholeクラウド・DevOps1.2k356 回インストール23 週間前に更新
リポジトリ

user-management について

Linux管理者向けのユーザー・アクセス管理ガイドです。アカウント、グループ、sudo、PAM、LDAP連携、一括操作、監査の手順を扱います。

主な機能

  1. 01ユーザーアカウントの作成・変更・削除
  2. 02グループとファイル権限の管理
  3. 03細かなsudoアクセス制御
  4. 04PAMパスワードポリシーの設定
  5. 05LDAPまたはActive Directoryとの連携
  6. 06ユーザーの一括登録とアクセス監査

活用シーン

  1. 01Linuxサーバーチームのユーザーとグループ設定
  2. 02コマンドやサービス単位でのsudo制限
  3. 03ホストの集中認証への接続
  4. 04アカウントの一括登録とアクセス確認

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/bagelhole/devops-security-agent-skills --skill user-management

インストール後、Claude Code で/user-managementを入力して読み込まれたか確認します。

詳細

ユーザー管理

Linux システム上のユーザー、グループ、権限、sudo アクセス、PAM モジュール、LDAP 連携を管理します。一括ユーザー操作やアクセス監査に役立つスクリプトも含まれています。

使用する場面

  • Linux サーバー上でローカルユーザーアカウントを作成、管理する
  • きめ細かな権限制御で sudo アクセスを設定する
  • チーム向けにグループベースのアクセス制御を設定する
  • 集中認証のために Linux ホストを LDAP または Active Directory と連携する
  • ユーザーアカウント、権限、アクセスパターンを監査する
  • ユーザーの一括プロビジョニングとプロビジョニング解除を自動化する

前提条件

  • 対象システムへの root または sudo アクセス
  • shadow-utils パッケージ(useradd、usermod などを提供) -- デフォルトでインストールされています
  • PAM 設定用の libpam-modules
  • LDAP の場合:sssd、realmd、libpam-ldapd、または nslcd パッケージ
  • 監査用の auditd パッケージ

ユーザー操作

ユーザーの作成
# Create a user with home directory, default shell, and comment
useradd -m -s /bin/bash -c "Jane Smith" jsmith

# Set the user's password interactively
passwd jsmith

# Create a user with a specific UID and primary group
useradd -m -s /bin/bash -u 1500 -g developers -c "Deploy Account" deploy

# Create a system account (no home, no login shell) for running services
useradd -r -s /usr/sbin/nologin -d /opt/myapp -c "MyApp Service Account" myapp

# Create a user with an expiration date (contractor access)
useradd -m -s /bin/bash -e 2025-12-31 -c "Contractor - Bob Lee" blee

# Create user and add to multiple supplementary groups at creation time
useradd -m -s /bin/bash -G docker,developers,ssh-users -c "Dev User" devuser
ユーザーの変更
# Add a user to a supplementary group (preserving existing groups with -a)
usermod -aG sudo jsmith
usermod -aG docker,developers jsmith

# Change the user's login shell
usermod -s /bin/zsh jsmith

# Change the user's home directory and move existing files
usermod -d /home/jsmith-new -m jsmith

# Lock a user account (disable login without deleting)
usermod -L jsmith

# Unlock a user account
usermod -U jsmith

# Set an account expiration date
usermod -e 2025-06-30 blee

# Change a user's login name
usermod -l jsmith-new jsmith

# Force password change on next login
chage -d 0 jsmith

# Set password aging: min 7 days, max 90 days, warn 14 days before
chage -m 7 -M 90 -W 14 jsmith

# View password aging info
chage -l jsmith
ユーザーの削除
# Remove a user and their home directory
userdel -r jsmith

# Remove a user but keep their home directory (for auditing)
userdel jsmith

# Find and reassign files owned by a deleted user (by UID)
find / -uid 1500 -exec chown newowner:newgroup {} \;

グループ管理

# Create a new group
groupadd developers

# Create a group with a specific GID
groupadd -g 2000 devops

# Add a user to a group
usermod -aG developers jsmith
# Alternative using gpasswd
gpasswd -a jsmith developers

# Remove a user from a group
gpasswd -d jsmith developers

# Set group administrators (can add/remove members without root)
gpasswd -A jsmith developers

# Delete a group
groupdel developers

# List all groups a user belongs to
groups jsmith
id jsmith

# List all members of a group
getent group developers

# Show all groups on the system
cat /etc/group | cut -d: -f1 | sort

sudo の設定

# Always edit sudoers via visudo (syntax validation prevents lockout)
visudo

# Better: use drop-in files in /etc/sudoers.d/
visudo -f /etc/sudoers.d/developers
/etc/sudoers.d/developers
# Allow the developers group to restart specific services
%developers ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart myapp, /usr/bin/systemctl status myapp

# Allow a deploy user full sudo with no password
deploy ALL=(ALL) NOPASSWD: ALL

# Allow ops team to run docker commands only
%ops ALL=(ALL) NOPASSWD: /usr/bin/docker, /usr/bin/docker-compose

# Allow a user to run commands as a specific service account
jsmith ALL=(myapp) NOPASSWD: /opt/myapp/bin/*

# Restrict to specific hosts (useful with centralized sudoers)
jsmith dbservers=(root) /usr/bin/systemctl restart postgresql

# Log all sudo commands to a dedicated file
Defaults log_output
Defaults!/usr/bin/sudoreplay !log_output
Defaults logfile="/var/log/sudo.log"

# Require password re-entry every 5 minutes (default is 15)
Defaults timestamp_timeout=5

# Require password for sudo even if user has NOPASSWD elsewhere
Defaults:jsmith !authenticate
# Validate sudoers syntax without applying
visudo -c

# Check what sudo permissions a user has
sudo -l -U jsmith

# Test a specific sudo command as a user
sudo -u myapp /opt/myapp/bin/healthcheck.sh

ファイル権限と ACL

# Standard permissions
chmod 755 /opt/myapp           # rwxr-xr-x
chmod 640 /etc/myapp.conf      # rw-r-----
chmod u+x script.sh            # Add execute for owner
chmod g+w shared-dir/          # Add write for group
chmod o-rwx private-file       # Remove all permissions for others

# Change ownership
chown deploy:developers /opt/myapp
chown -R deploy:developers /opt/myapp/   # Recursive

# Set the SGID bit (new files inherit group ownership)
chmod g+s /opt/shared/

# Set the sticky bit (only owner can delete their files)
chmod +t /tmp/shared/

# Access Control Lists (ACLs) for fine-grained control
# Grant read-execute to a specific user on a directory
setfacl -m u:jsmith:rx /opt/myapp/logs/

# Grant read-write to a group
setfacl -m g:developers:rw /opt/shared/

# Set default ACL (applied to new files created in the directory)
setfacl -d -m g:developers:rw /opt/shared/

# View ACLs
getfacl /opt/shared/

# Remove a specific ACL entry
setfacl -x u:jsmith /opt/myapp/logs/

# Remove all ACLs
setfacl -b /opt/shared/

PAM の設定

# PAM config files are in /etc/pam.d/
# Each file controls auth for a specific service (sshd, login, sudo, etc.)

# Enforce password complexity via pam_pwquality
# /etc/pam.d/common-password (Debian) or /etc/pam.d/system-auth (RHEL)
password requisite pam_pwquality.so retry=3 minlen=12 dcredit=-1 ucredit=-1 ocredit=-1 lcredit=-1

# Configure /etc/security/pwquality.conf
minlen = 12
dcredit = -1
ucredit = -1
ocredit = -1
lcredit = -1
maxrepeat = 3
dictcheck = 1

# Limit concurrent logins per user
# /etc/security/limits.conf
jsmith hard maxlogins 3
@developers hard maxlogins 5

# Lock account after 5 failed login attempts
# /etc/pam.d/common-auth (Debian)
auth required pam_faillock.so preauth silent deny=5 unlock_time=900
auth required pam_faillock.so authfail deny=5 unlock_time=900

# View failed login attempts
faillock --user jsmith

# Unlock a locked account
faillock --user jsmith --reset

LDAP / Active Directory との連携

# Install SSSD and realmd for AD integration (Ubuntu/Debian)
apt install -y sssd realmd adcli sssd-tools libnss-sss libpam-sss

# Install SSSD and realmd (RHEL/CentOS)
dnf install -y sssd realmd adcli sssd-tools oddjob oddjob-mkhomedir

# Discover and join an Active Directory domain
realm discover corp.example.com
realm join corp.example.com -U [email protected]

# Verify the join
realm list

# Allow specific AD groups to log in
realm permit -g "Linux [email protected]"
realm permit -g "[email protected]"

# Deny all except permitted groups
realm deny --all
realm permit -g "Linux [email protected]"

# Restart SSSD after config changes
systemctl restart sssd

# Test LDAP user lookup
id jsmith
getent passwd jsmith

# Grant sudo to an AD group
echo '%linux\ admins ALL=(ALL) ALL' > /etc/sudoers.d/ad-admins

一括ユーザー管理スクリプト

CSV からのユーザー一括作成
#!/bin/bash
# bulk-create-users.sh
# CSV format: username,fullname,groups,shell
# Example: jsmith,Jane Smith,developers;docker,/bin/bash

CSV_FILE="${1:?Usage: $0 <users.csv>}"

while IFS=',' read -r username fullname groups shell; do
  # Skip header line
  [[ "$username" == "username" ]] && continue

  if id "$username" &>/dev/null; then
    echo "SKIP: User $username already exists"
    continue
  fi

  # Replace semicolons with commas for -G flag
  group_list="${groups//;/,}"

  useradd -m -s "$shell" -c "$fullname" -G "$group_list" "$username"
  # Generate a random temporary password
  temp_pass=$(openssl rand -base64 12)
  echo "$username:$temp_pass" | chpasswd
  chage -d 0 "$username"   # Force password change at first login

  echo "CREATED: $username (groups: $group_list) temp-pass: $temp_pass"
done < "$CSV_FILE"
アクセス監査に便利なコマンド
# List non-system users (UID >= 1000)
awk -F: '$3 >= 1000 && $3 < 65534 { printf "%-20s UID=%-6s Shell=%s\n", $1, $3, $7 }' /etc/passwd

# List users with sudo access
getent group sudo wheel 2>/dev/null

# Find accounts that have never logged in
lastlog | awk '$0 ~ /Never logged in/ { print $1 }'

# Find accounts with empty passwords
awk -F: '($2 == "" || $2 == "!") { print $1 }' /etc/shadow 2>/dev/null

トラブルシューティング

症状診断コマンド一般的な対処法
ユーザーがログインできないpasswd -S username, faillock --user usernameアカウントのロックを解除し、パスワードをリセットして、シェルを確認します
「not in sudoers」エラーsudo -l -U usernameユーザーを sudo グループに追加するか、sudoers.d ファイルを作成します
グループメンバーシップが反映されないid username, groups username新しいグループを反映するには、ユーザーがログアウトして再ログインする必要があります
LDAP/AD ユーザーが見つからないid aduser, sssctl user-show aduserSSSD の状態を確認し、キャッシュをクリアします:sss_cache -E
ファイルへのアクセスが拒否されるls -la file, getfacl file所有者と権限を修正し、SELinux コンテキストを確認します
認証失敗後に PAM によってロックされるfaillock --user usernamefaillock --user username --reset
ホームディレクトリが作成されない/etc/login.defs の CREATEHOME を確認しますuseradd -m を使用するか、pam_mkhomedir を有効にします
パスワードポリシーが適用されない/etc/pam.d/common-password を確認しますpam_pwquality をインストールして設定します

関連スキル

  • linux-administration -- Linux サーバーの一般的な管理
  • ssh-configuration -- 管理対象ユーザー向けの SSH 鍵ベース認証
  • systemd-services -- サービスアカウントと systemd ユーザーインスタンス
  • performance-tuning -- cgroups と ulimits によるユーザーごとのリソース制限

コメント