Add and Delete users on Ubuntu and CentOS


To totally unlock this section you need to Log-in


Login

NOTE: the lines that the user needs to enter or customize will be in red in this tutorial. The rest should mostly be copy-and-pastable.

When you log into a new freshly spun up droplet, you are accessing it from the root user. Although this gives you the power to make any changes you need on the server, you are much better off creating another new user with root privileges on the server. Additionally, if other people will be accessing the server, you will need to make new users for them as well. This tutorial will go over creating a new user, granting them root privileges, and deleting users.

When you perform any root tasks with the new user, you will need to use the phrase “sudo” before the command. This is a helpful command for 2 reasons: first, it prevents the user making any system-destroying mistakes; second, it stores all the commands run with sudo to a file where can be reviewed later if needed. Keep in mind however, that this user is as powerful as the root user.

Setup

This tutorial requires access to the root user or a user with sudo privileges.

Users on Ubuntu

To add a new user in Ubuntu, use the adduser command, replacing the "newuser" with your preferred username.

sudo adduser newuser

As soon as you type this command, Ubuntu will automatically start the process:

  • Type in and confirm your password.
  • Enter in the user’s information. This is not required, pressing enter will automatically fill in the field with the default information.
  • Press Y (or enter) when Ubuntu asks you if the information is correct.

Congratulations! You have just added a new user. You can log out of the root user by typing exit and then logging back in with the new username and password.

How to Grant a User Root Privileges

As mentioned earlier, you are much better off using a user with root privileges.

You can create the sudo user by opening the sudoers file with this command:

sudo /usr/sbin/visudo

Adding the user's name and the same permissions as root under the the user privilege specification will grant them the sudo privileges.

# User privilege specification
root    ALL=(ALL:ALL) ALL 
newuser	ALL=(ALL:ALL) ALL

Press Ctrl + X to exit the file and then Y to save it.

How to Delete a User

Should you find that you find that you no longer want to have a specific user on the server you can delete them with a single command.

sudo userdel newuser

Finish up by the deleting the user’s home directory:

sudo rm -rf /home/newuser

Users on CentOS 6

To add a new user in CentOS, use the adduser command, replacing the “newuser” with your preferred username.

sudo adduser newuser

Follow up by providing the user with a new password, typing and confirming the new password when prompted:

sudo passwd newuser

Congratulations! You have just added a new user and their password. You can log out of the root user by typing exit and then logging back in with the new username and password.

How to Grant a User Root Privileges

As mentioned earlier, you are much better off using a user with root privileges.

You can create the sudo user by opening the sudoers file with this command:

sudo /usr/sbin/visudo

You will find the section to make the user privilege modifications at the bottom of the file. Type “a” to start inserting text. Adding the user’s name and the same permissions as root under the the user privilege specification will grant them the sudo privileges.

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
newuser ALL=(ALL)       ALL

Save and exit the file by pressing Shift + Z.

How to Delete a User

Should you find that you find that you no longer want to have a specific user on the server you can delete them with a single command.

sudo userdel newuser

You can add the flag “-r” to the command if you would like to simultaneously remove the users’s home directory and files.

sudo userdel -r newuser

Check if user is logged out ard force him to logout

You may use who to check which users are logged in:

who

You can log-out the user by sending the KILL signal to the user-process with:

sudo pkill -KILL -u <username>
(Which is same as sudo pkill -9 -u <username>)

Example:

sudo pkill -9 -u guest-2Rw4Lq
(To kill a guest session user named guest-2Rw4Lq)