The 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 own 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. In this article, I will explain sudo with some practical examples.
What is Sudo in Linux
Linux is a multi-user system where different users enjoy different privileges for security and administrative reasons. If everyone had all privileges, the system would be at risk. For instance, a user could have deleted an important system file or installed harmful programs. However, there is a user who has access to everything and it is called the root user (or superuser). By using sudo, a regular user can grant the privileges of the root user or superuser temporarily. This is the reason the word “sudo” is the short form of “superuser do”.
Sudo vs Su
Here I will list some of the differences between sudo and su.
A. Sudo
- It allows a user to perform a task that requires special privileges while being in the user’s own account.
- Provides temporary
- Limits
- Requires password or other forms of authentication.
- Less chance to harm the system.
B. Su
- It is used to switch to the root user or another user.
- Provides full
- Doesn’t limit
- Only requires the password of the target account.
- More chance to harm the system.
Basic of the “sudo” Command
The sudo command in Linux is used to grant root privileges. Its syntax goes like the following:
sudo [OPTION]...[COMMAND]...
Options
The sudo command in Linux provides a lot of options. You can check them by using the following command:
man sudo
Useful options
I have listed some useful options here
- -u, is used to execute commands as a different user.
- -l, –list, shows permissions and privileges of a user.
- -n, –non-interactive, doesn’t prompt for 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.
Examples of “sudo” command in Linux
Here, I will show you a few examples of 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:
Steps to Follow:
❶ 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.
❹ Press ENTER from your keyboard.
Output:
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
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:
Steps to Follow:
❶ 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.
❹ Now find the ENTER key from your keyboard and hit it.
Output:
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
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:
Steps to Follow:
❶ Press CTRL + ALT + T, to open an Ubuntu Terminal.
❷ Copy the following command in the Terminal and hit ENTER:
sudo -l
❸ Give your password.
❹ Now press ENTER from the keyboard.
Output:
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
When you use “sudo”, you are prompted for a password. However, the “-n” option gives a way to run commands without a password. You only need to put the password once. Now to run a command without password, do the following:
Steps to Follow:
❶ Open a Terminal in Ubuntu.
❷ Type the command below in the Terminal:
sudo -n apt-get update
❸ Hit the ENTER button.
Output:
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.
Similar Readings