sudo or superuser do is a utility used on Linux, BSD and Unix systems that provides the running command with root or Administrator privileges. Not every user needs to have root privileges but in some cases needs to run commands with root privileges. We can use sudo to run some specific or all provided commands and edit files with the root privileges.
sudo Configuration File sudoers
sudo command is configuration is stored
/etc/sudoers file. This file can be only viewed and edit with root privileges. We can edit this just with
visudo command like below.
$ sudo visudo
We can see that there is a different part of sudoers configıration file where default values, user and group privileges are provided.
We can use the following syntax in order to create a sudo configuration about the given user or group.
USER HOSTLIST=(USERLIST) COMMANDLIST
- USER is the user who can run the given command on the given host
- HOSTLIST is the hosts where given commands can be run
- COMMANDLIST is the commands can be run. Multiple commands can be delimited with the comma.
Specify A User To Run Sudo Command
We can add a user to run a specific command with root privileges. In this example, we will add user
ismail to run command
visudo with root privileges.
ismail ALL=(ALL:ALL) /usr/sbin/visudo
Passwordless Sudo Command
We can provide sudo command to the given user without any password. We will use
NOPASSWD configuration like below which will disable password for the user
List Available Commands
We can list available commands with the
-l option which will list currently used root privileged commands the current user can execute.
$ sudo -l
Run Command with Root Privileges
In this example, we will run commands with root privileges by using
sudo command. We will run
ls command in the
/root directory which is completely owned by root and only used by root user.
$ sudo ls /root
Edit File with Root Privileges
We also edit files with the root privileges by using sudo command. We will edit the
/etc/passwd file with the
vim command like below.
$ sudo vim /etc/passwd
Difference Between sudo and su
su command is used to login as root user on the other side
sudo command just runs given command as root in the current user sessions. While running sudo the user name will be also logged but with the sudo command just the root user name will be logged. The best way to