The Linux system allows multiple users to log in and access it. This means that more than one person can interact with the system at the same time. There are several reasons why one Linux system can be accessed by multiple users. So, as a system administrator, you want each user to have their own account, and your primary job is to add, remove, or manage how users interact on a single Linux system.
In this guide, we will show you how to add users in Linux and the best way to assign them the appropriate user groups.
RECOMMENDED READING: How to download and install Ubuntu Linux on a Laptop
Who is a Linux user?
A Linux user is an account assigned with privileges to access and use the Linux system. There are various types of Linux users;
Root User
The user with the highest privileges or administrative privileges is root. A root user is also known as a superuser and can add, delete, modify, and remove other users from accessing the system. In most cases, the root user account is only accessed by system administrators. This means that you shouldn’t allow everybody to have root access to your system as they can easily delete everything or compromise the entire system.
A root user can be identified by how you run commands on the system. In other words, if you’re a root user, you don’t need to prefix your Linux commands with sudo as it’s not necessary when running any command on the Linux terminal.
The root user account is created when the Linux system is being set up. assuming you’re installing Linux on your computer or server, you will have to set up your root credentials before you proceed with using your system.
Regular User
A regular user is the normal user account that can be assigned credentials to access the Linux system. A regular user is created by a root user or Superuser and can limit which files or directories to access. A regular Linux user doesn’t have administrative access to the system by default. However, where necessary, the root user can choose to elevate the regular user’s privileged access to the system by adding them to what is known as the Sudoer group
RECOMMENDED READING: Step-by-Step Guide to Installing FTP on Ubuntu Server
Using the useradd Command
We can create a new Linux user by using the useradd
command. Please see the syntax below;
useradd USERNAME
Assuming you wanted to create a new user named ugacomp, the following command would be used;
useradd ugacomp
Using the above command, a user named ugacomp will be created. Now, you can replace ugacomp with your desired name of your choice.
RECOMMENDED READING: Step-by-Step Guide to Installing FTP on Ubuntu Server
Add a Password to the Linux user
After creating the new user, you will need to add a password to it. This is necessary to secure the new user. The following syntax is used to add a password to a user;
passwd username
In this example, our username is ugacomp but you can replace it with yours as seen below;
passwd ugacomp
After typing the above command, you will need to hit Enter and then you will have to type in your password.
It’s important to note that while typing your password, it won’t be visible on the terminal. So, you need to first note your password somewhere to make sure you’re typing it in accurately as you want it.
Creating a Home Directory for the User
In some cases, the user Home directory may not be created for the New user by default. In Linux, a Home directory is the default working directory path for any Linux user when they log in, and it is where the user’s personal files and settings are stored.
When creating a new user, we can use the –m parameter to create the user’s default home directory as /home/username: Below is the command you’re going to use.
sudo useradd -m username
In this example, my username is ugacomp and so the following command will be used;
sudo useradd -m ugacomp
Creating a User with a Specific Home Directory
Home Directory /home
is created by default when you run the useradd
command. But if you need to assign a user to a custom home directory location, then we can use both the -m
& -d
parameters. For example, the following command will create a default directory as /opt/ugacomp for user ugacomp (replace ugacomp with yours);
sudo useradd -m -d /opt/ugacomp ugacomp
There are a few reasons why you might need to create a custom home directory path for a Linux user other than home
and these include the following;
- Organize your users’ home directories in a specific way.
For example, you might want to create separate home directories for system users, application users, and regular users.
- To isolate a user’s home directory from other users’ home directories.
This can be useful for security purposes, especially if you are running a multi-user system.
- To provide more disk space to a user.
For example, you might want to create a custom home directory for a user who is working on a large project.
- To store a user’s home directory on a separate filesystem.
This can be useful for performance reasons, or to protect the user’s data from corruption.
RECOMMENDED READING: Step-by-Step Guide to Installing FTP on Ubuntu Server
Creating a User with a Specific Group
Linux groups are bundles or collections of users that share defined access privileges to the system. If you’re a system administrator, especially working for a large company, you will need to add users to Linux groups with varying access privileges to the system. The user privileges usually include writing, reading, or executing permissions.
RECOMMENDED READING: How to install and configure UFW firewall on Ubuntu Linux
You will need to first create a User group using the following command syntax
groupadd -g group-ID group-name
The groupadd
is the main command to create a User group in Linux which is followed with the -g
parameter and the group-ID
parameter is the group ID which is a numerical identifier of the created group, and the group-name
defines how you want to call the User group you want to create.
When you create a new user, by default the useradd
command will also create a group with the same name as the username, and the same group ID will be the same as the user ID
But if you need to create a custom user group named server in which you can assign the users you want, the following command can be used.
sudo groupadd -g 1009 server
You will also notice that we have specified the Group-ID value as 1009 for easy identification. You can also choose not to define the group-ID and just run the command as follows;
sudo groupadd server
Now that our group is created, we can create a new user, ugacomp, and add it to the group named server using the following command;
sudo useradd -g server ugacomp
In the above command, the -g
parameter specifies server
as the group we want the user, ugacomp to be assigned to.
Creating a User and Assign Multiple Groups
A simple rule is that a Linux user can belong to one Primary User group and one or more Secondary User groups.
A primary user group in Linux is the group that is automatically assigned to a user when they are created. Each user must belong to a primary group. The primary group is used by default when creating new files (or directories), modifying files, or executing commands.
RECOMMENDED READING: How to Install WordPress on a VPS Server using Cloudron?
A secondary user group in Linux is a group that a user can belong to in addition to their primary group. Users can belong to up to 15 secondary groups. Secondary groups are used to grant users access to shared resources, such as files, directories, and devices.
Please note that the -g
parameter with a small g defines the primary User group and the -G
parameter with a big G is used to define Secondary user Groups when creating a new user.
The following command will create a new user named ugacomp with primary group server
and secondary groups wheel
and docker
.
sudo useradd -g server -G wheel,docker ugacomp
Please make sure you’ve also created the secondary groups before adding the user to them.
RECOMMENDED READING: How can I create FTP User Directory in Ubuntu?
Creating a User with an Expiry Date
You may want to create a temporary user with an expiry date. To achieve this, we can use -e parameter when using the useradd command.
It’s important to note that the only acceptable date format is YYYY-MM-DD
. For example, to create a new user account named ugacomp with an expiry time set to october 22 2023, you will need to run the following command;
sudo useradd -e 2023-10-22 ugcomp
To verify the user’s expiry date, you can run the following command;
sudo chage -l username
And the output will be seen as below;
Last password change : Dec 11, 2018
Password expires : never
Password inactive : never
Account expires : Jan 22, 2019
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7