Sudo Command in Linux terminal with example

Sudo Command in Linux terminal with example

In this article we are going to shows how to use sudo command in Linux. If you want to learn about sudo command in Linux with Examples then this post is ideal for you.

In the Linux, sudo(Super User DO) is a program that is used to run programs with the security privileges of another user, by default the superuser. With the help of sudo command, you can perform actions and operations a root user. Sudo command is one of the most and frequently used command for a Linux user

Using sudo program, you can set or grant limited administrative privileges to particular users without knowing them the root password.


Sudo Command in Linux

Simply follow below steps to sudo Command in Linux:

Installing Sudo (if sudo command not found)

At the present time, sudo package is comes pre-installed on most of modern Linux distributions.

To check the sudo package is installed or not on your system, run the command sudo. If sudo is installed on your system, it will display a help message. Otherwise, you will see a message like sudo command not found.

Install Sudo on Ubuntu and Debian

$ apt install sudo

Install Sudo on CentOS and Fedora

$ yum install sudo

Add User to Sudoers File

In most Linux distribution, you can grant sudo access to users just by adding them to sudo group which is defined in the sudoers file.

For RedHat Linux based distributions such as CentOS and Fedora, the name of the sudo group is wheel. Run the command below add the user to wheel group:

$ usermod -aG wheel username

For Debian and Ubuntu, run the command below:

$ usermod -aG sudo username

In the Ubuntu , by default the root user is disabled for the security purpose.


How to Use Sudo Command in Linux

In the Linux, the basic syntax of the sudo command is show as below:

sudo OPTION.. COMMAND

You can check sudo options here.

Basically, sudo command used in its basic form, without any option:

sudo command

Here, command is the command for which you want to sudo.

When you run a command with sudo, it will first check if the user have sudo access or not. At the first time, it will ask to enter user password for executing the command as root.

For an example, if you want to list all files inside the /etc/nginx directory, run the command below:

sudo ls /etc/nginx

Password Timeout

By default you will need to enter your password again after five minutes for sudo. You can change the default timeout by editing the sudoers file. Run the command below to open the file:

sudo visudo

After open the file, find out the Defaults env_reset line in file and set the default timeout as below:

Defaults env_reset, timestamp_timeout=15

add the below line in the file, if you want to change the timestamp only for a specific user:

Defaults:user_name timestamp_timeout=10

Run the specified command as a user other than root

To run a command as a specified user, use the -u option.

For an example we are using sudo to run the whoami command as a user raj:

sudo -u raj whoami
Output
raj

That’s all.

If you face any error and issue in above steps , please use comment box below to report.

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

1 thought on “Sudo Command in Linux terminal with example”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top