Using sudo instead of login in as root is more secure because you can grant limited administrative privileges to individual users without them knowing the root password.
In this tutorial, we will explain how to use the sudo command.
Table of Contents
Installing Sudo (sudo command not found)
The sudo package is pre-installed on most Linux distributions.
To check whether the sudo package is installed on your system, open up your console, type
sudo, and press
Enter. If you have sudo installed the system will display a short help message, otherwise you will see something like
sudo command not found.
If sudo is not installed you can easily install it using the package manager of your distro.
Install Sudo on Ubuntu and Debian
apt install sudo
Install Sudo on CentOS and Fedora
yum install sudo
Adding User to Sudoers
By default on most Linux distributions granting
sudoers file. Members of this group will be able to run any command as root. The name of the group may differ from distribution to distribution.
On RedHat based distributions such as CentOS and Fedora, the name of the sudo group is
wheel. To add the user to the group run:
usermod -aG wheel username
On Debian, Ubuntu and their derivatives, members of the group
sudo are granted with sudo access:
usermod -aG sudo username
The root user account in Ubuntu is disabled by default for security reasons and users are encouraged to perform system administrative
If you want to allow a
specific user to run only certain programs as sudo, instead of adding
the user to the sudo group add the users to the
For example to allow the user
learncybers to run only the
mkdir command as
and append the following line:
On most systems, the
visudo command opens the
/etc/sudoers file with the vim text editor. If you don’t have experience with vim check our article about how to save a file and quit the vim editor.
You can also allow users to run sudo commands without entering password:
learncybers ALL=(ALL) NOPASSWD: ALL
How to Use Sudo
The syntax for the
sudo command is as follows:
sudo OPTION.. COMMAND
sudo command has many options that control its behavior but usually
sudo is used in its most basic form, without any option.
To use sudo, simply prefix the command with
command is the command for which you want to use sudo.
Sudo will read the
file and check whether the invoking user is granted with sudo assess.
The first time you use sudo in a session, you will be prompted to enter
the user password and the command will be executed as root.
For example, to list all files in the
/root directory you would use:
sudo ls /root
[sudo] password for learncybers: . .. .bashrc .cache .config .local .profile
default, sudo will ask you to enter your password again after five
minutes of sudo inactivity. You can change the default timeout by
sudoers file. Open the file with
Set the default timeout by adding the line below, where
10 is the timeout specified in minutes:
If you want to change the timestamp only for a specific user add the following line, where user_name is the user in question.
Run a Command as a User Other than Root
There is a wrong perception that
sudo is used only to provide root permissions to a regular user. Actually, you can use
sudo to run a command as any user.
-u option allows you to run a command as a specified user.
In the following example we are using
sudo to run the
whoami command as a user “learn”:
sudo -u learn whoami
whoami command will print the name of the user running the command:
How to Redirect with Sudo
If you try to redirect the output of a command to a file that your user has no write permissions, you will get “Permission denied” error.
sudo echo "test" > /root/file.txt
bash: /root/file.txt: Permission denied
This happens because the redirection “
of the output is performed under the user you are logged in, not the
user specified by sudo. The redirection happens before the
sudo command is invoked.
One solution is to invoke a new shell as root by using
sudo sh -c:
sudo sh -c 'echo "test" > /root/file.txt'
Another option is to pipe the output as a regular user to the
tee command as shown below:
echo "test" | sudo tee /root/file.txt
You have learned how to use the sudo command and how to create new users with sudo privileges.
If you have any questions, feel free to leave a comment.