Not all the commands run the same. Some of them need elevated privileges to run. For instance, running a LAMP server (Linux Apache MySQL PHP server) and manually editing config files need extra privileges.

This is when we need sudo. But what is sudo, and how can we use it? If you’re familiar with using Windows, we can say it is somehow similar to the “run as administrator” option. 

What is sudo in Linux?

Sudo command in Linux, which stands for “Super User do”, is a prefix of Linux commands. When you use this prefix for special commands, you can run that command as a super user. 

When you have executed a command at the end, you will see an error message; This error indicates that you do not have permission to execute that command; the fastest solution is sudo.  

So, Sudo permits to do commands with high privileges like a super user. Users need to have the password to verify themselves. 

How to install the sudo command?

Most Linux distributions have the sudo command package pre-installed and by default. You can follow these steps to check and see if the package is installed on your system.

  • For the first step, open the terminal window.
  • Enter the word “sudo” and press enter.
  • A prompt will appear if the package is already installed on your device.
  • If not, you may see this message: “sudo command not found”.
  • You can easily install the package:
    • On Debian and Ubuntu: “apt install sudo”
    • On Fedora and CentOs: “yum install sudo”

Since sudo is designed to allow normal users to act as superuser, there must be a way to determine which users on a system have access to it. To accomplish this, Linux uses the sudoers file, which is simply a file that specifies which users are allowed to use sudo.

Adding yourself to the sudoers file is a little different depending on your Linux distribution. To add yourself on Debian, Ubuntu and other similar distributions, open a window and enter:

usermod -aG sudo username

Replace “username” with the username you want to add to the sudoers file and access that username.

To add yourself on CentOS or Fedora, open a window and enter:

usermod -aG wheel username

Replace “username” with the username you want to add to the sudoers file, and it will get sudo access.

How to use sudo in Linux

How to use the sudo is as follows:

sudo OPTION. COMMAND

The sudo has many options that control its behaviour. To use it, put sudo as a prefix:

sudo command

Sudo checks if the created user matches the sudor's evaluation by reading the etc / sudoers/file. When you enter the server, and you intend to use the Sudo command, you must enter the username and password. Then, you will be able to execute other commands as the root user.

How to change password timeout using sudo in Linux

By default, if five minutes pass and there is no activity, Sudo will ask you to enter your password again. You can change the default timeout by editing the sudoers file. Open the file with visudo:

sudo visudo

Set the default period by adding the following line; you can change the number 15 in the timestamp field:

Defaults timestamp_timeout=15

If you want to change the time period for a specific user, apply the changes from the user_name field.

Defaults: user_name timestamp_timeout=15

How to run commands with a user other than the Root user

There is a misconception that sudo is only used to grant Root permissions to a normal user. You can use sudo to run a command as any user.

The -u option allows you to run a command as a specified user.

In the following example, we use sudo to run the whoami command as the user "Jim":

sudo -u Jim whoami

The whoami command prints the name of the user running the command:

output

Jim

??Unlock the true power of your website with our cutting-edge Linux VPS hosting. Benefit from lightning-fast performance, dedicated resources, and complete control, ensuring a seamless online experience for your visitors and skyrocketing your online success.??

What are some of the important options of sudo in Linux?

In this part. We will explain some of the most important options in the sudo command:

1. –V

You can use the V option which stands for “Version” when you want the sudo command to print the version’s number and exit. 

2. –l

You can use the l option, which stands for “list”, when you want the sudo command to print the various commands which are permitted by the user over the current host. 

3. –h or –help

You can use the h option which stands for “help” when you want the sudo command to print the command options and syntax and exit.

4. –v 

V stands for “validate”, and you can use it for refreshing the time limit. This asks for the user’s password if necessary. As this option doesn’t execute a command, it won’t have a result.

5. –k 

k, which stands for kill, can be used when you want to end the current privileges of sudo commands. 

6. –b 

This one stands for “background”. You can use it to run your command in the background. 

7. –K

This is somehow similar to the “kill” option, but it stands for “sure kill”. It means it can be used to remove the user’s timestamp entirely.

8. –p 

You can use the prompt option for applying a custom password and cancelling the default one. 

9. –n

n stands for “non-interactive”. You can run any command without the need to enter passwords. It could be useful when you want to run commands as background tasks. 

10. –u

This one stands for “user” and lets the sudo command run the command as a user other than the usual root. You can use #uid to specify a uid rather than the username. 

11. –H

H, which stands for “Home”, sets a HOME environment variable to the root user's home directory. 

12. –s

This one stands for “shell” and, through a SHELL environment variable, runs the shell specified. This happens when it is set or the shell specified by the invoking user's password database entry.

13. –S

S stands for “stdin” and enables the sudo command to read the password through standard input instead of the terminal device.

14. –:

This option stops sudo processing the command line. 

15. –a

“a”, which stands for “authentication type”, lets the sudo command use the described authentication type when validating a user. 

Conclusion

Sudo in Linux is one of the most important commands, so it is one of the first commands every Linux user should learn to use. Modern Linux distributions can provide a work environment as easy to navigate as Windows or macOS. 

This article discussed what sudo is and how to install and use it. This command is very useful when dealing with permission problems in Linux.

People also read: