You might wonder why it is important.
You might say, "I can use my system only with default root account".
Using the system with default root account is not good, and it will end up in getting security problems.
So, it is always good to have a unprivileged user to carry out your task.
You can ask me, "How I am supposed to gain administrator privileges if I need."
No Problem, Still you can get the administrator privileges using a mechanism called sudo.
Here in this tutorials, I will show you how to create users, providing sudo privileges and removing the user.
How to Add User in Ubuntu
First, Let us see How to add a User.
If you are logged in as the root user, you can create new user all the time by typing the following command.
# adduser newuser
if you are a not root user** and you have sudo privileges, you can also create the user by typing the following command.
$ sudo adduser newuser
The prompt will ask you enter the following
Create the new password for the user and confirm it.
You will be asked to enter extra information about the user. It is not compulsion, and you can skip it by pressing Enter.
At last, you have to confirm that the information you provided is correct and Press Y.
The new user account is ready to use. Just log in with the password.
If you want your new user to have administrator privileges, continue to the second step.
How to Grant Sudo Privileges to User.
If your new user needs the rights to execute commands with administrator privileges, you will have to give the new user access to sudo.
Here we can do that process in two ways.
- You can add a user to sudoers.
- You can setup the user privileges in sudo per user basis.
Now the first approach:
Add User to Group
Here, the good thing is, By Default, the Ubuntu 16.04 can provide the full privileges to all users in the sudo group.
Let us see, How to add a user to sudoers.
You can see what group the new user exists by using the below command.
$ groups newuser Output newuser : newuser
The new user will be at their own group created during the account creation. Now, let use usermod command to add the user to the new group.
$ usermod -aG sudo newuser
-aG tells the usermod to add the new user to the listed groups.
To List Users in Ubuntu use
$ getent passwd
Testing the Users sudo user Privileges
The user can execute the command with administrator privileges.
You can execute the command as normal user with the user privileges like
You can also execute the above command with administrator privileges by adding sudo in front of the command.
Now, you will be asked to enter the password of the regular user which you used to log in.
Specifying user privileges in sudo configuration.
This is the another method where you will write the user privileges per user in the sudo configuration file.
Here, you will use vsudo command, Using vsudo is the best way to open /etc/sudoers.
Because vsudo locks the file against multiple simultaneous edits and performs sanity checks on file before start writing on it.
It prevents you from losing sudo privileges in case you misconfigure it. So you won't fall in trouble while fixing the problem.
If you are a root user now, then execute the following command.
In case, if you signed as a non-root user, but have sudo privileges, just type
$ sudo visudo
The visudo opens the /etc/sudoers in the traditional vi editor. This will confuse the new users. So to avoid the confusion, it is better to open the etc/sudoers in nano.
It will look like familiar. In that configuration file, you have to search for the following line.
Move the cursor using arrow keys to search the file.
/etc/sudoers root ALL=(ALL:ALL) ALL
Look at the above line. It shows that root user has all the privileges.
For the new user, you have to give the same permission.
Add the line below as mentioned.
/etc/sudoers root ALL=(ALL:ALL) ALL newuser ALL=(ALL:ALL) ALL
You have to add this line for new users whom you want to give sudo privileges.
Once finished, save and close it by CTRL+X and Y, then press Enter.
If you want to change the user password, you can use Ubuntu change user password command.
$ sudo passwd username
How to delete the user in Ubuntu
If you no longer need the user, you can delete the user account without deleting the file by executing the Ubuntu delete user command as root.
# deluser newuser
If you are a user with sudo privileges, execute the below command.
$ sudo deluser newuser
In case, if you like to remove the user along with its home directory, you can execute the below command as root.
# deluser --remove-home newuser
If you have to do the same task as a non-root user, you have to execute the following command.
$ sudo deluser --remove-home newuser
If you deleted a user with sudo privileges, you have to remove the privileges mentioned in the sudo configuration file.
To do that execute the below command.
If you are non-root user and have sudo privileges, execute the below command.
$ sudo visudo root ALL=(ALL:ALL) ALL newuser ALL=(ALL:ALL) ALL # Remove this line
We have to do this because, if we create a new user, he may accidently be given the sudo privileges.
So, it is the precaution to prevent the fault.
Now you have learned how to add and remove users in Ubuntu along with allocating privileges.
It is very important for you to know allocating the users with privileges that they require to do their job.
The User management is important when handling the servers and you have to careful about that.
The procedure is almost same with previous versions of Ubuntu.