There are multiple ways to create a user in Linux. The most common way is to use the useradd command. This command will add a new user to the system and create their home directory. In this article, let’s explore the useradd command in detail and understand the process of user creation in Linux.
Also read: Reading and Writing JSON to a File in Python
Create a user in Linux with useradd
To use the useradd command, you must be logged in as root or have sudo privileges. Then, you can run the following command to add a new user:
useradd [username]
Substitute [username] with the name of the user you want to add. For example, this command would add a new user named r2dee2:
useradd r2dee2
This command will create r2dee2’s home directory at /home/r2dee2/. It will also assign the user to the default group for new users (usually Users).
You can view all of the available options for this command by running man useradd.
Add a New Group Using The useradd command
You can add groups just like you add users but, instead of the useradd command, we use groupadd. For example, to create a new group called managers, you would type:
sudo groupadd managers
Creating Users with a Custom Home Directory
By default, the useradd command will create the user’s home directory at /home/[username].
If you want to specify a different location for the user’s home directory, you can use the -d option. For example, this command will create a new user named John with his home directory at /tmp/tempuser:
useradd -d /tmp/tempuser temp_user_two
Keep in mind that you must create the destination directory before running this command. Otherwise, the command will fail.
Creating a User with Specific User ID
sudo useradd -u 1001 username
The -u option allows you to specify the user ID (UID) for the new user. This can be useful if you have a set organizational structure and want to standardize how the UIDs are set for each user. Here we can replace 1001 with any permissible UID we like. The username can be changed as per the requirement as well.
Now, let’s check the uid of the specific user, the username will be replaced with the username that exists in the system.
id -u username
Creating A Group With Specific Group ID
Just like every user has their own unique numeric identifier (UID), every group also has its own numeric identifier called GID or Primary Group Identifier.
By default, when creating groups without explicitly assigning any particular GID, most Linux distributions assign them sequential GIDs starting from 1000.
Let’s move on ahead and see how we can add additional groups for our newly created account along with specifying custom GIDs for each group!
Every group also has a unique numeric identifier called a GID. By default, new groups are assigned the next available GID. However, you can use the -g option to specify a custom GID for a new group.
For example, this command will create a new group named developers with GID 0935:
groupadd -g 0935 extinctspecies
This is useful if you are migrating users from another system and need to preserve their existing GIDs. It can also be helpful when setting up certain applications that require specific GIDs.
Creating A User And Assign Multiple Groups
We’ve now learned how to add groups for our newly created account along with custom values. But what if we wanted to go one step further by actually assigning multiple groups to our users?
Luckily! Linux provides us with the usermod command which makes managing groups a whole lot easier.
You can assign multiple groups to a user by using the usermod command with the -a (append) option and -G (secondary group) option followed by a comma-separated list of desired groups. For example, this command will add the dinosaur user to both the sudo and extinctspecies groups:
usermod -aG sudo,extinctspecies dinosaur
We can check what all groups a user belong to by using:
groups <username>
If you want to remove a user from one or more groups, you can use the gpasswd command with the -d (delete) option followed by the username and group name. For example, this command would remove dinosaur from the sudo group:
gpasswd --delete dinosaur sudo
Adding/removing users from additional groups might prove quite useful in multi-user environments where common tasks can be easily automated by making use of cron jobs hence every Linux power user can definitely benefit from knowing this particular technique.
Now that we have removed dinosaur from the sudo group let’s check what all group dinosaur is associated with,
Creating a User with Specific Login Shell
Every user on a Linux system has their own personal login shell. This is the shell that is started when the user logs into the system. By default, new users are assigned the /bin/bash shell.
However, you can use the -s option to specify a different login shell for the user. For example, this command would create a new user named trex with the /bin/zsh shell:
useradd -s /bin/zsh trex
Now let’s try implementing this,
We need to check what all shells are installed in the system. You can view all of the available shells on your system by looking in the /etc/shells file.
Let’s go with /bin/bash for our user trex
Creating A User With Custom Comments
Up until now, we’ve pretty much focused on managing regular accounts. What if we wanted to add some sort of description or “comment” associated with each individual account?
Luckily most popular Linux distributions including Ubuntu make it possible to do so using the following command:
useradd -c 'crocodile' sarcosuchus
The -c option allows you to specify a comment for the user. This is useful if you want to add additional information about the user such as their full name or job title.
Creating a User with an Expiry Date in Linux
It is often desirable to create a user that has a limited lifespan. This might be the case when you want to give someone temporary access to your system. To do this, you can use the -e option with the useradd command. The format for this option is:
-e YYYY-MM-DD
For example, the following command would create a user called tempuser that would expire on May 31st, 2019:
sudo useradd -e 2022-09-29 velociraptor
Creating a System User in Linux
System users are those that are used by system processes and services rather than by human beings. It is therefore important that they have very restricted permissions. To create a system user, you use the –system option with useradd like this:
sudo useradd --system dinosaur_moderator
Adding Password to User in Linux
If we want our new users to actually be able to log into our system, we need to give them passwords. We can do this using the passwd command like so:
sudo passwd newusername
Once again, you will be prompted for your password and then asked twice to enter a password for your new user. Be sure not to choose something too easy!
We have now added our first regular non-root user account but there’s still one more thing we should do before logging out of our root account – add our new user account to the sudo group so that it can perform administrative tasks.
We can do this with the usermod command like so:
sudo usermod -aG sudo dinosaur_moderator
Delete a User
Deleting a user account is just as easy as creating one. The command to use is userdel and, again, we need to be logged in as root or have superuser privileges. The basic syntax for deleting a user account is:
sudo userdel [username]
Summary
In this article, we have looked at how to add and delete user accounts on a Linux system as well as how to create groups. We have also seen how to change passwords and modify group memberships.