Sudo Command | How to use sudo command in linux | use of sudo

Sudo Command

Sudo command stands for either “substitute user do” or “super user do” (depending upon how you want to look at it). What sudo does is incredibly important and crucial to many Linux distributions. Effectively, sudo allows a user to run a program as another user ( Most often the root user ). There are many that think sudo is the best way to achive “best practice security” on linux.

Users can login using their username and password and can issue administrative commands placing sudo in front of the commands, eg sudo rpm –Uvh *.rpm, to tun the command which installs and updates programs in Linux(rpm)

The file /etc/sudoers file has the rules that users have to follow  when using sudo command. That means that whatever commands access is provided to any user in /etc/sudoers file , that user can only run those commands.

Advantages of Using SUDO

Two Best advantages about using sudo are:

  • Limited user privileges

As we have studied above that we can restrict users to use certain commands as privileged user as per the role of the user.

Eg: Networking commands for network user and Admin commands for admin users etc.

  • Logs of the actions done by users

All commands executed by sudo users will be stored in /var/log/secure file, but still if you want you can make your own log file by passing an entry in /etc/sudoers file at the bottom as “Defaults logfile=/var/log/sudo.log” or whatever name you want, to save the logs of what commands is executed by which sudo user.

The /etc/sudoers File:

  • As we read above that it is the configuration file for sudo users, which is used to assign specific commands to the specific users or groups.
  • Always use visudo command to edit this file. It prevents two users from editing the file at the same time, and it also provides limited syntax checking.

When we run Visudo command the output will be as follows.

 

  • As we can see there is basically one line
  • Root ALL=(ALL) ALL
  • This line means that the user root can execute from all terminals, acting as ALL (any) users, and run ALL(any) Command.
  • So the first part is the user, the second is the terminal from where the user can use sudo, the third is as which user he may act, and the last one, is which commands he may run.
  • The advantage of visudo command, while editing if there are any syntax error it will be reflected as follows.

 

Some Options Of SUDO Command

Like any good command there are a few options to make sudo, do more!

sudo su This command will make you the root user and load your custom user environment variables.

sudo –b This command will run the command in the background.  This is useful for commands that display a lot of output as they are running.

sudo –s This command will run the shell specified with elevated privlages, giving you the # prompt (don’t forget to exit!)

Sudo gives us safe elevated privileges when we want to run important commands.  It might be the most used and powerful command among Ubuntu users, as it has become the preferred method in that distribution.  Now that you have the power, be sure to be safe when you issue your commands!  There is no su-undo!

Example:

Allow a user “pooja” all privileges like root

To assign root privileges to user add a line by using sudoers file as shown below.

#visudo (Save file using “wq!”)

First try to run fdisk command normally and see what happens.

  • Now using SUDO command.

It’s all done!

You can learn more about these commands through man pages.

# man sudo

# man su

I Hope you like this article……..

Leave a Reply

Your email address will not be published. Required fields are marked *