0
0
Linux-cliHow-ToBeginner · 3 min read

How to Use sudo in Linux: Simple Guide and Examples

In Linux, use the sudo command before another command to run it with superuser (administrator) privileges. This lets you perform tasks that require higher permissions without logging in as root.
📐

Syntax

The basic syntax of sudo is simple:

  • sudo [options] command

Here, sudo runs the command with elevated privileges. You may be asked to enter your password to confirm your identity.

bash
sudo command
💻

Example

This example shows how to update the package list on a Debian-based Linux system using sudo. The command apt update requires superuser rights, so we prefix it with sudo.

bash
sudo apt update
Output
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Get:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB] Fetched 215 kB in 1s (300 kB/s) Reading package lists... Done
⚠️

Common Pitfalls

Common mistakes when using sudo include:

  • Running sudo unnecessarily, which can be risky.
  • Typing the wrong password multiple times, causing sudo to deny access temporarily.
  • Using sudo with graphical applications incorrectly, which may cause permission issues.

Always use sudo only when needed and avoid running unknown commands as superuser.

bash
sudo ls /root
📊

Quick Reference

Here is a quick reference for common sudo usage:

CommandDescription
sudo commandRun a command with superuser privileges
sudo -iStart a root shell session
sudo -kInvalidate cached credentials, forcing password prompt next time
sudo -lList allowed commands for the current user
sudo !!Run the last command with sudo

Key Takeaways

Use sudo to run commands with administrator rights safely.
You will usually need to enter your password when using sudo.
Avoid using sudo unnecessarily to reduce security risks.
Use sudo -l to check which commands you can run with sudo.
Remember sudo !! to quickly rerun the last command as superuser.