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
Image source: wallbase.cc.
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
--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
You can repeat it with sudo using the following command:
If you need to launch the default shell with superuser privileges, you can use the
--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:
To launch the default shell with superuser privileges without changing the environment, use the following:
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:
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.
I don’t know why, But I keep forgetting
sudo !!exists lol. Also did not know about
--group. This could be useful when I have to adapt some programs for sysfs.
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.
Also, I am now suddenly online. Every day hopefully.