FUNDAMENTALS A Complete Guide for Beginners
Sudo can be referred to as the supreme command. It is the abbreviation for “Super User DO”. It allows a user to act as a superuser and run commands accordingly. One can run specific commands prefixed by sudo with boosted rights. In this article, you will learn all the features and aspects of sudo command in Linux.
A. Description
sudo allows a permitted user to execute a command as the superuser or another user, as specified by the security policy. It is considered analogous to the “run as administrator” process of Windows.
B. Syntax
From the man page, you can get a brief overview of the sudo command. The syntax for the sudo command is pretty simple which is the command itself followed by some specific options and then the command you want to run as a superuser.
sudo [OPTION]...[COMMAND]...
C. Options
You can see the manpage of the sudo displays on the screen. In the synopsis, you can see the options of this sudo command that can be executed. I will describe some useful options for your better understanding.
Useful Options
- -D directory, –chdir=directory (executes the command in the specific directory)
- -e (edits one or multiple files instead of executing commands)
- -h (Displays a short help message and exits)
- -l (runs specific commands as the root user)
- -k (kills the user’s timestamps)
- -u user, –user=user (executes the command as a user other than the specific default user. )
- -v (Prints the sudo version string as well as the version string of any configured plugins)
Installing sudo Command in Linux
Most of the Linux distributions have the sudo package preinstalled. In my machine, it was preinstalled also. To check whether you have it preinstalled or you just type the below command on the command prompt:
sudo
It will look like the picture below if you have it already on your machine. You will see a list of short help.If don’t have sudo already installed on your machine, you can follow the below instructions.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
sudo apt install
➌ Type your password.
➍ Now, press the ENTER button.
After completing the entire process, sudo will be installed on your machine.
Practical Examples of Sudo Command in Linux
The sudo in Linux is an easy-to-use command which gives you permission to execute a command as the superuser. Some of the most useful applications of the sudo command have been illustrated below:
Example 1: Get an update using sudo
Being a general user you cannot get any updates. To update any packages you have to follow the below procedure.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
sudo apt-get update
➌ Now, press the ENTER button.
Output >
You can see in the picture that the package update is installed.
Similar Readings
- The “time” Command in Linux [4 Practical Examples]
- The “tty” Command in Linux [4 Practical Examples]
- The “uptime” Command in Linux [5 Practical Examples]
- The “vmstat” Command in Linux [6 Practical Examples]
- The “uname” Command in Linux [11 Practical Examples]
- The “apt-get” Command in Linux [10 Practical Examples]
Example 2: Add User to sudo Group
You can add any user to sudo group. Here I am adding a user named abir to the sudo group. To add abir to the sudo group you should follow some steps. Those are described below:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
sudo usermod -aG sudo abir
Replace abir with the user you want to add.
➌ Type your password.
➍ Now, press the ENTER button.
➎ To check whether the user is added to the group, type the following command:
groups abir
➏ Again, press the ENTER button.
Output >
You can see that in the picture abir is added to the sudo user group.
Example 3: Use sudo to Run a Command as the Root User
There are some commands that you can not run as a general user. Here, I want to list my root directory but the permission is denied. To run that command as the root user you have to follow the below procedure:
Steps to Follow >
➊ Open the Ubuntu Terminal first.
➋ Type the following command in the command prompt:
ls /root
Permission is denied for not being the root user.
➌ Type the same command using sudo
sudo ls /root
➌ Now, press the ENTER button.
Output >
You can see in the picture that the snap directory is listed here inside the root.
Similar Readings
- The “free” Command in Linux [8 Practical Examples]
- The “getent” Command in Linux [11 Practical Examples]
- The “groupadd” Command in Linux [7 Practical Examples]
- The “addgroup” Command in Linux [7 Practical Examples]
- The “groups” Command in Linux [6 Practical Examples]
- The “firewall-cmd” Command in Linux [7 Practical Examples]
Example 4: Switch to Root User using sudo
You can change your command prompt by switching to the root user. To do this you need to follow below procedures:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
sudo bash
➌ Now, press the ENTER button.
Output >
You can see that my command prompt has changed to the root user.
Example 5: Use sudo to Run a Command as a Different User
You can run a command as a different user other than the root user. Here, I have used my own user to run a command as the root user. To do this you need to follow below procedures:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
whoami
borhan is the user here.
➌ Again type the following command in the command prompt:
sudo -u root whoami
➍ Type your PASSWORD.
➎ Now, press the ENTER button.
Output >
You can see that the output of the command has changed from borhan to root.
Similar Readings
- The “groupmod” Command in Linux [5+ Practical Examples]
- The “id” Command in Linux [7+ Practical Examples]
- The “service” Command in Linux [6 Practical Examples]
- The “sestatus” Command in Linux [4 Practical Examples]
- The “shutdown” Command in Linux [7 Practical Examples]
- The “finger” Command in Linux [6 Practical Examples]
Example 6: List User Privileges with sudo Command
You can list your user privileges with the sudo command. To do this you need to follow below procedures:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
sudo –list
➌ Now, press the ENTER button.
Output >
You can see that my user privileges are shown in the output.
Example 7: Run Multiple Commands in One Line
sudo command can be used to run multiple commands in a single command line. I have used 3 commands together in one line. To do this you need to follow below procedures:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
sudo ls; whoami; date
➌ Type your PASSWORD.
➍ Now, press the ENTER button.
Output >
You can see that the output lists of files and directories are shown first, then the user id and the execution of the date command at the end.
Similar Readings
- The “dmesg” Command in Linux [7+ Practical Examples]
- The “install” Command in Linux [6+ Practical Examples]
- The “reboot” Command in Linux [3 Practical Examples]
- The “sync” Command in Linux [8 Practical Examples]
- The “chage” Command in Linux [7 Practical Examples]
- The “env ” Command in Linux [9 Practical Examples]
Example 8: Change Password Timeout of sudo Command
The default timeout limit for sudo users in the Ubuntu terminal is set to 15 minutes. After that, you will have to give the password again to execute any command as a superuser. You can change that password timeout limit with the sudo command. Here, I will set my password timeout to 45 minutes. To do this you need to follow below procedures:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
sudo visudo
➌ Press the ENTER button.
A nano text editor file will open in your terminal.
➍ Go to the Defaults env_reset line
➎ Change the line to the following line:
Env_reset, timestamp_timeout=45
➏ Now, press CTRL+X and Y in the next stage to save the timestamp.
➐ Finally, press the ENTER button.
Output >
You will not anything in the display but your timeout for the password will be changed accordingly.
Conclusion
After completing this article, you will acquire all the knowledge about the sudo command in Linux. With the given practical examples, you will be able to execute a lot of commands as a power user.
Similar Readings
- The “passwd” Command in Linux [7 Practical Examples]
- The “df” Command in Linux [11 Practical Examples]
- The “apt” Command in Linux [13+ Practical Examples]
- The “top” Command in Linux [8 Practical Examples]
- The “htop” Command in Linux [7 Practical Examples]
- The “enable” Command in Linux [6 Practical Examples]