linux beginner
Now Reading
sudo command in Linux with examples
0

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

snap(1)

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

Snap(2)

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
snap(3)

Search “Terminal” and press the enter key.
snap(4)

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

snap(5)

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
user1@linuxhelp:~$

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

snap(7)
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
user1@linuxhelp:~

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

Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.

For more help topics browse our website www.linuxhelp.com

Be the first to comment, we value your suggestions. For further queries please comment below.

Leave a Response