How To Add and Grant Sudo Privileges to Users in Linux RedHat/CentOS

There are two ways to run administrative applications in Linux. 1- You can either switch to the super user root with the su command. 2 - Use sudo.

 What is sudo ? 

Is a program that allows users to run programs with the security privileges of another user, by default the superuser.

So let us add a new user and grant him sudo privileges

The first step is to add a new user. New users, by default, are unprivileged. This means that they will only be able to modify files in their own home directory, which is what we want at this stage. To create a new user user adduser commad
adduser userJoe
  • so we have create our user called userJoe(Joe is a very important user in our big company).
Lets give userJoe a password by using the passwd command 
[root@aodba]# passwd userJoe
Changing password for user userJoe.
New password:  xxxxxx
Retype new password:  xxxxxx
  • you will be asked to type the password two times.

Grant Administrative Privileges to userJoe

Well at this point we have userJoe ready to receive elevated privileges. To make userJoe part of the sudoers group we can use the visudo command or edit the /etc/sudoes file. Logged in as root run the visudo command: When you type this command, you will be taken into a text editor session with the file that defines sudo privileges pre-loaded. We will have to add our user to this file to grant our desired access rights. Find the part of the file that is labeled "User privilege specification":
# User privilege specification
root    ALL=(ALL:ALL) ALL
- so we need to add userJoe as replica of the root user.
# User privilege specification
root        ALL=(ALL:ALL) ALL
userJoe     ALL=(ALL:ALL) ALL
Save and close. Now, when you are logged in as your userJoe user, you can execute a certain command with root privileges by typing:
sudo "command"
You will be prompted to enter your user's password (not the root user's password but userJoe password). The command will then be executed with elevated access. And is done userJoe is now ready ot take on the world :) We can also run the following command that will edit the /etc/sudoers file 
chmod +w /etc/sudoers  echo "userJoe ALL=(ALL)       NOPASSWD: ALL"  /etc/sudoers  chmod -w /etc/sudoers
  • /etc/sudores file is read only by default, so you can alter that, push the userJoe rule as a sudo user and change back the file read only type.
Note: 
  • in this last command we specify that userJoe can  switch to sudo without having to punch in his password due to the NOPASSWD option.
hope this was useful...