Monday, September 28, 2015

How add a user to sudoers file in RHEL7 / CentOS7




bash-4.2$ sudo systemctl start service
[sudo] password for userOne:
userOne is not in the sudoers file.  This incident will be reported.

If you receive “userOne is not in the sudoers file.  This incident will be reported” message when you trying to run commands using sudo, then you have to update sudoers file. 

To do that you have to edit sudoers file. 
There are two ways to do that.


  1.   Using visudo command – this is the safest way to do changes in sudoers file. And it can be used to prevent configuration errors of using sudo. When you use visudo command, it will open sudoers file using vi editor directly and you can edit sudoers file.
  2.  By directly editing sudoers file using editor. By default sudoers file comes with read only priviledges. So first change permissions from read only to editable. Then open sudoers file using any editor you like, # vi /etc/sudoers


Now let’s check how to add user in sudoers file,
Find the below code that comes by default,

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

then add user details under the code,

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

Save and exit. Also remember to change privileges of sudoers file If you used second method to add details.