Linux sudo commands for beginners
The Linux sudo command gives you safe, elevated privileges to run important commands. Read on to see what sudo can do for you!
Jun 08, 2023 • 6 Minute Read
Sudo, the one command to rule them all. It stands for “super user do!” and is pronounced like “sue dough”.
As a Linux system administrator or power user, it’s one of the most important commands in your arsenal. Have you ever tried to run a command in terminal only to be given “Access Denied?”. Well this is the command for you! But, with great power comes great responsibility! Running the sudo command is much better than logging in as the root user, or using the su “switch user” command. Read on to see what sudo can do for you!
Accelerate your career
Get started with ACG and transform your career with courses and real hands-on labs in AWS, Microsoft Azure, Google Cloud, and beyond.
What is sudo for?
So, what is sudo for and what does it do? If you prefix “sudo” with any Linux command, it will run that command with elevated privileges. Elevated privileges are required to perform certain administrative tasks. Someday you may wish to run a LAMP (Linu Apache MySQL PHP) server, and will have to manually edit your config files. You might also have to restart or reset the Apache web server or other service daemons. You even need elevated privileges to shutdown or restart the computer. “Hey, who turned this thing off?!” If you're familiar with Windows, it's very similar to the Windows User Account Control dialog box that pops up when you try to do anything important, just not as friendly.
In Windows, if you try to perform an administrative task, a dialog box asks you if you wish to continue (“Are you really sure your want to run that program you just clicked on?”). The task is then performed. On a Mac, a security dialog box pops up and you are required to type in your password and click OK. It's more of a dramatic story in Linux. Things might behave quite strangely without the proper permissions. The important config file you were editing may not save correctly. A program you installed may simply refuse to run. That awesome source code you downloaded and need to compile wont. You might even be lucky enough to given an “Access Denied” or another friendly error message. All your worst fears have come true, but all you needed to do was ask for permission! That's why we want to remember to ask for superuser permissions upfront like this:
Watch what happens in this screenshot if we don’t first elevate our permissions with sudo. First, we use the reboot command to try and reboot the system. The command fails citing: “must be superuser”. We then try with sudo reboot. Sudo asks for your user password. Note that it is asking for your password, not the root password. Finally we see the broadcast message that the system will be rebooted now.
Sudo is like saying the magic word. It might as well be named opensesame or abracadabra or even bippityboppitybacon.
Why is sudo better than the alternatives?
Sudo is the best and safest way to elevate privileges. Let's take a look at another way of doing things. The switch user command “su” will ask you for the root password and give you a super user prompt, signified by the # symbol. That # symbol means “DANGER! YOU'RE LOGGED IN AS ROOT!”. The first command you issue may go well. But your forgetfulness will cause you to stay logged in as root. One bad typo and BAM! You erased the entire hard drive instead of that fake mp3 you downloaded. The cat decides to lay on your nice warm laptop. POOF! Your web server and home business are gone!
With the sudo command, you have to enter in “sudo” before every command. That means you don’t have to remember to switch back to regular user mode, and fewer accidents will happen.
The sudoers file
This file is the seedy underbelly of sudo. It controls who can use the sudo command to gain elevated privileges. It is usually located at /etc/sudoers. The best and safest way to edit this file is by using the visudo command. This command will start the vi editor with elevated privileges so that you can edit the file and save it. It also will put a filelock on the sudoers file so that no one else can edit it.
Once you're done editing it, it will parse the file for simple errors. It's a much safer way of editing the sudo file than just using any old text editor. This file contains many parameters. You can specify which users of which groups can perform what commands. We are simply going to grant ourselves access to sudo by adding the following at the bottom:
username ALL=(ALL) ALL //gives user "username" sudo access%wheel ALL=(ALL) ALL //Gives all users that belong to the wheel group sudo access
Now the specified username will be able to use all root privileges. You can also allow a user or group to have sudo access to only specific services or servers in replace of the ALL parameter, but that's a topic for another day.
How can you do more with sudo?
Like any good command, there are a few nifty options to make sudo do more! sudo –b will run the command in the background. This is useful for commands that display a lot of output as they are running. sudo –s will run the shell specified with elevated privileges, giving you the # prompt (don’t forget to exit!). And sudo su - will make you the root user and load your custom user environment variables.
Using the Linux sudo command
Sudo gives us safe elevated privileges when we want to run important commands. It might be THE most used and powerful command among Ubuntu users, as it has become the preferred method in that distribution. Now that you have the power, be sure to be safe when you issue your commands! There is no su-undo!
If you're ready to take your learning to the next level, check out our Overview of Linux course.