DEV Community

Cover image for πŸ‘€ Linux User & Group Management
DevOps Man
DevOps Man

Posted on

πŸ‘€ Linux User & Group Management

Want to really understand how Linux handles users and groups? This hands-on guide not only gives you commands β€” it breaks them down flag by flag, so you know exactly what you're doing.

Grab your terminal and follow along. Ready? Let’s get started. πŸš€

πŸ“ Important Files β€” Know What Powers User Management

/etc/passwd   # User info: username:x:UID:GID:comment:home:shell
/etc/shadow   # Has (hashed) passwords
/etc/group    # Lists all groups
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Try:

cat /etc/passwd | head -n 3
sudo cat /etc/shadow | head -n 3
cat /etc/group | head -n 3
Enter fullscreen mode Exit fullscreen mode

πŸ› οΈ Create a User with useradd

useradd [OPTIONS] username
Enter fullscreen mode Exit fullscreen mode

πŸ”‘ Common Flags:

  • -m β†’ Create a home directory (e.g., /home/username)

  • -d /custom/path β†’ Set a custom home directory

  • -s /bin/bash β†’ Set user’s login shell

  • -c "comment" β†’ Add a comment (e.g., full name or role)

  • -G group1,group2 β†’ Add to secondary groups (must already exist)

  • -g group β†’ Set primary group (must already exist)

πŸ“Œ Practical Example:

sudo useradd -m -d /home/john -c "C++ Developer" -s /bin/bash -G sudo,adm john
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • -m: Create /home/john

  • -d /home/john: Use this as the home directory

  • -c "C++ Developer": Store this comment

  • -s /bin/bash: Set Bash as login shell

  • -G sudo,adm: Add to secondary groups

➑️ Now set a password:

sudo passwd john
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Modify a User with usermod

usermod [OPTIONS] username
Enter fullscreen mode Exit fullscreen mode

Useful Flags:

  • -aG group1,group2 β†’ Append user to new secondary groups

  • -s /new/shell β†’ Change default shell

  • -d /new/home β†’ Change home dir (-m to move files)

  • -c β†’ Change comment

πŸ”§ Example:

sudo usermod -aG docker,developers john
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • -a: Append (don’t remove existing groups!)

  • -G: Add to docker and developers groups


❌ Delete a User with userdel

sudo userdel [OPTIONS] username
Enter fullscreen mode Exit fullscreen mode

Flags:

  • -r β†’ Remove user’s home directory and mail spool Example:
sudo userdel -r john
Enter fullscreen mode Exit fullscreen mode

Result: Deletes john + /home/john


πŸ‘₯ Group Management with groupadd, groupdel

βž• Add a Group:

sudo groupadd devops
Enter fullscreen mode Exit fullscreen mode

❌ Delete a Group:

sudo groupdel devops
Enter fullscreen mode Exit fullscreen mode

πŸ” View All Groups:

cat /etc/group
Enter fullscreen mode Exit fullscreen mode

πŸ” Check Group Membership

groups           # For current user
groups username  # For another user
id username      # UID, GID, group list
Enter fullscreen mode Exit fullscreen mode

πŸ§‘β€πŸ’Ό Make a User Admin (Sudo Access)

On Ubuntu:
sudo usermod -aG sudo john

On CentOS:
sudo usermod -aG wheel john


πŸ•΅οΈ Monitor Logged-In Users

who -H       # Show who is logged in
w            # Who is logged in + what they’re doing
uptime       # Show load, users, system uptime
id           # Show UID, GID, and groups
whoami       # Show your effective user ID
Enter fullscreen mode Exit fullscreen mode

πŸ“œ Login History

last             # Recent logins
last -u username # Logins by a specific user
Enter fullscreen mode Exit fullscreen mode

✍️ Final Words

Linux user & group management is must-know stuff for sysadmins, DevOps engineers, and anyone using Linux daily.

Top comments (0)