Managing users and groups in Linux with the `useradd`, `usermod`, `passwd` and `userdel` commands
- Chris
- Apr 10
- 4 min read

Creating users in a Linux system is a fundamental task required by system administrators and anyone managing a Linux server or a desktop workstation.
The `useradd` command is the main tool for creating user accounts on Linux systems. In this blog post, we will look at how to effectively use the `useradd` and other user management commands, the various options available, and how to assign a user to a specific group and shell.
Understanding the Useradd Command
The `useradd` command is a low-level utility that creates a new user or updates default user information. In its simplest form, it creates an account with minimal configurations.
useradd [options] username
Here, `username` is the name of the account you want to create.
Basic Options of Useradd
-m (Create Home Directory)
One of the most commonly used options is `-m`, which creates a home directory for the new user. This home directory is essential as it provides space for the user’s personal files.
useradd -m username
This command creates a new user called `username` and a corresponding home directory at `/home/username`.
-s (Specify the Shell)
By default, users may be assigned a shell like `/bin/sh`. You can specify a different shell using the `-s` option. This is helpful if you want a user to have a specific environment.
useradd -m -s /bin/bash username
For example, this command creates a user named `username` with `/bin/bash` as the default shell, which is the preferred shell for many users due to its ease of use and functionality.
-G (Add User to Groups)
Linux allows users to belong to multiple groups, which control permissions for accessing different resources. You can add a user to one or more groups when creating the account using the `-G` option.
useradd -m -s /bin/bash -G developers username
In this case, the user `username` is created, assigned `/bin/bash` as the shell, and added to the `developers` group, giving them access to all resources that the group governs.
-c (Comment Field)
Adding a comment enhances user management by providing additional context regarding the user's role. The `-c` option allows you to include a short description.
useradd -m -s /bin/bash -c "John Doe's account" username
This particular command assigns a comment to the user that clarifies the account’s purpose. For instance, in environments with many users, this can help administrators quickly identify roles.
-e (Set Expiration Date)
For temporary accounts, you may want to set an expiration date using the `-e` option. This function will disable the account after a specified date.
useradd -m -s /bin/bash -e 2024-12-31 username
In this case, the user `username` will be disabled after December 31, 2024, making it easy to manage seasonal employees or short-term projects.
Comprehensive Example
Let’s summarize the options we’ve discussed with a comprehensive command. This command creates a user named `jdoe`, creates a home directory, assigns `/bin/bash` as the shell, includes a comment, and adds the user to the `developers` group.
useradd -m -s /bin/bash -c "John Doe's account" -G developers jdoe
This effectively sets up a user with personalized attributes and permissions specifically designed to make their tasks easier.
Most common usage: user with sudo privileges
In a standard workstation the user should have access to the 'wheel' or 'sudo' group(s) in order to get sudo privileges.
The command to add a new user, add it to the 'wheel' group with 'bash' as the default shell is the following:
useradd -mG wheel -s /bin/bash username
replace 'username' with your chosen username. Remember that in linux everything is case-sensitive so "username" is not equal to "Username"
Viewing User Details
Once you have created a user, you might want to verify the details. You can do this by checking the `/etc/passwd` file.
cat /etc/passwd | grep jdoe
This command retrieves the entry for the user `jdoe`, showing the user ID, group ID, home directory, and default shell. With over 15 million Linux users globally, keeping track of user information is crucial for effective system management.
Creating or modifying a user password with `passwd`
After creating a user, it is required to specify a login password.
This is what the 'passwd' command does.
The usage is:
passwd username
where username is your actual username.
You will be prompted to type a password for the specified username and repeat it for safety.
Pay attention to the final output.
If no username is specified the command will try to modify the password of the 'root' user (if there is an active root account in the system).
The same syntax can be used to modify the password of an existing user account.
In this case you will be asked to type the old password and specify the new one.
Modifying User Accounts with `usermod`
If you need to adjust user information, you can use the `usermod` command. This allows you to make various changes without deleting and recreating the user.
For instance, to change the shell of `jdoe` to `/bin/zsh`, the command is:
usermod -s /bin/zsh jdoe
This command helps maintain user preferences when a user prefers a different shell environment.
Deleting Users with `userdel`
If you need to remove a user, the `userdel` command is used. You can remove a user while also deleting their home directory by including the `-r` option.
userdel -r jdoe
This command deletes the user `jdoe` and their home directory, helping keep the system clean and free from unnecessary files.
Final Thoughts
Understanding and effectively using the `useradd`, `usermod`, `userdel` and `passwd` commands is essential for any Linux administrator looking to maintain an organized system.
It is also useful for the regular user to manage their account(s) without any GUI.
Comments