What is the sudo Command?
“Make a sandwich.” No. “Sudo make a sandwich.” Sure. This is a recurring joke in the programming community. This article will help you understand the sudo joke.
Sudo is a command that runs any Linux command with elevated privileges. Sudo is used when your user account does not have the necessary permissions to execute a command.
In this guide, we’re going to talk about what sudo is, why it is used, and when you should use it. We’ll walk through an example of the command so you can see how it works.
What is the Sudo Command?
Sudo runs a command with elevated privileges. It originally stood for
superuser do because the command allows you to run another command as if you are the superuser.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
The superuser on a Linux computer has special privileges. They can access files like /root which may be controlled by permissions.
There are a range of commands that you cannot run on your computer without sudo. You cannot shutdown or restart a computer using the
reboot commands without sudo. You need elevated privileges. You cannot modify certain system files without sudo.
Why Should I Use Sudo?
“Access denied” messages may be annoying but they serve an important purpose. These messages help prevent you from making changes without first considering what you are doing.
The sudo command acts as a sort of protection mechanism. It ensures that you do not execute any commands that may have a significant impact on your system without giving it a second thought. It may not seem natural to ask a computer for permission but it’s what you’ll need to get used to when using the Linux command line.
Sudo is somewhat similar to Windows’ User Account Control boxes, or macOS’ security box. These boxes appear when you install new software or when a piece of software needs to access a particular file. They ensure that you know that certain privileged files on your system may be accessed or modified.
Sudo in Action
Let’s try to edit the file /etc/hosts. This file translates hostnames or domain names into IP addresses. This file is critical for Linux networking which means that it is protected.
Let’s try to modify the file using
127.0.0.1 localhost 255.255.255.255 broadcasthost 192.168.1.124 pis
I have just added
s to the end of
pi in the file. When I go to save the file, I get this message:
[ Error writing /etc/hosts: Permission denied ]
Because the /etc/hosts file is protected, my standard user account cannot change it. That’s where the sudo command comes in. If I want to modify this file, I need to use sudo:
sudo nano /etc/hosts
You may be prompted to enter your password if you use sudo. This will happen the first time you run sudo in each session. You may be asked to enter your password again later.
Once you enter your password, the above command will open up the /etc/hosts file in the nano editor. You will be allowed to make any change to the file because you are accessing it as if you were the superuser.
Sudo is a way of telling your computer to do something. It should be used with caution. Only use sudo when you are absolutely sure you want to run a command. Many commands are irreversible and sudo acts as a sort of protection mechanism against yourself.
Switching Users to Sudo Using su
You can switch users to the sudo user using su:
su is short for
substitute user. When you do not specify a username,
su will log you in as the sudo user. Before this command changes your user, you will need to enter your password.
This command will open up a shell where you are the sudo user. Most Linux users prefer to use
sudo over changing users because it is safer. When you are logged in as the superuser, you will not see any “Access denied” messages. There are no safeguards.
You’re best to stick with
sudo unless you know what you are doing.
The /etc/sudoers File
The sudo command works by referencing a file called /etc/sudoers. This file contains a list of the users who can access the sudo command. You can modify this command in your default text editor or using the visudo command:
You should be careful when you modify this file. If your sudoers configuration is not correct, it may cause other users to gain sudo access who should not have such access. A broken configuration may cause other problems on your system.
The sudo command runs any Linux command as the superuser. It is used to run commands which require special privileges to execute. For instance, you need sudo access to modify a system file.
You can change your user to the superuser in a shell using the
su command. You should be careful when using this command because it will remove all the “Access denied” prompts when you run commands until you exit the shell.
Now you’re ready to use the sudo command like a Linux expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.