sudo command in Linux with examples

The sudo command in Linux is one of the most important and widely used commands for managing system permissions. It allows you to run a command as the superuser, giving you elevated privileges that are necessary for performing administrative tasks on the system.

This article is a follow-up to the previous 90 Linux Commands frequently used by Linux Sysadmins post. As time allows, I will publish articles on the 90 commands geared toward Linux sysadmins and Linux power users.

 

sudo Command in Linux

sudo command in Linux - i am root
Image source: wallbase.cc.

The sudo command in Linux is used to execute a command with administrative privileges, also known as superuser or root access. The root user has complete control over the system and can perform any action, including modifying system files, installing software, and modifying system settings.

By default, most Linux distributions do not allow regular users to run commands as the root user. Instead, they must use the sudo command to temporarily elevate their privileges to the root level. This helps to prevent accidental damage to the system and to maintain security by allowing users to perform administrative tasks only when necessary.

 

sudo Command Examples

The most basic usage of the sudo command is to run a command as the superuser.

To run a command as the superuser, use the following:

sudo less /var/log/syslog

If you need to edit a file as the superuser, you can use the --edit option with the sudo command.

To edit a file as the superuser with your default editor, use the following:

sudo --edit /etc/fstab

In some cases, you may need to run a command as another user or group. To do this, you can use the --user and --group options with the sudo command:

sudo --user=user --group=group id -a

To repeat the last command prefixed with sudo (only in bash, zsh, etc.),

For example, if you just ran the following command ls /tmp

You can repeat it with sudo using the following command:

sudo !!

If you need to launch the default shell with superuser privileges, you can use the --login or --shell options with the sudo command.

To launch the default shell with superuser privileges and run login-specific files (.profile, .bash_profile, etc.), use the following:

sudo --login

To launch the default shell with superuser privileges without changing the environment, use the following:

sudo --shell

To launch the default shell as the specified user, loading the user’s environment and reading login-specific files (.profile, .bash_profile, etc.), use the following:

sudo --login --user=user

To launch ist the allowed (and forbidden) commands for the invoking user, use the following:

sudo --list

 

Conclusion

The sudo command is a powerful tool that allows you to run commands as the superuser, edit files as the superuser, run commands as another user or group, repeat the last command with sudo, launch the default shell with superuser privileges, and much more.

Whether you are a beginner or an experienced Linux user, the sudo command is an essential tool that you will regularly use in your day-to-day work. By understanding its various options and capabilities, you will be able to use it more effectively and efficiently to manage your system.

Tags: , , ,

Discussion

  1. I don’t know why, But I keep forgetting sudo !! exists lol. Also did not know about --edit and --group. This could be useful when I have to adapt some programs for sysfs.

  2. Thanks for the feedback. Sometimes when posting about well-known commands, it’s a worry if it would be useful to a large audience.

    I have also either forgotten or not aware of a lot of these command options. Good to know I’m not alone. I will continue this series.



Top ↑