sudo (short for superuser do) is one of those commands that anyone who has ever used Linux has had to use at some point or another. The closest comparison would be the “Run as Administrator” option in Windows—it runs a program with elevated privileges, allowing it to access otherwise restricted parts of the operating system.
sudo does very much the same thing.
sudo allows users to run commands with elevated privileges or as another user. That in and of itself makes it one of the de-facto commands that any Linux user should familiarize themselves with.
Why use sudo?
We could just log in as
root and execute whatever command but we don’t want to do that as it is a massive security risk.
sudo lets us execute elevated commands as if we were
but as a more “restricted” normal user.
sudo uses the systems’ security policy (groups) which determines who can & can’t run
sudo. Obviously you need to be using a user that has permission
sudo. We can check if the user we are currently logged in as has
permissions by executing the following in a terminal:
sudoprivileges you’ll be prompted for the password, otherwise you’ll get an error message, you can also run the groups command which will output all the groups your current user is a part of.
sudogroup and on Redhat\Rocky\Arch you’ll see the
wheelgroup. If you don’t see that group you’ll need to contact your system admin or if you are the admin you’ll need to use the
usermodcommand to add yourself to the
sudogroup (which ironically you’ll need to have
sudopermissions to use).
sudo usermod -aG sudo linuxman
Dude where's my sudo?
sudo: command not found, worry not! You only have to install the
sudocommand to your system. This can be done in a few ways (no surprise) depending on your distro. In general the first step you’ll need to do is logout of your current user and switch to
rootso that you can install
apt install sudo
yum install sudo
pacman -S sudo
sudo is installed and while still logged in as
root, simply add your user to the
wheel group as outlined above and you’ll be all set!
sudocommand simply type
sudofollowed by the command you want to execute with elevated privileges.
sudoto update the system. For example, on Debian based distros you’ll need to use
sudoin front of
aptin order for the updates to actually be applied.
sudo apt-get update
sudowill prompt you for your password and once authenticated, the command will be executed as a superuser with elevated permissions.
Quick start sudo options
Editing a file as sudo
sudopermission to edit. The following command will open your default terminal text editor in an elevated state.
sudo --edit /etc/fstab
Running a command as another
sudo --user= --group=
sudo --user=linuxman --group=sudo groups
Run last command as sudo
This one is fantastic for those times a command doesn’t run quite right the first time around.
Launch default shell as another
sudocan also launch a elevated instance of the default shell as any user on the system using the following:
sudo --login --user=
Check sudo list
We can also check what commands a user has access to.
sudo --user=linuxman --group=sudo sudo --list
sudo, take precaution! Edit the wrong file or run
sudo rmin the wrong place and your system is done for. Always double check your directory with
sudocommands and follow proper security protocols and you’ll have a happy, stable, secure system.