The sudo command in Linux stands for “superuser do“, which allows an approved user to run a command as the superuser or another user, as specified by the security policy.
This is especially useful for performing tasks that require administrative privileges without logging in as the root user.
Setting Up sudo User in Linux
Before using sudo, you need to ensure it is set up correctly. Typically, sudo is pre-installed on most Linux distributions. If it’s not installed, you can install it using your package manager.
sudo apt install sudo [On Debian, Ubuntu and Mint] sudo yum install sudo [On RHEL/CentOS/Fedora and Rocky/AlmaLinux] sudo emerge -a sys-apps/sudo [On Gentoo Linux] sudo apk add sudo [On Alpine Linux] sudo pacman -S sudo [On Arch Linux] sudo zypper install sudo [On OpenSUSE] sudo pkg install sudo [On FreeBSD]
To allow a normal regular existing user to use sudo, you must add them to the sudo group.
sudo usermod -aG sudo username [On Debian systems] sudo usermod -aG wheel username [On RedHat systems]
Alternatively, you can create a new sudo user by using the adduser or useradd command.
Make sure to replace new_username with the actual username you want to grant sudo privileges to.
sudo adduser new_username OR sudo useradd new_username sudo passwd new_username
Once created, add the new user to the sudo group.
sudo usermod -aG sudo username [On Debian systems] sudo usermod -aG wheel username [On RedHat systems]
Switch to the new user and check if they have sudo access.
su - new_username sudo whoami
How to Use sudo in Linux
Once a user is added to the sudo group, they can use the sudo command to perform administrative tasks.
Basic sudo Usage
To use sudo, simply prepend it to the command you want to run with superuser privileges.
sudo apt update
When you run this command, you’ll be prompted to enter your user password. After entering the password, the command will execute with elevated privileges.
Running a Command as Another User
You can also use sudo to run a command as another user using the -u
option followed by the username.
For example, to list files as the user ravi:
sudo -u ravi ls -l /home/ravi
Editing Files with sudo
To edit a system file with a text editor, you often need sudo privileges.
sudo nano /etc/hosts
Advanced sudo Configuration
The sudo command is highly configurable. You can customize its behavior by editing the /etc/sudoers
file. It’s crucial to edit this file correctly to avoid configuration issues.
To edit /etc/sudoers file, always use the visudo command.
sudo visudo
Granting Specific Permissions
You can grant specific permissions to users or groups in the /etc/sudoers file. For example, to allow the user ravi to restart the Apache service without a password prompt, add the following line.
ravi ALL=(ALL) NOPASSWD: /usr/sbin/service apache2 restart
Restrict Access to Commands for Multiple Users
User aliases allow you to specify a list of users who share a common set of privileges, which is particularly useful when you want to grant the same level of access to multiple users.
For example, if you have a group of developers who need access to certain administrative commands, you can create a user alias for them.
User_Alias DEVELOPERS = user1, user2, user3
With this alias defined, you can then grant sudo privileges to all users in the DEVELOPERS alias.
DEVELOPERS ALL=(ALL) /usr/bin/apt
This line allows all users in the DEVELOPERS alias to run the apt command with sudo privileges.
Conclusion
The sudo command is an essential tool for managing a Linux system. It provides a secure way to perform administrative tasks without logging in as the root user.