Using sudo command you can perform the actions and operations as root user. It’s the mostly used command for Linux user. It’s always recommended to login as a sudo user instead of as root and perform task. You can give limited administrative privileges to sudo users without providing them root password.
This guide will explains how to use the
Install Sudo (sudo command not found)
Now a days, with the modern Linux distributions, sudo package comes pre-installed. You can check either sudo package is installed or not by typing
sudo in terminal. If sudo is not installed it will show
sudo command not found otherwise it will show a help message.
You can install sudo easily using package manager if the sudo package is not installed.
Install Sudo on Ubuntu and Debian
apt install sudo
Install Sudo on CentOS and Fedora
yum install sudo
Add User to Sudoers
In latest Linux distributions you can give sudo access to user just by adding them to sudo group which defined in the sudoers file. Each member of this group can perform any command as root.
The sudo group name will be different as per distributions. For RedHat based distros like CentOS and Fedora will have the name of the sudo group is wheel. Run below command to add the user to wheel group:
usermod -aG wheel username
For Debian and Ubuntu type following to grant with
usermod -aG sudo username
Here, don’t forget to replace username with your actual name.
If you have a Ubuntu system, you notice that by default root user is disabled for the security purpose. Ubuntu creates a initial user with the member of sudo group so can have sudo privileges.
You can also give limited access to users instead of adding them to sudo group. If you would like to give mkdir command as sudo to user
tecnstuff then just type:
visudo command opens the
/etc/sudoers file with text editor. Append below line to file:
Save the file and quit.
How to Use Sudo
The basic syntax for
sudo command is as below:
sudo OPTION.. COMMAND
It has different options for different purpose but mostly sudo command is used in basic form, without any options:
You just have to prefix the command with sudo to use sudo. Here, command is the command for which you want to sudo.
Sudo will read the
/etc/sudoers file and check if the user have
sudo access or not. At the first time, it will ask to enter user password and on success command will be execute as a root.
For example, to list all files inside the
/etc/nginx directory you would use:
sudo ls /etc/nginx
[sudo] password for tecnstuff: conf.d koi-win nginx.conf sites-enabled fastcgi.conf mime.types proxy_params snippets fastcgi_params modules-available scgi_params uwsgi_params koi-utf modules-enabled sites-available win-utf
Within a certain time, you don’t need to enter the password again to use another sudo. By default, the timeout is about 5 minutes for sudo. You can change the default timing by editing
sudoers file. Run below command to open file:
Find out the Defaults env_reset line in file and change it to as below:
Defaults env_reset, timestamp_timeout=15
Run a Command as a Other User
The sudo command is not only use to perform as root but you use it to run any command as another user also.
-u will allows you to run a command as a specified user.
In the below example we are using
sudo to run the
whoami command as a use
sudo -u john whoami
It will returns the current user name
In this guide, you learned how to use the sudo command and grant sudo privileges to users. If you have any query or suggestion, please comment below.