How To Use ‘sudo’: The Complete Linux Command Guide

Sudo is one of the most popular commands available in the Linux command line and you might even come across memes about it. Despite its popularity, few people know how to exploit its capabilities fully. In this post, I will give you a comprehensive guide on the sudo command and some real-world use cases where you can use it.

In Linux, the sudo command stands for “superuser do.” It allows users to execute commands with the privileges of another user, typically the superuser or root.

You can use the table of contents below to find what you are looking for. However, I recommend you read along so as not to miss anything.

Want the best experience? Become a premium member for ad-free browsing, access exclusive content, and ask questions in our private forums. Your membership helps support the site!
Linux doesn’t have to be intimidating. With my e-book, Master Linux Commands, you’ll uncover the secrets of the terminal in a fun, step-by-step journey. From basics to scripts, get ready to level up your Linux skills. Oh, and did I mention the handy cheat sheet you get as a bonus?

Command syntax

The syntax for using the “sudo” command in Linux looks like this:
sudo [options] command [arguments]

Let’s have a quick look at each part of this command:

  • sudo: This is the command itself, which stands for “superuser do.”
  • [options]: These are optional flags that modify the behaviour of “sudo.” Some common options include -u to specify a different user, -i to start a new shell with root privileges, and -s to run the shell specified by the SHELL environment variable.
  • [command]: This is the command you want to execute with elevated privileges. It can be any valid Linux command.
  • [arguments]: These are optional arguments you can pass to the command being executed.

However, in most cases, you might not need the [options] parameter.

Usage examples

Let’s have a look at some real-world use cases of “sudo” to give you a better understanding.

Update or upgrade the system

Updating and upgrading your Linux system regularly is always recommended to ensure you are running the latest software packages.

These are the two commands you generally use for doing this, and they both start with “sudo”:
sudo apt update
sudo apt upgrade

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Now let’s see what happens when you try to run the two commands without “sudo.”

As you can see, you will get the error “Permission denied.”
That’s because these commands can only be executed with elevated privileges.

Change user password

If you want to change the password of a user on your system, you will need to use the <passwd> command as shown below:
sudo passwd [username]

For example:
sudo passwd johndoe

Now, let’s see what happens when you try running the same command without ‘sudo.’

As you can see, you will get an error. Once again, “sudo” is used to give you temporary administrator privileges, so you can run these commands even if your user doesn’t have the right permissions to do them directly.

Run a command as a different user

Another useful case where “sudo” can be used is to run a command as a different user.

Let’s assume you want to create a file inside the home directory of user “johndoe” but are currently logged in as “raspberrytips.” The only way to do that is by using the ‘sudo’ command with the “-u” option.

See the syntax below:
sudo -u [different_username] [command]

For example:
sudo -u johndoe touch /home/johndoe/newfile.txt

Execute previous commands

In Linux, we use the exclamation mark “!!” to run previous commands. One way you can utilize “sudo” with this command is when you ran a program earlier that required “sudo” but forgot to include it.

For instance, if you ran “apt update” instead of “sudo apt update” to update your system earlier, you can type:
sudo !!

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Run multiple commands in one line

Think of a scenario where you want to run three commands that will all require “sudo.”
For example:
sudo apt update
sudo apt upgrade
sudo apt install vlc

Instead of executing the three commands one-by-one, you can use the command below, which creates a sub-shell:
sudo sh -c 'apt update && apt upgrade && apt install vlc'

Below is a quick overview of the parameters used in the command above:

  • sudo is used to execute the entire sequence of commands with superuser privileges.
  • sh -c starts a new shell and executes the provided command string.
  • The && operator ensures that each command is executed only if the previous one succeeds.

Shutdown or reboot system

Finally, another case when you need “sudo” is to shut down or restart your system.
You can use the commands below:

sudo shutdown
sudo shutdown -r
sudo reboot

When you try executing any of the commands above without “sudo,” you will get the error “command not found.”

Main Options

