How to Use Sudo Command in Linux (with Examples)
Ever tried to execute a command on your Linux system and received a “Permission Denied” error? The simplest solution to counter this error is using the “sudo” command. In this article, we will delve deep into the world of sudo and explore its functionality to overcome the notorious “Permission Denied” error in Linux. We will learn how to use the sudo command in Linux along with some examples here.
User Privileges in Linux
Before we proceed further, it’s important to learn about user privileges in Linux. The way Linux security works is by limiting the privileges among users. There are three types of users in Linux:
If you need to learn more about permissions in Linux, check out our extensive guide onLinux file permissions.
What is Sudo Command in Linux
Now that you know that the root user holds the complete administrative rights of the Linux system, you may be wondering if you can grant administrative rights to all users. Well, this could lead to a complete disaster leading to various security risks in the system.
This is where the sudo command in Linux comes into play. sudostands for “Superuser do”and allows system administrators to grant specific permissions to various system users, allowing them to run only specific administrative tasks. Thus, it offers a much more precise and controlled approach to user privilege management.
How to use the sudo Command in Linux
With the functions of sudo out of the way, let us now see how to use the sudo command in Linux. Managing user privileges with the sudo command is pretty straightforward:
1. Grant Sudo Access to Users
Users’ system privileges are stored in the file ‘/etc/sudoers’. This file is a configuration file containing a set of rules that defines each user or group that is allowed to run commands with sudo access. Follow these steps to grant sudo access to a user:
Here’s what the above syntax means:
For example, if you want to add the user parrot to the sudoers group and make it a part of all users and groups (that too with access to all commands), the command will look like this:
Save the changes made to the sudoers files and exit the editor.Note: Be extra careful of who you give sudo access to and grant access to only those users who need administrative rights.
2. Sudo Command: Syntax and Options
Once you have granted sudo access to the users, they can now runLinux Terminal commandsas a sudo user. The syntax to use the sudo command in Linux is:
Some of the popular options to pair with the sudo command are:OptionDescription-bRuns the in the background-g groupRuns the command with the specified group as the primary group-nAvoid prompting the user for input of any kind-p promptUse a custom password prompt with optional escape sequences-uRuns the command as a user other than the default user (usually root)
Sudo Command in Linux: Examples
Now that you are familiar with the basic syntax, let us see some examples of how to use the sudo command for effective Linux security.
Run a Command as a Root user
To run a command as a root user, simply add sudo before the command:
For example, to run the whoami command as the root user:
Execute a Command As a Different User
To run a command as a different user, use the -u flag with sudo:
For example, to run the whoami command as the “user” user, use this command:
Execute a Program in the Background
Running a command in the background means the command will give no output while it finishes executing in the background. To execute a program in the background, use this syntax:
For example to open an instance of Vim, use the sudo command as:
Basic Sudo Command Troubleshooting
With great power, comes great responsibility. The sudo command can help to improve your Linux workflow immensely. But, sometimes you may face some problems while using the sudo command. Here are some troubleshooting tips for the common Sudo command issues you can face:
-
Syntax error: This is the most common Sudo problem faced by users. Alwaysuse the “visudo” commandto edit the sudoers files to prevent any syntax that may creep in.
-
Incorrect permissions: Always double-check that the sudoers file is owned by the root and that the permissions for read and write are set only for the root.
-
Lockouts: Always keep a root shell ready in case of system lockouts that occur when you modify the sudoers file in the wrong way.
Sudo Command Best Practices
Some of the recommended best practices to follow with the sudo command in Linux:
-
Regularly review and update the sudoers file: Regularly review the sudoers file to ensure permissions are granted only to trusted users. Delete outdated entries or unknown entries to minimize security threats.
-
Use strong passwords: Always use a strong password with a combination of lowercase, uppercase, numbers, and special characters. Also, avoid using common passwords like your username or date of birth.
-
Monitor sudo logs: Regularly monitor sudo activity via the sudo logs and report any malicious or unknown activities to appropriate authorities.
-
Keep the system updated and backed up: Regularly back up the latest updated system. Periodic updates can help fix some critical bugs and security patches.
Beebom Staff
Bringing the latest in technology, gaming, and entertainment is our superhero team of staff writers. They have a keen eye for latest stories, happenings, and even memes for tech enthusiasts.
Add new comment
Name
Email ID
Δ
01
02
03
04
05