DEV Community

Cover image for Mastering User Management and Permission in Linux: The Ultimate Guide
Mohammad Aman
Mohammad Aman

Posted on

3 2 1 2 2

Mastering User Management and Permission in Linux: The Ultimate Guide

User management is the backbone of Linux system administration. Whether you're setting up a personal server, managing a development team, or securing a production environment, knowing how to handle users and groups effectively is essential. This guide is designed to be your one-stop resource for everything related to user management in Linux. We'll cover every command, concept, and best practice in exhaustive detail, with practical examples, tips, and even advanced techniques. Let’s dive into the world of Linux user management and explore every nook and cranny together!


Understanding Users and Groups

In Linux, every process—whether it's a command you run or a service in the background—operates under a user account. Users are the entities that interact with the system, and groups are collections of users that share common permissions. This structure is fundamental to Linux’s security and access control model.


Types of Users

  • Root User: The superuser with UID (User ID) 0, capable of performing any action on the system. Use it sparingly to avoid accidental damage.

  • Regular Users: Everyday accounts for humans, typically with UIDs starting at 1000 (varies by distribution).

  • System Users: Non-human accounts for services (e.g., www-data for web servers), usually with UIDs below 1000.


Groups

Groups simplify permission management. Instead of setting permissions for each user individually, you assign them to a group and manage access collectively.

  • Primary Group: Every user has one primary group, stored in /etc/passwd. New files a user creates inherit this group by default.

  • Supplementary Groups: Additional groups a user belongs to, listed in /etc/group, for extra permissions.

Example: A "developers" group might have access to a project directory, while "admins" have broader system privileges.

Tip: Use id to check a user’s UID, GID (Group ID), and group memberships:

id newuser
# Output: uid=1001(newuser) gid=1001(newuser) groups=1001(newuser),1002(developers)
Enter fullscreen mode Exit fullscreen mode

User Account Management

Creating Users

Linux offers two main tools for creating users: useradd and adduser.

useradd

A low-level command requiring manual specification of options.

sudo useradd -m -s /bin/bash -c "New User" newuser
Enter fullscreen mode Exit fullscreen mode
  • -m: Creates a home directory (e.g., /home/newuser).

  • -s /bin/bash: Sets the default shell.

  • -c "New User": Adds a comment (e.g., full name).

Set a password afterward:

sudo passwd newuser
Enter fullscreen mode Exit fullscreen mode

adduser

A higher-level, interactive tool (common on Debian-based systems).

sudo adduser newuser
Enter fullscreen mode Exit fullscreen mode
  • Prompts for password, full name, and other details.

  • Automatically creates a home directory and sets a shell.

Tip: Use adduser for ease of use, especially for beginners. It’s less error-prone.


Modifying Users

The usermod command adjusts user properties.

Change shell:

sudo usermod -s /bin/zsh newuser
Enter fullscreen mode Exit fullscreen mode

Change home directory:

sudo usermod -d /new/home/newuser -m newuser
Enter fullscreen mode Exit fullscreen mode
  • -m: Moves existing home directory contents.

Add to supplementary groups:

sudo usermod -aG developers,admins newuser
Enter fullscreen mode Exit fullscreen mode
  • -a: Appends to existing groups (omit it, and you’ll overwrite them!).

Tip: Verify changes with id or cat /etc/passwd | grep newuser.


Deleting Users

Use userdel to remove users.

Delete user without touching home directory:

sudo userdel newuser
Enter fullscreen mode Exit fullscreen mode

Delete user and home directory:

sudo userdel -r newuser
Enter fullscreen mode Exit fullscreen mode
  • -r: Removes /home/newuser and mail spool.

Tip: Back up critical data before using -r. There’s no undo!


Managing User Passwords

The passwd command handles passwords.

Set/change password:

sudo passwd newuser
Enter fullscreen mode Exit fullscreen mode

Force password expiration (user must change at next login):

sudo passwd -e newuser
Enter fullscreen mode Exit fullscreen mode

Lock account (disable login):

sudo passwd -l newuser
Enter fullscreen mode Exit fullscreen mode

Unlock account:

sudo passwd -u newuser
Enter fullscreen mode Exit fullscreen mode

Tip: Use passwd -S newuser to check account status (e.g., locked or password set).


