DEV Community

Cover image for 👑Mastering User and Group Management in Linux
SAHIL
SAHIL

Posted on

👑Mastering User and Group Management in Linux

👉🏻Linux provides powerful command-line tools for managing users and groups. In this post, you'll learn how to use essential commands like useradd, usermod, userdel, and passwd, as well as how to manage groups and understand the important files involved in user and group management.


👤 User Management Commands

useradd

Used to add a new user.

# Create a new user
sudo useradd username

# Create user with home directory and default shell
sudo useradd -m -s /bin/bash username
Enter fullscreen mode Exit fullscreen mode

Options:
-d _path_: specify custom path of home directory.
-u _uid_: set custom UID.
-g _group_ : set primary group for user
-G _group_ : add user to supplementary groups.
-c "comment" : add comment to GECOS field.

-e "YYYY-MM-DD" : set account expiration date.

🛠️ usermod

Modify an existing user.

Syntax:

usermod [OPTIONS] user
Enter fullscreen mode Exit fullscreen mode
# Change a user's login name
sudo usermod -l newname oldname

# Change the user's home directory
sudo usermod -d /new/home username

# Add user to a group
sudo usermod -aG groupname username
Enter fullscreen mode Exit fullscreen mode

Options:
-l _newusername_: change login name
-d _path_ : change user's home directory.
-m: move content of current directory to new directory. Used with -d option while changing directory.
-s _path_: change user's default shell.
-u _new_uid : change uid of user.
-g _new_group_ : change primary group.
-G _new_group_ : add or replace supplementary group.


userdel

Delete a user.

# Delete a user
sudo userdel username

# Delete a user and their home directory
sudo userdel -r username
Enter fullscreen mode Exit fullscreen mode

🔑 passwd

Set or change user passwords.

# Set password for a user
sudo passwd username

# Lock a user account
sudo passwd -l username

# Unlock a user account
sudo passwd -u username
Enter fullscreen mode Exit fullscreen mode

📂 Important Files in User and Group Management

/etc/passwd

Contains basic user account information.

👉🏻Format:
username:x:UID:GID:comment:home_directory:shell
Enter fullscreen mode Exit fullscreen mode

/etc/shadow

Contains encrypted user passwords and password-related info.

/etc/group

Stores group information.

👉🏻Format:
groupname:x:GID:members
Enter fullscreen mode Exit fullscreen mode

/etc/gshadow

Secure group account information including group passwords.


🧠 Summary

Mastering user and group management is essential for any Linux administrator. Make sure you understand how these commands and files work together to control access, enhance security, and structure your system efficiently.

Happy Learning! 🚀

🔗 Follow me for more Linux tips

Top comments (0)