USD ($)
$
United States Dollar
Euro Member Countries
India Rupee

Sudo and Privilege Escalation

Lesson 16/49 | Study Time: 20 Min

In Linux, many administrative tasks require elevated privileges beyond those of normal user accounts. Instead of logging in as the root user, which can be risky and insecure, the sudo command provides a controlled and secure way for users to temporarily escalate their privileges to perform specific commands.

This method enhances system security by enabling fine-grained access control and auditing, making sudo an essential tool in modern Linux administration.

What is sudo?

sudo stands for "superuser do." It allows permitted users to execute commands with the privileges of another user, typically the root user. Unlike logging in as root, sudo requests the user’s password and logs the command execution for security and accountability. Configurations define who can run sudo and which commands are permissible, typically managed through the /etc/sudoers file.


Basic Usage of sudo

Syntax:

text
sudo [command]


Example:

text
sudo apt update


This runs the apt update command with root privileges, prompting the user for their password.

After entering the password once, sudo grants a timestamped token valid for a default period (usually 15 minutes) during which no password is needed for further sudo commands.

Running Commands as Another User

Use the -u option to run a command as a user other than root.

text
sudo -u username command


Example:

text
sudo -u alice whoami

Executes whoami as user alice.

Using sudo to Open a Root Shell


  • To start a shell session with root privileges:
text
sudo -s


  • Alternatively, to simulate full login:
text
sudo -i


These commands allow performing multiple root commands without prefixing each with sudo.

Advanced sudo Features and Options

Logging and auditing of all sudo commands enhance security tracking.

Security and Best Practices


1. Only assign sudo privileges to trusted users.

2. Configure /etc/sudoers carefully, preferably using visudo to avoid syntax errors.

3. Limit the scope of sudo permissions to needed commands using the sudoers file.

4. Review sudo logs regularly (/var/log/auth.log or equivalent).

5. Avoid sharing passwords; use individual user accounts with sudo privileges.

6. Prefer sudo over root login for accountability and reduced risk exposure.

Samuel Wilson

Samuel Wilson

Product Designer
Profile

Class Sessions

1- What is Linux and Operating System Concepts 2- Linux History and Evolution 3- Linux Distributions and Their Purposes 4- Open Source Software and Licensing 5- Graphical User Interface (GUI) and Desktop Environments 6- Terminal Access and Command-Line Fundamentals 7- Getting Help and Command Documentation 8- File System Hierarchy and Directory Structure 9- Navigating Directories and Listing Contents 10- Creating, Copying, and Moving Files and Directories 11- Deleting Files and Directories 12- Symbolic and Hard Links 13- Understanding File Permissions Model 14- Modifying Permissions and Ownership 15- User and Group Management 16- Sudo and Privilege Escalation 17- Text Searching and Pattern Matching 18- Text Processing and Stream Editing 19- Compressing and Archiving Files 20- Text Editing and File Creation 21- Package Management Systems Overview 22- Installing and Updating Software with APT 23- Installing and Updating Software with YUM/DNF 24- Managing Software from Non-Repository Sources 25- Understanding Processes and Process Management 26- Viewing Running Processes 27- Process Control and Termination 28- Task Scheduling with Cron 29- Networking Concepts and IP Addressing 30- Viewing and Configuring Network Interfaces 31- Basic Network Troubleshooting 32- Shell Script Basics 33- Variables and Data Types 34- Conditional Logic in Scripts 35- Loops and Iteration 36- Functions and Code Reuse 37- Input/Output and User Interaction 38- System Authentication and Access Control 39- File System Security 40- Software Updates and Patching 41- Basic Firewall Concepts 42- System Information and Monitoring 43- Service and Daemon Management 44- System Boot Process and Runlevels 45- System Backup and Disaster Recovery 46- Comprehensive File System Management 47- System Automation Workflows 48- Multi-Concept Troubleshooting Scenarios 49- Continued Learning Pathways