FUNDAMENTALS A Complete Guide for Beginners
Sudo in Linux allows a user to run a command with elevated privileges. For instance, a hotel has different keys for different rooms. If you are a guest, you will only have the key to your room. But what if you want to access a restricted area? You need to get the key from the hotel manager as he has access to all the rooms. And by doing so, you are giving yourself some privileges of the hotel manager. This is what sudo does. It allows regular users to run certain commands and programs with the privileges of a “root” user.
In this article, I will explain what is sudo in Linux with 4 practical examples.
What is “sudo” in Linux?
sudo stands for superuser do which enables users with the power to run some commands that only the superusers or root users are allowed to run. It temporarily elevates privileges to do sensitive tasks without logging in as the root user such as installing software, editing system configuration files, etc.
Linux is a multi-user system where different users enjoy different privileges for security and administrative reasons. If everyone had all the privileges, the system would be at risk. For instance, a user could have deleted an important system file or installed harmful programs. However, prefixing with “sudo” allows the user proper permission to execute the command having administrative-like powers of the root user or superuser temporarily.
By default, sudo necessitates the users to authenticate themselves with a password that is typically the user password.
Linux “sudo” Command Syntax
The sudo command in Linux is used to grant root privileges. Its syntax goes like the following:
sudo [OPTION]...[COMMAND]...
Linux “sudo” Command Options
The sudo command in Linux provides a lot of options. You can check them by using the following command:
man sudo
Below I have listed some useful options to use with the sudo command:
Options |
Description |
-u | Is used to execute commands with a different user. |
-l, – -list | shows the permissions and privileges of a user. |
-n, – -non-interactive | doesn’t prompt for a user password. |
-b, – -background | runs commands in the background. |
-r, – -role | runs commands with SELinux security context. |
-e | edits one or multiple files instead of executing commands. |
-k | kills the timestamp of the user. |
Note: All options are case-sensitive.
4 Examples of “sudo” command in Linux
Here, I will show you 4 examples of the sudo command in Linux. These examples will help you understand the concept better.
Example 1: Update the system using “sudo”
In this example, I will update the system. As updating a system is a sensitive task, only root user has this privilege. I will use sudo to grant that access. Now to update your system using sudo, do the following:
- At first, launch an Ubuntu Terminal.
- Insert the following command in the command prompt and hit ENTER:
sudo apt-get update
- Provide the password to access root privileges and press ENTER .
The “apt-get update” command updates the local package database with information of latest software packages and it won’t work without proper privileges and permissions. In the output, I have used sudo to take advantage of root privileges. And as you can see, the system is updating successfully.
Example 2: Execute a Command as a Different User with “sudo”
Now I will execute a command with the privileges of a different user instead of current user. For instance, my current user name is “walid”. However, my system has another user named “jim” and I want to execute a command as “jim”. I can do that by using the “-u” option of command sudo. Now to execute a command as a different user, follow the steps below:
- Launch a Terminal in Ubuntu first.
- Write the following command in the Terminal and press ENTER:
sudo -u jim ls /var
- Provide password of the current user and hit the ENTER key.
Here I have listed the contents of the “/var” directory. You can do anything you want but make sure that the user has proper permissions.
Example 3: List the Permissions and Privileges with “sudo”
If you want to check whether you have root privileges or not, you can use the “-l” option of the sudo command. To list the permissions and privileges of a user, do the following:
- Press CTRL + ALT + T, to open an Ubuntu Terminal.
- Copy the following command in the Terminal and hit ENTER:
sudo -l
- Give your password and press ENTER from the keyboard.
As you can see, in my case, the user “walid” is also the root user. In the output, “(ALL : ALL) ALL” denotes that user “walid” have permissions to execute all commands.
Example 4: Run Command without Password Using “sudo”
When you use “sudo”, you are prompted for a password. However, the “-n” option provides to run commands without a password. You only need to enter the password once. Now to run a command without password, run the following command in your terminal:
sudo -n apt-get update
In Example 1, I have updated the system as well. If you can remember, it was asking for a password back then. But in this case, it isn’t prompting for any password even though I am using sudo.
Conclusion
Learning “what is sudo in Linux” is very important for any type of user. In this article, I have tried to provide a comprehensive view of sudo. Hopefully, you have got everything you need.
Frequently Asked Questions
How to use the Linux “sudo” command?
To use the Linux sudo command is very easy. Type the command you intend to run with root privileges and prefix it with the command “sudo”. For instance, to update the system using “apt”, type:
sudo apt update
Upon pressing the ENTER button, you will be prompted to enter the user password to finally update with superuser privileges.
What is sudo access?
Sudo access is a feature in Unix-based Operating systems such as Linux that allows users to run commands and programs with the privileges of another user, typically the administrator or the “root” user. Availing of sudo access means that the users can perform tasks that they are normally not allowed to do including installing software, editing system configuration files, managing users or groups, etc.
How do I enable and disable sudo access in Linux?
You can enable or disable sudo access in Linux by adding or removing the specific user name from the ” sudoers “ file. The file is typically located at “/etc/sudoers”. You can edit the sudoers file to add or omit the user’s sudo access. For example, adding the line “username ALL=(ALL:ALL) ALL” gives the username complete access to the sudo command.
Can I switch to the root user with the “sudo” command?
Yes, you can. Type the following command in the terminal and press the ENTER button:
sudo bash
This command effectively switches your command prompt to the bash shell as a root user. Your command shell will change to the following:
root@hostname:/home/[username]#
For instance, in the case of the username prem, the shell becomes: root@hostname:/home/prem#
Can I run multiple commands in Linux in a single line with superuser privileges?
Yes. The sudo command lets you run multiple commands in one line with root privileges. To do so, type the string of commands in the terminal separated by semicolons, and prefix them with sudo. For example, to run the commands ls
, whoami
, and hostname
with sudo, type the following command and press ENTER:
sudo ls; whoami; hostname
What is the difference between “su” and “sudo” command in Linux?
In Linux, the su (switch user) command lets the user log in as another user (often the root user) to access their files and system settings. Contrarily, the sudo command stands for “superuser do” and allows the user to execute certain commands with privileges that only the root users have in general. It helps users to do tasks with administrative power without the need to log in exclusively as the root user.
Similar Readings