The “sudo” Command in Linux [8 Practical Examples]

Sudo can be referred to as the supreme command. It is the abbreviation for “Super User DO”. It allows a user to act as a superuser and run commands accordingly. One can run specific commands prefixed by sudo with boosted rights. In this article, you will learn all the features and aspects of sudo command in Linux.

A. Description

sudo allows a permitted user to execute a command as the superuser or another user, as specified by the security policy. It is considered analogous to the “run as administrator” process of Windows.

B. Syntax

From the man page, you can get a brief overview of the sudo command. The syntax for the sudo command is pretty simple which is the command itself followed by some specific options and then the command you want to run as a superuser.

sudo [OPTION]...[COMMAND]...
Note: In the above syntax OPTION enclosed by a square bracket and followed by 3 dots represents that multiple options can be utilized at the same time.

Synopsis of sudo command in linux

C. Options

You can see the manpage of the sudo displays on the screen. In the synopsis, you can see the options of this sudo command that can be executed. I will describe some useful options for your better understanding.

Useful Options

  • -D directory, –chdir=directory (executes the command in the specific directory)
  • -e (edits one or multiple files instead of executing commands)
  • -h (Displays a short help message and exits)
  • -l (runs specific commands as the root user)
  • -k (kills the user’s timestamps)
  • -u user, –user=user  (executes the command as a user other than the specific default user. )
  • -v (Prints the sudo version string as well as the version string of any configured plugins)

Installing sudo Command in Linux

Most of the Linux distributions have the sudo package preinstalled. In my machine, it was preinstalled also. To check whether you have it preinstalled or you just type the below command on the command prompt:

sudo

It will look like the picture below if you have it already on your machine. You will see a list of short help.getting a short help message after giving sudo command for preinstalled usersIf don’t have sudo already installed on your machine, you can follow the below instructions.

Steps to Follow >

➊  At first open the Ubuntu Terminal.

   Type the following command in the command prompt:

sudo apt install

➌ Type your password.

➍ Now, press the ENTER button.

After completing the entire process, sudo will be installed on your machine.Installing sudo in Linux

Practical Examples of Sudo Command in Linux

The sudo in Linux is an easy-to-use command which gives you permission to execute a command as the superuser. Some of the most useful applications of the sudo  command have been illustrated below:

Example 1: Get an update using sudo

Being a general user you cannot get any updates. To update any packages you have to follow the below procedure.

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

sudo apt-get update

➌ Now, press the ENTER button.

Output >

You can see in the picture that the package update is installed.Update using sudo command in Linux


Similar Readings


Example 2: Add User to sudo Group

You can add any user to sudo group. Here I am adding a user named abir to the sudo group. To add abir to the sudo group you should follow some steps. Those are described below:

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

sudo usermod -aG sudo abir

Replace abir with the user you want to add.

➌ Type your password.

➍ Now, press the ENTER button.

➎ To check whether the user is added to the group, type the following command:

groups abir

➏ Again, press the ENTER button.

Output >

You can see that in the picture abir is added to the sudo user group.

Adding new user in Linux

Example 3: Use sudo to Run a Command as the Root User

There are some commands that you can not run as a general user. Here, I want to list my root directory but the permission is denied. To run that command as the root user you have to follow the below procedure:

Steps to Follow >

➊  Open the Ubuntu Terminal first.

  Type the following command in the command prompt:

ls /root

Permission is denied for not being the root user.

➌ Type the same command using sudo

sudo ls /root

➌ Now, press the ENTER button.

Output >

You can see in the picture that the snap directory is listed here inside the root.

using sudo to run command as root user


Similar Readings


Example 4: Switch to Root User using sudo

You can change your command prompt by switching to the root user. To do this you need to follow below procedures:

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

sudo bash

➌ Now, press the ENTER button.

Output >

You can see that my command prompt has changed to the root user.

switching to root user using sudo command in inux

Example 5: Use sudo to Run a Command as a Different User

You can run a command as a different user other than the root user. Here, I have used my own user to run a command as the root user. To do this you need to follow below procedures:

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

whoami

borhan is the user here.

➌ Again type the following command in the command prompt:

sudo -u root whoami

➍ Type your PASSWORD.

➎  Now, press the ENTER button.

Output >

You can see that the output of the command has changed from borhan to root.

Running a command as a different user


Similar Readings


Example 6: List User Privileges with sudo Command

You can list your user privileges with the sudo command. To do this you need to follow below procedures:

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

sudo –list

➌ Now, press the ENTER button.

Output >

You can see that my user privileges are shown in the output.Listing user privileges using sudo command in LIinux

Example 7: Run Multiple Commands in One Line

sudo command can be used to run multiple commands in a single command line. I have used 3 commands together in one line. To do this you need to follow below procedures:

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

sudo ls; whoami; date

➌ Type your PASSWORD.

➍ Now, press the ENTER button.

Output >

You can see that the output lists of files and directories are shown first, then the user id and the execution of the date command at the end.Executing multiple commands in one line


Similar Readings


Example 8: Change Password Timeout of sudo Command

The default timeout limit for sudo users in the Ubuntu terminal is set to 15 minutes. After that, you will have to give the password again to execute any command as a superuser. You can change that password timeout limit with the sudo command. Here, I will set my password timeout to 45 minutes. To do this you need to follow below procedures:

Steps to Follow >

➊  At first open the Ubuntu Terminal.

  Type the following command in the command prompt:

sudo visudo

➌ Press the ENTER button.

A nano text editor file will open in your terminal.

➍ Go to the Defaults env_reset line

➎ Change the line to the following line:

Env_reset, timestamp_timeout=45

Opening nano text editor to change password timeout➏ Now, press CTRL+X and Y in the next stage to save the timestamp.

➐ Finally, press the  ENTER button.

Output >

You will not anything in the display but your timeout for the password will be changed accordingly.saving the nano file after writing the password timeout line

Conclusion

After completing this article, you will acquire all the knowledge about the sudo command in Linux. With the given practical examples, you will be able to execute a lot of commands as a power user.


Similar Readings

Rate this post
Borhan Uddin

Hello, I am Borhan Uddin, a resident of Dhaka, Bangladesh. I have completed my undergraduate degree in Electrical and Electronic Engineering (EEE) from Chittagong University of Engineering and Technology (CUET). I love to spend my leisure by playing and watching various movies & dramas. Currently, I am working as a Linux Content Developer Executive here. I would like to learn more about Linux every day of the week and would be keen to share it with you rapidly. Read Full Bio

Leave a Comment