Group Management

Creating Groups

Create groups with groupadd.

sudo groupadd developers
Enter fullscreen mode Exit fullscreen mode

Specify a GID:

sudo groupadd -g 2000 developers
Enter fullscreen mode Exit fullscreen mode

Modifying Groups

Use groupmod to tweak group settings.

Rename a group:

sudo groupmod -n devteam developers
Enter fullscreen mode Exit fullscreen mode

Change GID:

sudo groupmod -g 2500 devteam
Enter fullscreen mode Exit fullscreen mode

Deleting Groups

Remove groups with groupdel.

sudo groupdel devteam
Enter fullscreen mode Exit fullscreen mode

Tip: If a group is a user’s primary group, you must reassign or delete those users first.


Adding Users to Groups

Add to a group:

sudo usermod -aG developers newuser
Enter fullscreen mode Exit fullscreen mode

Set as primary group:

sudo usermod -g developers newuser
Enter fullscreen mode Exit fullscreen mode

Tip: Use groups newuser to list a user’s groups.


User and Group Files

These files store user and group data. Avoid direct edits unless absolutely necessary—use commands instead.

/etc/passwd

Holds user account details.

Format: username:password:UID:GID:comment:home_dir:shell

Example:

newuser:x:1001:1001:New User:/home/newuser:/bin/bash
Enter fullscreen mode Exit fullscreen mode
  • x: Password is in /etc/shadow.

/etc/shadow

Stores encrypted passwords and account aging info.

Format: username:encrypted_password:last_change:min:max:warn:inactive:expire

Example:

newuser:$6$abc...:18900:0:99999:7:::
Enter fullscreen mode Exit fullscreen mode
  • 18900: Days since Jan 1, 1970, of last password change.

/etc/group

Lists groups and their members.

Format: groupname:password:GID:member_list

Example:

developers:x:1002:newuser,anotheruser
Enter fullscreen mode Exit fullscreen mode

/etc/gshadow

Secure group info (rarely used manually).

Tip: Use getent passwd newuser or getent group developers to safely view entries.


Permissions and Ownership

File Permissions

Permissions define who can read (r), write (w), or execute (x) a file, split into owner, group, and others.

  • Symbolic: rwxr-xr-- (owner: rwx, group: r-x, others: r--)

  • Octal: 754 (7=rwx, 5=r-x, 4=r--)

Check with:

ls -l file.txt
# Output: -rwxr-xr-- 1 newuser developers 0 Oct 10 12:00 file.txt
Enter fullscreen mode Exit fullscreen mode

Changing Permissions with chmod

Set permissions:

chmod 755 script.sh
Enter fullscreen mode Exit fullscreen mode

Add execute for group:

chmod g+x script.sh
Enter fullscreen mode Exit fullscreen mode

Remove write for others:

chmod o-w file.txt
Enter fullscreen mode Exit fullscreen mode

Recursive change:

chmod -R 750 /project/dir
Enter fullscreen mode Exit fullscreen mode

Changing Ownership with chown and chgrp

Change owner and group:

sudo chown newuser:developers file.txt
Enter fullscreen mode Exit fullscreen mode

Change group only:

sudo chgrp developers file.txt
Enter fullscreen mode Exit fullscreen mode

Recursive ownership:

sudo chown -R newuser:developers /project/dir
Enter fullscreen mode Exit fullscreen mode

Tip: Use stat file.txt for detailed ownership/permission info.


Sudo and Root Privileges

Understanding sudo

sudo lets users run commands as root or another user, controlled by /etc/sudoers.

Example:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Configuring the sudoers File

Edit with visudo for safety:

sudo visudo
Enter fullscreen mode Exit fullscreen mode

Grant full sudo to a user:

newuser ALL=(ALL:ALL) ALL
Enter fullscreen mode Exit fullscreen mode

Limit to specific commands:

newuser ALL=(ALL) /bin/ls, /bin/cat
Enter fullscreen mode Exit fullscreen mode

Group-based sudo (e.g., sudo group):

%sudo ALL=(ALL:ALL) ALL
Enter fullscreen mode Exit fullscreen mode

Best Practices for sudo

  • Minimize sudo users.

  • Use NOPASSWD sparingly:

