Editing the sudoers file in Redhat based systems


If you have ever freshly installed a CentOS or a RedHat based system then it is quite possible that you have encountered that you cannot execute system level commands, even when you use sudo. The error –

Right now you might be pulling all your hair out in order to figure out why you can’t execute system level commands ?

What’s worth noticing is that when you become root you can execute any command you want.

But worry not, there is a solution. We need to edit the sudoers file in order to allow your user to be able to execute system level commands. To do that, you need to first become root.

Become root by su – Enter the root password when asked.

By default the sudoers file lives in  /etc/sudoers

YOU NEED TO BE REALLY CAREFUL while editing this file. Otherwise you can lose root access to your system forever and then the only way is to reinstall the OS all over again.

So, moving right along. After logging in as root if you run “visudo“. It will open up the sudoers file in the vi text editor. It is higly recommended that you edit the sudoers file using this particular command only.

Here you can see all of the permissions that have been setup. This is a heavily commented configuration file. If you are a first time vi user you wont be able to move anywhere as it is really different from other text editors.

You need to move down the file, by pressing j’

If you keep pressing ‘j’ you will come to this section,

You have to make changes here

Now keep pressing ‘j’ until your cursor comes just below line root ALL=(ALL) ALL

Press ‘i’ to get into inserting mode or the ‘typing mode’.

Type in your user name followed by ALL=(ALL) ALL

Syntax <username> ALL=(ALL) ALL

Example- prateek ALL=(ALL) ALL

By adding above line in sudoers file will allow the user ‘aby’ to run any commands anywhere. In particular, giving ‘aby’ administrator access.

Now its time to save the changes and close the sudoers file.

To do that –

1. Press ‘esc’
2. Type in :wq followed by enter

That’s how you change mode in vi and save, exit the file.

Phew, that was a lot but you are done! You have successfully added you user to the sudoers file. Now you can execute system level commands with ease. (You still need to give in your password though :P)