Use The SUDO Command On Linux Properly!

Table of Contents

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 command on linux 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 root 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 to use sudo. We can check if the user we are currently logged in as has sudo permissions by executing the following in a terminal:

				
					sudo -v
				
			
If your account has sudo privileges 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.
				
					groups
				
			
On Debian\Ubuntu systems you’ll see the sudo group and on Redhat\Rocky\Arch you’ll see the wheel group. 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 usermod command to add yourself to the sudo group (which ironically you’ll need to have sudo permissions to use).
				
					sudo usermod -aG sudo linuxman
				
			
Once you have that verified we can now run some commands as sudo.

Dude where's my sudo?

If for some insidious reason your terminal returns an error stating: sudo: command not found, worry not! You only have to install the sudo command 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 root so that you can install sudo.

Debian\Ubuntu

				
					apt install sudo
				
			

RHEL

				
					yum install sudo
				
			

Arch

				
					pacman -S sudo
				
			

After sudo is installed and while still logged in as root, simply add your user to the sudo/wheel group as outlined above and you’ll be all set!

sudo Command On Linux Basics

To use the sudo command simply type sudo followed by the command you want to execute with elevated privileges.
				
					sudo <command>
				
			
One of the first tasks most Linux users will have to complete will be updating their system. Depending on you package manager you’ll often have to use sudo to update the system. For example, on Debian based distros you’ll need to use sudo in front of apt in order for the updates to actually be applied.
				
					sudo apt-get update
				
			
Once the command is entered sudo will 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

Another common task will be editing certain config files, especially anything not under your user’s home will likely require sudo permission 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

You can also run a command as another user or group:
				
					sudo --user=<username> --group=<group> <command>
				
			
A real world example you can use to illustrate the above can be something simple like this:
				
					sudo --user=linuxman --group=sudo groups
				
			

Run last command as sudo

Here’s a handy shortcut for running the last command prefixed with sudo (only in bash, zsh, etc.).
				
					sudo !!
				
			

This one is fantastic for those times a command doesn’t run quite right the first time around.

Launch default shell as another

sudo can also launch a elevated instance of the default shell as any user on the system using the following:
				
					sudo --login --user=<username>
				
			
This command will load the user’s environment and any login-specific files required.

Check sudo list

We can also check what commands a user has access to.

				
					sudo --list
				
			
We can also double up this command with the previous command to check a specific user’s sudo privileges:
				
					sudo --user=linuxman --group=sudo sudo --list
				
			
Here’s some example output from a machine running EndeavourOS which uses wheel as its sudo group name.
SUDO Command On Linux
sudo --list on an Arch based distro

sudo conclude

To wrap this up, I just want to give a general warning: We all know that Uncle Ben quote right? Even though we aren’t logged in as root, we can still do a lot of damage with sudo, take precaution! Edit the wrong file or run sudo rm in the wrong place and your system is done for. Always double check your directory with pwd before running sudo commands and follow proper security protocols and you’ll have a happy, stable, secure system.

Meet the Author

Leave a Reply