If you are using Linux, you might have come across situations where you need to perform administrative tasks that require root access. In such cases, you can either log in as the root user or use the sudo
command to execute the command with elevated privileges. However, it is not recommended to use the root user account for everyday tasks, as it can be a security risk. Instead, you can create a new user account with sudo
privileges. In this article, we will discuss how to add a sudo user in Linux.
Prerequisites
Before getting started, you need to have access to a Linux system with root privileges. You should also have a basic understanding of the Linux command line and the sudo
command.
Steps to Add a Sudo User in Linux
The process of adding a sudo user in Linux involves the following steps:
- Log in as the root user or switch to the root user account using the
su
command.
su -
- Create a new user account using the
adduser
command.
adduser username
Replace username
with the desired username for the new user account.
- Set a password for the new user account using the
passwd
command.
passwd username
Replace username
with the username of the new user account.
- Add the new user account to the
sudo
group using theusermod
command.
usermod -aG sudo username
Replace username
with the username of the new user account.
- Verify that the new user account has
sudo
privileges by running a command withsudo
.
sudo ls /root
This command should list the contents of the root directory. If the new user account has sudo
privileges, the command should execute successfully.
Explanation
Let’s go through each step in detail to understand what is happening.
Step 1: Log in as the root user or switch to the root user account using the su
command.
The root user has full administrative privileges on a Linux system. However, it is not recommended to use the root user account for everyday tasks, as it can be a security risk. Instead, you can switch to the root user account temporarily using the su
command.
Step 2: Create a new user account using the adduser
command.
The adduser
command is used to create a new user account in Linux. When you run this command, it prompts you to enter some information about the new user account, such as the username, password, and full name. Once you provide this information, the adduser
command creates a new user account with a home directory and some default settings.
Step 3: Set a password for the new user account using the passwd
command.
The passwd
command is used to set or change the password for a user account in Linux. When you run this command, it prompts you to enter the new password twice for confirmation. Once you provide this information, the passwd
command sets the new password for the user account.
Step 4: Add the new user account to the sudo
group using the usermod
command.
The sudo
command allows a user to execute a command with elevated privileges. To use the sudo
command, the user must be a member of the sudo
group. In this step, we add the new user account to the sudo
group using the usermod
command.
Step 5: Verify that the new user account has sudo
privileges by running a command with sudo
.
In this step, we verify that the new user account has sudo
privileges by running a command with sudo
. If the new user account has sudo
privileges, the command should execute successfully.
Conclusion
Adding a sudo user in Linux is a straightforward process that involves creating a new user account, setting a password, and adding the user account to the sudo
group. Once you have added a sudo user, you can use the sudo
command to execute commands with elevated privileges without logging in as the root user. This is a safer and more secure way of performing administrative tasks in Linux.