In the section above, we only used one “sudo” option: the “-u” option, which is used to run a command as a different user. Let’s explore other options that you can use with “sudo.”

  • -s or ‐‐shell: This option allows you to specify the shell to use when executing commands. For example, if the default shell is /bin/bash but you want to run the command using /bin/zsh, you will use the -s option and specify the shell. See the example below:
    sudo -s /bin/zsh [command]
  • -g or ‐‐group: Allows you to run the command as a member of a specified group. For example:
    sudo -g group_name command
    The command above will execute with the privileges of the group group_name.
  • -n or ‐‐non-interactive: This option prevents “sudo” from prompting for a password if authentication is required but not possible. This technique is mostly used when writing scripts to automate a process. You don’t want “sudo” to prompt for a password yet you are not present to type it which will only disrupt the flow of execution.
  • -u or ‐‐user: We looked at this option earlier but didn’t go in-depth. This option allows you to run a command as another user. It mostly comes in handy when you want to run a command with the privileges of another user on the system.
  • -h or ‐‐help: Displays syntax and command options.
  • -V or ‐‐version: Displays the current version of the “sudo” application.
  • -v or ‐‐validate: Refresh the time limit on “sudo” without running a command. Let me explain this further.

    You will notice that when you run a command with sudo, you will be prompted for a password. However, when you run other commands with sudo immediately, you will not see a password prompt because you are still within the “sudo” time limit.

    The -v option enables you to extend the session timeout without running a command.
  • -l or ‐‐list: This option serves two purposes:
    When executed without any command, it lists the privileges of the invoking user, showing what commands they are allowed to run with “sudo.”
    sudo -l

    When executed with a command, it checks whether the specific command is allowed to be run with sudo.
    sudo -l command
    • When executed without any command, it lists the privileges of the invoking user, showing what commands they are allowed to run with “sudo.”
      sudo -l
    • When executed with a command, it checks whether the specific command is allowed to be run with sudo.
      sudo -l command
  • -k or ‐‐kill: This option is used to end the current “sudo” privileges without executing any command. It effectively terminates the sudo session, revoking any elevated privileges granted to the user.

The sudoers File

The sudoers file is a key component in Linux systems. This file determines which users or groups are granted permission to execute specific commands with elevated privileges using the “sudo” command. The path to this file is “/etc/sudoers.”

You have probably seen this error after creating a new user on your system and trying to use “sudo” with this new user. “User is not in the sudoers file. This incident will be reported.”

Adding a User to the sudoers File

If you come across the error above, don’t panic. It only means the user needs to be added to the “sudoers” file. Follow the steps below.

  • Open the “sudoers” file using your favorite command-line editor. In my case, I will go with nano. But you can also use vi or vim.
    sudo nano /etc/sudoers
  • Scroll to the bottom of the file and add the line below. Remember to replace user_name with the exact name of the user you want to add.
    user_name    ALL=(ALL:ALL) ALL

  • Save the file (Ctrl + S) and exit (Ctrl + X).

Adding a User to the sudo Group

When you add a user to the “sudo” group, they will have elevated privileges allowing them to execute commands with “sudo.” Below is a step-by-step guide on how to add a user to the “sudo” group.

  • Open the terminal.
  • Execute the command below. Remember to replace “user_name” with the username you want to add to the “sudo” group.
    sudo usermod -aG sudo username

  • That’s it! You have successfully added a user to the “sudo” group. These configurations are applied immediately and you can go ahead and execute commands with “sudo.” However, if you experience any issues, log out or reboot your system.


su command

Unlike most Linux commands, such as Grep, which typically have several alternatives, the “sudo” command lacks numerous substitutes. The only close alternative is the “su” command which stands for “switch user” or “substitute user”. Even though, this also doesn’t exactly operate like the sudo command.

“su” enables users to switch to another user account entirely, including the root user, by entering the target user’s password.

For example, the command below will switch to another user and even the terminal prompt will change.

su user_name
su phinix

When you look at the image above, you will notice that the user changed from “raspberrytips” to “phinix.”


Like many other commands, the “sudo” command supports various Environment variables. While you may not interact with them daily, these variables serve critical roles in system administration, scripting, and program execution.

Environment Variables

  • EDITOR: Specifies the default editor for editing files using commands like visudo, crontab, or git.
  • HOME: Represents the user’s home directory invoking the “sudo” command.
  • PATH: This variable sets the directory where the shell looks for executable files. If you don’t modify this variable, the default directories should be similar to the those listed in the image below.
  • SHELL: This variable specifies the default shell used by the user.
  • SUDO_PROMPT: Sets the custom prompt displayed by sudo when requesting the user’s password. It allows system administrators to customize the password prompt to provide additional context or instructions.
  • SUDO_COMMAND: Contains the full command line of the commands being executed with sudo. It provides information about the command being run, which can be useful for auditing and logging purposes.
  • SUDO_USER: Contains the username of the user who invoked the sudo command.
Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Related questions

The difference between ‘sudo’ and ‘su’

Earlier, I said that the alternative to using “sudo” is the “su” command. However, I stated clearly that the “su” command doesn’t operate fully like the “sudo” command. Let’s look at the main differences between the two commands.

  • sudo (superuser do):
    • It allows users to execute command with elevated privileges and they will need to authenticate using their own password.
    • The user who invokes the “sudo” command has to be in the /etc/sudoers file.
    • When a command is executed using “sudo,” it will log the information about the command execution.
  • su (Switch User):
    • It allows you to switch to another user, including the root user. However, you will need to provide the password of the target user you are switching to.
    • The “su” command will give you full access to the target user’s environment, including their shell, PATH, and other settings.
Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Reminder: Remember that all the members of my community get access to this website without ads, exclusive courses and much more. You can become part of this community for as little as $5 per month & get all the benefits immediately.

Additional Resources

Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.

VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

Similar Posts