| name | user-management |
| description | Manage users, groups, and permissions on Linux systems. Configure sudo and access controls. Use when managing system access. |
| license | MIT |
| metadata | {"author":"devops-skills","version":"1.0"} |
User Management
Manage users, groups, permissions, sudo access, PAM modules, and LDAP integration on Linux systems. Includes practical scripts for bulk user operations and access auditing.
When to Use
- Creating and managing local user accounts on Linux servers
- Configuring sudo access with fine-grained privilege controls
- Setting up group-based access control for teams
- Integrating Linux hosts with LDAP or Active Directory for centralized auth
- Auditing user accounts, permissions, and access patterns
- Automating bulk user provisioning and deprovisioning
Prerequisites
- Root or sudo access on the target system
shadow-utils package (provides useradd, usermod, etc.) -- installed by default
libpam-modules for PAM configuration
- For LDAP:
sssd, realmd, libpam-ldapd, or nslcd packages
- For auditing:
auditd package
User Operations
Creating Users
useradd -m -s /bin/bash -c "Jane Smith" jsmith
passwd jsmith
useradd -m -s /bin/bash -u 1500 -g developers -c "Deploy Account" deploy
useradd -r -s /usr/sbin/nologin -d /opt/myapp -c "MyApp Service Account" myapp
useradd -m -s /bin/bash -e 2025-12-31 -c "Contractor - Bob Lee" blee
useradd -m -s /bin/bash -G docker,developers,ssh-users -c "Dev User" devuser
Modifying Users
usermod -aG sudo jsmith
usermod -aG docker,developers jsmith
usermod -s /bin/zsh jsmith
usermod -d /home/jsmith-new -m jsmith
usermod -L jsmith
usermod -U jsmith
usermod -e 2025-06-30 blee
usermod -l jsmith-new jsmith
chage -d 0 jsmith
chage -m 7 -M 90 -W 14 jsmith
chage -l jsmith
Deleting Users
userdel -r jsmith
userdel jsmith
find / -uid 1500 -exec chown newowner:newgroup {} \;
Group Management
groupadd developers
groupadd -g 2000 devops
usermod -aG developers jsmith
gpasswd -a jsmith developers
gpasswd -d jsmith developers
gpasswd -A jsmith developers
groupdel developers
groups jsmith
id jsmith
getent group developers
cat /etc/group | cut -d: -f1 | sort
Sudo Configuration
visudo
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
visudo -c
sudo -l -U jsmith
sudo -u myapp /opt/myapp/bin/healthcheck.sh
File Permissions and ACLs
chmod 755 /opt/myapp
chmod 640 /etc/myapp.conf
chmod u+x script.sh
chmod g+w shared-dir/
chmod o-rwx private-file
chown deploy:developers /opt/myapp
chown -R deploy:developers /opt/myapp/
chmod g+s /opt/shared/
chmod +t /tmp/shared/
setfacl -m u:jsmith:rx /opt/myapp/logs/
setfacl -m g:developers:rw /opt/shared/
setfacl -d -m g:developers:rw /opt/shared/
getfacl /opt/shared/
setfacl -x u:jsmith /opt/myapp/logs/
setfacl -b /opt/shared/
PAM Configuration
password requisite pam_pwquality.so retry=3 minlen=12 dcredit=-1 ucredit=-1 ocredit=-1 lcredit=-1
minlen = 12
dcredit = -1
ucredit = -1
ocredit = -1
lcredit = -1
maxrepeat = 3
dictcheck = 1
jsmith hard maxlogins 3
@developers hard maxlogins 5
auth required pam_faillock.so preauth silent deny=5 unlock_time=900
auth required pam_faillock.so authfail deny=5 unlock_time=900
faillock --user jsmith
faillock --user jsmith --reset
LDAP / Active Directory Integration
apt install -y sssd realmd adcli sssd-tools libnss-sss libpam-sss
dnf install -y sssd realmd adcli sssd-tools oddjob oddjob-mkhomedir
realm discover corp.example.com
realm join corp.example.com -U admin@CORP.EXAMPLE.COM
realm list
realm permit -g "Linux Admins@corp.example.com"
realm permit -g "Developers@corp.example.com"
realm deny --all
realm permit -g "Linux Admins@corp.example.com"
systemctl restart sssd
id jsmith
getent passwd jsmith
echo '%linux\ admins ALL=(ALL) ALL' > /etc/sudoers.d/ad-admins
Bulk User Management Scripts
Bulk User Creation from CSV
#!/bin/bash
CSV_FILE="${1:?Usage: $0 <users.csv>}"
while IFS=',' read -r username fullname groups shell; do
[[ "$username" == "username" ]] && continue
if id "$username" &>/dev/null; then
echo "SKIP: User $username already exists"
continue
fi
group_list="${groups//;/,}"
useradd -m -s "$shell" -c "$fullname" -G "$group_list" "$username"
temp_pass=$(openssl rand -base64 12)
echo "$username:$temp_pass" | chpasswd
chage -d 0 "$username"
echo "CREATED: $username (groups: $group_list) temp-pass: "
<
Quick Access Audit Commands
awk -F: '$3 >= 1000 && $3 < 65534 { printf "%-20s UID=%-6s Shell=%s\n", $1, $3, $7 }' /etc/passwd
getent group sudo wheel 2>/dev/null
lastlog | awk '$0 ~ /Never logged in/ { print $1 }'
awk -F: '($2 == "" || $2 == "!") { print $1 }' /etc/shadow 2>/dev/null
Troubleshooting
| Symptom | Diagnostic Command | Common Fix |
|---|
| User cannot log in | passwd -S username, faillock --user username | Unlock account, reset password, check shell |
| "not in sudoers" error | sudo -l -U username | Add user to sudo group or create sudoers.d file |
| Group membership not applied | id username, groups username | User must log out and back in for new groups |
| LDAP/AD user not found | id aduser, sssctl user-show aduser | Check SSSD status, clear cache: sss_cache -E |
| Permission denied on file | ls -la file, getfacl file | Fix ownership/permissions, check SELinux context |
| PAM lockout after failed attempts | faillock --user username | faillock --user username --reset |
| Home directory not created | Check /etc/login.defs CREATEHOME | Use useradd -m or enable pam_mkhomedir |
| Password policy not enforced | Check /etc/pam.d/common-password | Install and configure pam_pwquality |
Related Skills
linux-administration -- General Linux server management
ssh-configuration -- SSH key-based authentication for managed users
systemd-services -- Service accounts and systemd user instances
performance-tuning -- Resource limits per user via cgroups and ulimits