Skip to main content

The `sudo` Command in Linux

The sudo command, short for "superuser do," is one of the most vital and commonly used utilities in the Linux command line. It allows a permitted user to execute a command as the superuser or another user, as specified in the sudoers file. This feature is particularly beneficial for executing commands that require elevated permissions. This comprehensive guide will introduce you to the sudo command, its syntax, options, and some practical examples.


The general syntax of the sudo command is:


Here, OPTION are the flags or options you can use with the command, COMMAND is the command you want to execute, and ARG are the arguments for the command.

Options and Their Descriptions

Here is a table detailing the options available for the sudo command:

--help-hDisplay a help message and exit.
--version-VDisplay the installed version of sudo.
--user-uRun the command as a specified user other than root.
--preserve-env-EPreserve the user environment when running the command.
--list-lList the allowed and forbidden commands for the user.
--validate-vValidate the user's cached credentials.
--invalidate-kInvalidate the user's cached credentials.

Using Our Test Users

We will be using the user1 and user2 accounts created in the previous article to demonstrate some examples. If you haven't created these users yet, you can do so with the following commands:

sudo adduser user1
sudo adduser user2

Basic Usage

The most basic usage of sudo is running a command as the superuser. For example, to update the package list on your system:

sudo apt update

Using Options

Running Commands as Another User

You can run a command as another user using the -u option. Let's assume user1 is allowed to run commands as user2:

sudo -u user2 whoami

Preserving Environment

To preserve the environment variables while running a command, use the -E option:

sudo -E env

Listing Permissions

To see what permissions a user has, use the -l option:

sudo -l

Validating and Invalidating Credentials

To validate your credentials (useful for caching your password for future sudo commands), use the -v option:

sudo -v

To invalidate the cached credentials, use the -k option:

sudo -k

Combining Options

You can also combine multiple options. For instance, to run a command as user2 and preserve the environment:

sudo -u user2 -E env


The sudo command is an essential tool for Linux users, offering elevated permissions safely and effectively. While it's a powerful tool, caution is advised because running certain commands with sudo can make irreversible changes to the system.

This guide aimed to provide a comprehensive understanding of the sudo command, its syntax, options, and practical use-cases. Mastering sudo can significantly enhance your Linux command line experience, providing a greater level of control over your system.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle