For managing servers, it is not recommended to use root user account due to security reasons.
So you can follow this quick guide to add a new user in your ubuntu server and grant sudo access so that you can run any command like authorized sudo user.
Step 1: Switch to root user
sudo su
Step 2: Create a new user on Ubuntu
Create a new user using following command and provide required information or you can choose press ENTER button to use default information for Full Name, Room Number, Work Phone, Home Phone & Other etc.
(Example: let’s assume we want to create a new user named “adarsh”. You can choose whatever username you prefer)
adduser adarsh
Now in order to switch to newly created user, you can enter following command.
In my case new user created is adarsh, so I will use it. You need to modify this according to the new user that you created for yourself.
su adarsh
Now if you run any command with sudo for example like following command, you get an error like – “User is not in sudoers file.”
sudo apt-get update
That means we need to give sudo access to this newly created user to run any command with sudo privileges.
Step 3: Grant sudo access to the new user account
Before granting sudo access to new user, we need to switch to root user first. So enter following command
sudo su
Now run in order to edit the sudoers file, run following command
sudo visudo
Sudoer file will be opened. Now go to the section in this file that says – “Allow members of group sudo to execute any command”.
Here after the line %sudo ALL=(ALL:ALL) ALL
Write the same line just after it, and then replace the “sudo” word with your new user.
In my case, it is adarsh. So my new file looks something like below.
Now save and exit from this file by pressing CTRL+X and confirming onscreen instructions.
That’s it. Now the newly created user has been added in the sudoers file.
To confirm it, switch to the new user account using following command
sudo adarsh
They try again this update command like following
sudo apt-get update
Now you’ll see no error shows like before and by providing the password of new user you can run any command with sudo access.
Thanks for reading this article… have a good day!