newuser ALL=(ALL) NOPASSWD: /usr/bin/apt
Enter fullscreen mode Exit fullscreen mode
  • Log sudo usage (/var/log/auth.log).

Tip: Test sudo rules with sudo -l -U newuser.


Advanced User Management

User Templates and Skeletons

The /etc/skel directory provides default files for new users’ home directories.

Customize:

sudo cp mybashrc /etc/skel/.bashrc
Enter fullscreen mode Exit fullscreen mode

Managing User Environments

  • .bashrc: For interactive shells (aliases, PATH).
echo "alias ll='ls -la'" >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode
  • .profile: For login shells (environment variables).
echo "export PATH=$PATH:/my/bin" >> ~/.profile
Enter fullscreen mode Exit fullscreen mode

Limiting User Resources with ulimit

Limit processes:

ulimit -u 100
Enter fullscreen mode Exit fullscreen mode

Limit file size:

ulimit -f 10240  # 10MB
Enter fullscreen mode Exit fullscreen mode

Permanent limits (in /etc/security/limits.conf):

newuser hard nproc 100
newuser hard fsize 10240000
Enter fullscreen mode Exit fullscreen mode

Tip: Check limits with ulimit -a.


Security Considerations

Password Policies

Set via PAM (e.g., /etc/pam.d/common-password):

password requisite pam_pwquality.so retry=3 minlen=10 difok=3
Enter fullscreen mode Exit fullscreen mode

Enforce aging:

sudo chage -M 90 -m 7 -W 14 newuser
Enter fullscreen mode Exit fullscreen mode
  • -M 90: Max 90 days.

  • -m 7: Min 7 days between changes.

  • -W 14: Warn 14 days before expiration.

Locking and Unlocking User Accounts

sudo passwd -l newuser
sudo passwd -u newuser
Enter fullscreen mode Exit fullscreen mode

Monitoring User Activity

Last logins:

last
Enter fullscreen mode Exit fullscreen mode

Lastlog per user:

lastlog -u newuser
Enter fullscreen mode Exit fullscreen mode

Check auth logs:

sudo cat /var/log/auth.log | grep newuser
Enter fullscreen mode Exit fullscreen mode

Tip: Set up auditd for detailed user auditing.


Automation and Scripting

Scripting User Management Tasks

Batch create users:

#!/bin/bash
while read -r user; do
  sudo useradd -m -s /bin/bash "$user"
  echo "$user:password123" | sudo chpasswd
done < users.txt
Enter fullscreen mode Exit fullscreen mode

Delete users from list:

while read -r user; do
  sudo userdel -r "$user"
done < users.txt
Enter fullscreen mode Exit fullscreen mode

Using awk and sed for Batch Operations

Change all shells to Bash:

sudo sed -i 's|/bin/sh|/bin/bash|g' /etc/passwd
Enter fullscreen mode Exit fullscreen mode

Extract users with UID > 1000:

awk -F: '$3 > 1000 {print $1}' /etc/passwd
Enter fullscreen mode Exit fullscreen mode

Tip: Test scripts on a VM first!


Troubleshooting

Common Issues and Solutions

  • User can’t log in:

    • Check lock status: passwd -S newuser
    • Verify shell: grep newuser /etc/passwd
    • Ensure home directory exists: ls -ld /home/newuser
  • Permission denied:

    • Check permissions: ls -l
    • Verify groups: groups newuser

Checking Logs for User-Related Activities

  • Auth logs:
sudo less /var/log/auth.log
Enter fullscreen mode Exit fullscreen mode
  • System logs:
sudo less /var/log/syslog
Enter fullscreen mode Exit fullscreen mode

Tip: Use journalctl -u sshd for SSH login issues.


Conclusion

User management and permission in Linux is a vast and powerful domain. From creating users to securing their accounts, this guide has covered it all—commands, examples, tips, and beyond. Whether you’re a beginner setting up your first server or a seasoned admin automating a fleet of machines, these skills will serve you well.


Written by Mohammad Aman + AI

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Image of Stellar post

Discover what it takes to launch a Web3 startup

Bringing your Web3 idea to life and launching a startup can be a difficult, complicated process. Check out the Stellar Dev Diaries to learn from a team that is makiung it happen, in real time!

Learn more

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️