How to use sudo and su Commands in Linux

How to use sudo and su Commands in Linux

In this article we are going to shows how to use sudo and su commands in Linux. If you want to learn about sudo and su commands 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 as 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.

In other hand, su (stands for substitute user or switch user) is a command line utility that allows you to run commands with another user’s privileges. With help of su is the simplest and easiest way to switch or change to the administrative account in the current logged in session.


How to use sudo and su Commands in Linux


Sudo Command

Simply follow below steps to use 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

Su Command in Linux

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

su [OPTIONS] [USER [ARGUMENT...]]

When you run the su command without any option, it will run as an interactive shell as root:

su

When you run the above command, it will be prompted you to enter the root password. After successfully authentication, the su command allow you to switch to the root account.


Common su Command Options

To display help window for the su command, use -h option with the su command:

su –h

To run a specific Command as substitute user without starting an interactive shell, use the -c--command option with the su command:

su -c ps

The above command run ps command as root.

You can switch to another user on the system by using username as an argument with su command as show below:

su studiesonline

To use another shell instead of the default in the passwd file, use the -s--shell option:

su –s /usr/bin/zsh

To preserve(keep) the entire environment (HOMESHELLUSER, and LOGNAME) of the current user account, use the -p--preserve-environment option:

su -p

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.

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