Adding a Sudo User, Permission Denied

I am logged in as root and trying to create a sudo user because I receive the error "root user is not in the sudoers file" whenever I try to use sudo

So I tried:

visudo

and received permission denied, then tried:

chmod 755 /etc/sudoers

and received "chmod: changing permission of 'etc/sudoers': Operations not permitted

I running centos7. Any idea of where to go next?

Thanks!

4 Replies

CentOS handles sudo slightly differently then Debian-based distro's like Debian/Ubuntu, and you can read more about adding a limited user with sudo privileges here.

TLDR; This is what you need:

usermod -aG wheel example_user

This will add your example_user to the wheel group, which is how sudoers are configured in CentOS.

Why are you trying to use sudo if you're already logged in as root?

The user would need to use the NOPASSWD directive in his /etc/sudoers file.

If your current user is named ‘user’ and your host ‘host’, you would need to insert the following lines to the file /etc/sudoers:

user host = (root) NOPASSWD: /sbin/shutdown
user host = (root) NOPASSWD: /sbin/reboot

Once this is done, the user ‘user’ will be able run these commands on ‘host’ without a password. The other commands however will still require a password though.

The commands mentioned in the /etc/sudoers file would need to be qualified in full, which means that their absolute path has to be specified correctly as dictated in the sudoers help or man page . A relative path is therefore not accepted and will be considered a syntax error.

In order to run any command in a given directory, the command would need to end with a trailing character (/) and should point to that specific directory. Note that the sub directories are not included in this case.

In the example below, the user is able to run any command in the directory /home/userfolder/bin/:

user host = (root) NOPASSWD: /home/userfolder/bin/

Note: to not lock yourself out of the system, it is good practice to use the command visudo for editing the sudoers file – This is just a precautionary measure in case you unintentionally write an incorrect statement in the sudoers file.
visudo will actually save your altered file to a temporary folder and will only overwrite the original sudoers file if the modified file has no errors.

Checkout this page for additional information. How to use sudo without having to enter a password in Ubuntu

Thank you all for the replies. As you can tell I am new to Linux.

@_Brian thanks, now I am using the correct commands for CentOS but it is still returning permission denied.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct