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)