sudo command in Linux with examples

Configuring sudo command

Configuring sudo command and the difference between " su" & " sudo" in Linux distributions is discussed in this article.

The major difference between " su" and " sudo" is, the first compels you to share the root password to other users whereas " sudo" allows you to run commands without root passwords.

Switch to root user, enter the following command in the terminal.

Example

user1@linuxhelp:~$ su
Password:
root@linuxhelp:/home/user1#

To Install " sudo"

By using the following command install " sudo" .

root@linuxhelp:/home/user1# apt-get install sudo

Adding username to sudo group

Type the following command, to add username to the group.

root@linuxhelp:/home/user1# adduser user1 sudo
Adding user ' user1'  to group ' sudo' ...
Adding user user1 to group sudo
Done.

Now, add the user name to /etc/sudoers file by using the following command. Hit " Ctrl+x" then press " y" . Finally save the file.

root@linuxhelp:/home/user1# nano /etc/sudoers

To exit out of the Terminal completely, use the following command.

root@linuxhelp:/home/user1# exit
exit
user1@linuxhelp:~$exit

Open a new Terminal and click " Activities" .

Search " Terminal" and press the enter key.

Type the following command to test.

user1@linuxhelp:~$ sudo ls   

We trust you have received the usual lecture from the local system
Administrator. It ususally boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

[sudo] password for user1:
Desktop Documents Downloads Music Pictures Public Templates Videos

Create a new user, using the adduser command.

user1@linuxhelp:~$ sudo adduser user2

To add a new user by using the useradd command

Now to add a new user by using the useradd command

user1@linuxhelp:~$ sudo useradd mark
user1@linuxhelp:~$ sudo passwd mark
Enter new UNIX password:
Retype new UNIX password:
passwd: passwd updated successfully

Delete the created user by running the following command.

user1@linuxhelp:~$ sudo deluser --remove-home mark
Looking for files to backup/remove ...
Removing files ...
Removing user ' mark'  ...
.
.
.
Done.

The --remove-home option will delete the user' s home directory as well.

If you are logged in as root, you do not need to add the sudo before the command.

root@linuxhelp:/home/user1# deluser --remove-home username

To execute ‘ sudo‘ command without entering password

To execute a command without entering password use ‘ NOPASSWD‘ flag.

adam ALL=(ALL) NOPASSWD: PROCS

Here the user " adam" can execute all the commands aliased under “ PROCS” , without entering password.

user1@linuxhelp:~$ sudo nano /etc/sudoers

To delete the user without entering the sudoers password, run the following command.

user1@linuxhelp:~ sudo deluser --remove-home user2
Looking for files to backup/remove ...
Removing files ...
Removing user ' user2'  ...
Warning: group ' user2'  has no more members.
Done.

To login another user, run the following command

user1@linuxhelp:~$ su - adam
Password:

Enter the following command to test.

adam@linuxhelp:~$ sudo ls
    
We trust you have received the usual lecture from the local system
Administrator. It ususally boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
        
[sudo] password for adam:
adam is not in the sudoers file. This incident will be reported.
adam@linuxhelp:~$ exit
logout

Here, the user is not added in the sudoers file.

Tag : Sudo
FAQ
Q
How to add a user to the sudo group?
A
Use command "adduser username sudo" and you can also add the names under "/etc/sudoers" file.
Q
What is the major difference between su and sudo?
A
The major difference between " su" and " sudo" is, the first compels you to share the root password with other users whereas " sudo" allows you to run commands without root passwords.
Q
How to change password in command line for certain users?
A
In order to change the password, use "passwd username"
Q
How to assign 775 permission for the sub-folders in Linux?
A
Use chmod -R 775 followed by the directory path.
Q
How to give all privileges to a particular user in sudo functionality?
A
Add the required user to the wheel group.