July 27, 2012

Beginner

How To Add and Delete Users on Ubuntu 12.04 and CentOS 6

Tagged In: Linux Basics, Ubuntu, Cent Os

What the Red Means

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 virtual private server. Additionally, if other people will be accessing the virtual 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: 1) it prevents the user making any system-destroying mistakes 2) 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. If you only need a user for a limited number of tasks on the VPS, you do not need to give them root privileges.

Setup


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

You should have received your root password from the welcome email after you launched your droplet.

Users on Ubuntu 12.04


How to Add a User on Ubuntu 12.04


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 ‘cntrl 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 virtual private 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


How to Add a User 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 press “shift” ZZ.

How to Delete a User


Should you find that you find that you no longer want to have a specific user on the virtual private 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

Next Steps


Once you have set up the users will you need, you can start building up your VPS. A good place to start is to install the LAMP stack (a collection of basic web server software) on your droplet, using the tutorials below.

LAMP on Ubuntu 12.04

LAMP on CentOS 6

LEMP on Ubuntu 12.04

LEMP on CentOS 6



By Etel Sverdlov

Share this Tutorial

Vote on Hacker News

Try this tutorial on an SSD cloud server.

Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

Create an account or login:

23 Comments

Write Tutorial
  • Gravatar about 1 year

    The instructions for centOS6 are wrong. I needed to use: "passwd " instead of "sudo passwd" per the guide.

  • Gravatar chris about 1 year

    On Centos, worked as expected. Thanks. Quick question. Sometimes I like to SFTP into the server and add a folder or a file. For instance, into /etc/nginx dir to add sites to sites-enabled. The problem I am running into is that this new user does not have the rights to add a file into /etc/nginx, I get a permission error. I could ssh in and su, then add the files as root, but I am trying to come up with a solution that is both secure and allows for easier interaction via SFTP when I am in the mood for that. Any suggestions? I imagine some sort of chown and chmod might be in order.

  • Gravatar chris about 1 year

    ... here's what I did to make working via the command line and via SFTP a bit easier within the webroot and various config directories. chown -R username:nginx /srv chown -R username:nginx /etc/nginx Seems to be working.

  • Gravatar koro76 about 1 year

    I tried your instructions for Ubuntu 12.04 to no avail. Creating the user worked fine, but your instructions for giving the user root permissions did not work.

  • Gravatar Paul White 12 months

    On Ubuntu 12.10 I needed to also do adduser sudo to enable sudo access to my new username

  • Gravatar bardi_e 12 months

    On ubuntu 12.04 as root I simply did: sudo adduser bardi sudo adduser bardi sudo Very easy if you know how!

  • Gravatar Prasenjit 10 months

    Hi, Please fix the syntax for setting password for new user in CentOS. It is supposed to be

    sudo passwd newuser

  • Gravatar Kamal Nasser 10 months

    @Prasenjit Thanks, updated.

  • Gravatar oscar 10 months

    On Ubuntu to get sudo access all you need to do is add the user to the sudo group which can simply be done by typing: sudo adduser newuser sudo adduser newuser sudo You don't have to manually edit the sudoers file.

  • Gravatar dborchert 8 months

    I did this now I cannot get back into the /usr/sbin/visudo area. It says: sudo: >>> /etc/sudoers: syntax error near line 19 <<< sudo: parse error in /etc/sudoers near line 19 sudo: no valid sudoers sources found, quitting sudo: unable to initialize policy plugin and then it goes back to the prompt. Any suggestions? Did I add a user the wrong way and if so is there a way to just edit the file I just modified and remote the line I added which, according to the error was entered wrong (obviously)

  • Gravatar Kamal Nasser 8 months

    @dborchert: Did you edit /etc/sudoers manually?

  • Gravatar dborchert 8 months

    I did. Only because in the GUI, when I went to User Accounts, I couldn't add anything. It kept saying to click the Unlock icon but not matter what I do I can't click it -- almost like it's just an image, not a button.

  • Gravatar dborchert 8 months

    Kamal, fixed it. http://askubuntu.com/questions/209558/broken-sudo-sudo-parse-error-in-etc-sudoers-near-line-23

  • Gravatar crispyO 7 months

    I had the same problem as chris above in wanting to upload from a new user via SFTP but getting permission denied. I followed his guidelines and set this on Ubuntu: chown -R username /var/www This gives me privileges to upload to any of my domains through my new user. But - it doesn't feel like that's the correct way of doing it? Can anyone tell me of a better solution?

  • Gravatar Kamal Nasser 7 months

    @crispyO: If you want to share a certain domain's files with two users, you have to create a 'developers' (it could be named anything you want) group (

    groupadd developers
    ) and add both users to that group (
    usermod -a -G developers user1 # same for user2
    ). Once you've done that, you should change the site's file's group to 'developers' and add group write permissions:
    sudo chgrp -R developers /var/www/domain.com
    sudo chmod -R g+w /var/www/domain.com
    I also recommend setting the setgid bit so that any new file's group is automatically set to developers:
    sudo chmod -R g+s /var/www/domain.com

  • Gravatar ethan 7 months

    What if I want to create a user that *doesn't* have root privileges? What if I want to restrict a certain user to a specific folder?

  • Gravatar Kamal Nasser 7 months

    @ethan:

    sudo adduser newuser
    should create a new user without root/sudo privileges.

  • Gravatar Pablo of vDevices.com 4 months

    Suggestion: What ever happened to America being the land of equal opportunity? How 'bout adding links to DigitalOcean's LEMP stack articles in the Next Steps section?

  • Gravatar Kamal Nasser 4 months

    Pablo, I believe the LEMP articles didn't exist when this one was written. I'll add them, thanks :]

  • Gravatar eMangaka about 1 month

    I can't use my keyboard. I can't = or : ....

  • Gravatar Kamal Nasser about 1 month

    @eMangaka: Are you using the remote console? Try using SSH instead, that might fix it.

  • Gravatar aravind.eriventy about 1 month

    this much of Information is very helpful to us

  • Gravatar aravind.eriventy about 1 month

    This Information is very helpful to us

Leave a Comment

Create an account or login:
Ajax-loader