Linux sudo command With Examples | Easy Approach

Hello everyone In this Article we are going to learn how to give the normal users root rights in Linux in a very simple and easy step-by-step approach

Yellow Fishing Boat Blog Banner 89

Linux Sudo command

What is Need to create the users

Suppose that we have one server that is running for 24X7 now the users are also logged in to that server if we don’t make any users then they have the root access in that server and if someone will do any bad activity in the server it is very difficult to find out who has done soo the concept of user came in the picture where we can assign different permissions to the users in the server

And the problem with the normal users is that they have very little permission on the server soo the conecept of sudo came into the picture where we can able to assign the same access to the normal user that the root user had.

We have added one user devopstutuser on the server and assigned the password for the devopstutuser

Screenshot 1 7

Now we are logged in as devopstutuser and we can see that we cannot able to execute the commands it is showing permission denied here.

Screenshot 2 7

Now we want devopstutuser to execute the commands that are showing permission denied here commands that we need to give the rights to devopstutuser of useradd and fdisk -l commands

Whatever commands we need to give access to the sudo user we first need to get the binaries of that commands and we can get the binary using which command.

Now get the binary of the fdisk command using which command

Screenshot 3 8

Now get the binary of the useradd command using which command

Screenshot 4 8

Now again we need to log in as root in order to make devopstutuser as sudo user.

Now we need to edit the /etc/sudoers file we are using vi editor to edit the sudoers file

Screenshot 5 8

Now we want that the tutdevopsuser will run the fdisk -l command and the useradd command.

To do this we need to add the binary of the fdisk -l and useradd command in the /etc/sudoers file with the username that we want to make it as sudo

Screenshot 6 7

Now when we run the command fdisk -l command when we login with the devopstutuser then they can able to execute the commands that we have done an entry in the sudoers file.

Screenshot 8 5

Now when we run the command useradd command when we login with the tutdevopsuser then they can able to execute the commands,

Screenshot 9 5

Now we want that devopstutuser to run all of the commands soo we need to do entry in the following format in /etc/sudoers file

Just add the devopstutuser entry in the /etc/sudoers files again in order to make tutdevopsuser to run all of the commands.

Screenshot 10 5

Now tutdevopsuser is able to run all the commands in the Linux system

Sometimes we want that we don’t allow sudo user to run some commands to do this we can add the entry in /etc/sudoers file in the following format

In this case, we don’t want sudo user to run the reboot and power off command. we can add ! the symbol in the commands binary

Screenshot 11 5

We can also add more users in the /etc/sudoers files same as the above method

But in industry, we don’t add the users one by one we make one group then add the members in that group then define the name of the group in the /etc/sudoers file, and add the permission on that group the commands we need to execute

Just add the users harry and Natasha users to the devopstutgroup using the following method

Screenshot 12 5

Now edit the /etc/sudoers file using the following method in order to give the devopsutgroup rights to execute the commands.

Screenshot 13 3

User harry and natasha is the members of the group devopstutgroup now they have right to execute all of the command using sudo method.

How to add the aliases in the /etc/sudoers file

With the help of aliases, we can define one alias and we can define the certain command wrt that aliases now we just need to type the name of the aliases and automatically that commands will execute which is bind with that aliases

Cmd_Alias is the variable in which we can define bunch of commands .And aliases name should be in capital letter

Screenshot 15 2

Here we are using NETWORKING aliases in the devopstutuser. Now will able to execute all of the commands that are defined in the aliases in the above picture

Screenshot 16 2

How to make the user passwordless

When we run the sudo command then it will, again and again, ask for the password to make that user passwordless we can use the following syntax

Screenshot 17 2

Now the system will not ask for the password when the user execute command using sudo

Conclusion

If you want to learn Linux in the most simple language then please click on this link.

This is the complete knowledge of how we can make the normal user as sudo user in the Linux kernel if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on DevOps Articles.

Leave a Comment