Managing users and groups is an essential skill for any Linux system administrator. This guide provides a clear overview of the basic commands and files involved in creating, modifying, and deleting users and groups on a Linux system.
Creating Users
To add a new user to the system, use the following command:
useradd username
Example:
useradd sharecodepoint
This command creates a new user called sharecodepoint.
Note: Only the root user can create new users. To switch to root, type:
su -
Enter the root password (it won’t be visible as you type — don’t worry!).
User Account Information Files
Linux stores user-related information in these key files:
cat /etc/passwd
– Contains one line for each user account.cat /etc/group
– Defines the groups on the system.
Set Password for User
To set a password for a user:
passwd username
Example:
passwd sharecodepoint
You’ll be prompted to enter the new password.
Delete a User
To remove a user from the system:
userdel username
Example:
userdel sharecodepoint
To view the list of existing users:
cat /etc/passwd
Understanding the /etc/passwd File
Each entry in the /etc/passwd
file has the format:
user:password:UID:GID:comment:home:shell
- Username: Login name (1–32 characters)
- Password: Encrypted, stored in
/etc/shadow
- UID (User ID): Unique numeric ID for the user (0 for root, 1-99 for system, 100-999 for other system accounts)
- GID (Group ID): User’s primary group ID from
/etc/group
- Comment/User Info: Optional; full name or additional info
- Home Directory: Path to user’s home directory
- Shell: Default shell (e.g.,
/bin/bash
)
Creating a Group
To create a new group:
groupadd groupname
Example:
groupadd cappractice
To view existing groups:
cat /etc/group
Adding Users to Groups
You can use either useradd
or usermod
to add a user to a group.
There are two types of groups:
- Primary Group: Default group assigned to a user.
- Secondary/Supplementary Group: Additional groups a user belongs to.
Primary Group
The primary group is defined by the 4th field in /etc/passwd
. Files and directories created by the user will belong to this group.
Secondary Groups
Users can be part of additional groups. These are managed via /etc/group
.
To add a user to a secondary group:
useradd -G groupname username
Example:
useradd -G cappractice sharecodepoint
To add a user to multiple groups:
useradd -G admins,ftp,www,developers jerry
Use uppercase
-G
to specify secondary groups.
Add User to Primary Group
To assign a user to a primary group:
useradd -g groupname username
Example:
useradd -g developers tony
id tony
Use lowercase
-g
to specify the initial login (primary) group.
Change a User’s Primary Group
To change a user’s primary group:
usermod -g groupname username
Add an Existing User to a Group
To add an existing user to a secondary group:
usermod -a -G groupname username
Example:
usermod -a -G ftp tony
To change the primary group for the user:
usermod -g www tony
Use
-a
only with-G
to avoid overwriting existing group memberships.
Delete a Group
To delete a group:
groupdel groupname
Example:
groupdel cappractice
This guide gives you a solid foundation in managing Linux users and groups. Whether you're setting up new accounts or configuring group permissions, mastering these commands will make your system management smoother and more secure.