Please refer instead to our updated quickstart tutorial, How To Create a New Sudo-enabled User on CentOS 8.
The sudo
command provides a mechanism for granting administrator privileges, ordinarily only available to the root user, to normal users. This guide will show you the easiest way to create a new user with sudo access on CentOS, without having to modify your server’s sudoers
file. If you want to configure sudo for an existing user, simply skip to step 3.
Log in to your server as the root
user.
- ssh root@server_ip_address
Use the adduser
command to add a new user to your system.
Be sure to replace username with the user that you want to create.
- adduser username
Use the passwd
command to update the new user’s password.
- passwd username
Set and confirm the new user’s password at the prompt. A strong password is highly recommended!
Set password prompts:Changing password for user username.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
Use the usermod
command to add the user to the wheel
group.
- usermod -aG wheel username
By default, on CentOS, members of the wheel
group have sudo privileges.
Test sudo access on new user account
Use the su
command to switch to the new user account.
- su - username
As the new user, verify that you can use sudo by prepending “sudo” to the command that you want to run with superuser privileges.
- sudo command_to_run
For example, you can list the contents of the /root
directory, which is normally only accessible to the root user.
- sudo ls -la /root
The first time you use sudo
in a session, you will be prompted for the password of the user account. Enter the password to proceed.
Output:[sudo] password for username:
If your user is in the proper group and you entered the password correctly, the command that you issued with sudo should run with root privileges.
Here is a link to a more detailed user management tutorial:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Software Engineer @ DigitalOcean. Former Señor Technical Writer (I no longer update articles or respond to comments). Expertise in areas including Ubuntu, PostgreSQL, MySQL, and more.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi, this doesn’t work for me. I added my new user to the wheel group (on centos 6.8 x64) and I just get a message saying that I am not a sudoer and that this will be reported.
How do I complete this setup?
This doesn’t working anymore!! please delete the tutorial. to not confuse other peoplel
that worked for me as expected (centos 7), you should open an other NEW SESSION and make test
e.g. ls -la /root
You can make things more “proper” :
Add new group, let say mycompany
#groupadd mycompany
Go under /etc/sudoers.d/
and create new file, let say 70-mycompany-users
#vi /etc/sudoers.d/70-mycompany-users
Add these lines
# User rules for mycompany (add comments ...)
%mycompany ALL=(ALL) ALL
Add user to group
#usermod -aG mycompany username
Now open NEW SESSION with username credits and make test
$ls -la /root
ls cannot open directory /root : Permission denied
Now prefix that commande with sudo
$sudo ls -la /root
[sudo] password for username : *******
and bingo !
Hope this help someone else.
The easiest way to allow a user to have same rights as root is as follows:
edit /etc/sudoers file and below this section:
%wheel ALL=(ALL)
add similar line for your user e.g.: %username ALL=(ALL)
I believe that the below command is unnecessarily then (but I have done it as per the manual above):
The above works in you home lab/project where only a single or a few users can be added with root priviledges and no password is required (for convenience) when running root-related commands e.g. sudo yum update or like earlier: sudo ls -alh /root etc.
Now this tutorial is complete! have fun people!
I’m unsure if CentOS changed this in their latest release, but after adding the new user to the wheel
group, I needed to uncomment this line in the sudo
file:
#%wheel ALL=(ALL) ALL
This gives wheel
sudo access.
Thanks, this works for me. The only issue it hadn’t stated is that it only takes effect in a new session. i.e. Starting a new terminal doesn’t work, While doing logout and login again worked for me.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.