Using Linux VPS means you will often have to know how to add and remove a user. After creating a new VPS you are given only the root account as the default.
This will give you a lot of power and flexibility, however it is also dangerous and might be destructive. For this reason, it is usually a better idea to add an additional, unprivileged user to do common tasks.
You will also need to create additional accounts for different users which you might have on your system. Every user will have a different account.
You can still get administrator privileges when you require them through a mechanism known as sudo. In this tutorial we will teach you how to create user accounts and then assign sudo privileges and delete users.
How To Add a User
If you are already signed in as the root user, you may create a new user at any time by entering the below.
If you have signed in as a non-root user that has been given sudo privileges, you can append a new user by entering the following.
sudo adduser firstuser
You should be asked a couple of questions. The process will be as follows.
- Assigning and confirming a password for the new user
- Type any additional information about the new user. That part is completely optional and may be skipped by hitting ‘ENTER’ in the case that you do not want to utilize these fields.
- Lastly, you will be prompted to confirm that the information you have provided was correct. Type ‘Y’ to continue.
Your new user should now be ready to use. You will now be able to log in with the password you have set up.
Note: Continue only if you would like to add access for your new user to have administrative functionality
How To Grant a User Sudo Privileges
If your new user needs to have the ability to execute commands using root (administrative) privileges, you will have to give the new user access to sudo.
You can do this by using the ‘visudo’ command which will open the appropriate configuration file in your editor. It is the safest way to make these changes.
If you have signed in already as the root user, type the below.
If you are signed in with a non-root user with sudo privileges, enter the below.
Look for the line looking like the below.
root ALL=(ALL:ALL) ALL
In the line below, copy the format you see here, replacing only the word ‘root’ to reference the new user which you want to give sudo privileges to.
root ALL=(ALL:ALL) ALL firstuser ALL=(ALL:ALL) ALL
You have to add a new line like this for each user that is given full sudo privileges.
After you are finished, you may save and close the file by pressing CTRL+X, followed by ‘Y’ and afterwards hitting ‘ENTER’ to confirm.
Next, your new user should be able to run commands using administrative privileges.
After signing in as the new user, you may run commands as your normal user by entering commands as normal.
You can run the same command with administrative privileges by entering sudo after the command.
You will be prompted to enter the password of the regular user account you have signed in as.
How To Delete a User
In the event that you no longer need a user, we suggest to simply delete the old account.
You may delete the user itself without deleting each of their files by entering this as root.
If you have signed in as another non-root user with sudo privileges, you should instead the following.
sudo deluser firstuser
If, you would rather delete the user’s home directory after the user is deleted, you can issue the following command as root.
deluser --remove-home newuser
If you are executing this as a non-root user with sudo privileges, you can enter this instead.
sudo deluser --remove-home newuser
If you have already configured the sudo privileges for the user you deleted, you might want to remove the relevant line again by entering the below.
Instead you can also use this if you are a non-root user with sudo privileges.
root ALL=(ALL:ALL) ALLfirstuser ALL=(ALL:ALL) ALL # DELETE THIS LINE
This should prevent a new user created with the same name from being accidentally given sudo privileges.
You now know how to add and delete users at ease from your Ubuntu 14.04 system. Effective user management should allow you to separate users and append them only the permits that are necessary to do their job.