Friday 20 January 2017

How to Add and Delete Users on Ubuntu 14.04 Server


Once a new server is created, you will be only given the root privilege. If you want other users to access the same sever, you need to add users accordingly. After adding the users you can give super user privileges to the new user too. In this post, I will show you how to add, delete users and assign sudo privileges to the users.

1. How to Add User

If you are signed in as a root user

adduser username

If you are not signed in as a root user but have sudo privilege, you can enter

sudo adduser username

After you enter the command, you will be asked some questions

enter password

re-enter password

Once you have entered the password, you will be asked to enter user details. This is optional and can be skipped by hitting ENTER button. At the end, you will be asked to confirm by pressing 'Y'.

You have successfully created a new user. Now you can login using the username and password.

2. How to Grant Sudo Privileges to the New User

If you want to the new user to have sudo privilege, then you need to add the user to the sudo file

If you are signed in as a root user

visudo

If you are not signed in as a root user but have sudo privilege, you can enter

sudo visudo

Search for the line that looks like

root    ALL=(ALL: byALL) ALL

Here, all you need to do is just below this line copy and paste the same and replace 'root with the 'username' of the new user

root    ALL=(ALL:ALL) ALL
username    ALL=(ALL:ALL) ALL

Now, save and close the file.

The new user has sudo access and can execute commands.

3. How to Delete User

In some cases, you need to delete the users once their work is done. You can do the same this way

If you are signed in as a root user

deluser username

If you are not signed in as a root user but have sudo privilege, you can enter

sudo deluser username

Above command will delete the user from your machine.

If you want to delete the user's home directory, enter the command

If you are signed in as a root user

deluser --remove-home username

If you are not signed in as a root user but have sudo privilege, you can enter

sudo deluser --remove-home username

If you have given sudo privilege to the deleted user, then don't forget to remove the previously added line in the visudo file

root    ALL=(ALL:ALL) ALL
username    ALL=(ALL:ALL) ALL  #Delete this line

Congratulations, you have successfully added, deleted and assigned sudo access to the new user.

No comments:

Post a Comment