This guide explains how to use the sudo command on Linux, which lets a permitted user run a command as the superuser (root) or another user. You’ll learn the syntax, common options, and practical examples, plus how to avoid frequent pitfalls. Before starting, you need a Linux system with a user account that has sudo privileges, and you’ll need to know that user’s password (or the root password, depending on configuration).
What You’ll Need
- A Linux distribution (Ubuntu, Debian, Fedora, CentOS, etc.)
- A user account that is in the
sudogroup (or listed in/etc/sudoers) - Terminal access
- The password for your user account (or root, if sudo is configured that way)
Step-by-Step
- Open a terminal. You can usually find it in the applications menu, or press
Ctrl+Alt+Ton most desktop environments. - Check if your user has sudo access. Run
sudo -l. If you see a list of commands, you’re good. If you get an error like “user is not in the sudoers file,” you’ll need to add your user to the sudo group (see Troubleshooting). - Learn the basic syntax. The general form is:
sudo [options] command [arguments]. For example,sudo apt updateruns theapt updatecommand as root. - Run a command with sudo. Type
sudofollowed by the command. For instance,sudo whoamiwill outputroot, confirming you’re running as root. - Enter your password when prompted. After you press Enter, the terminal will ask for your password. Type it (you won’t see any characters, not even asterisks, as you type) and press Enter. The command will then execute with elevated privileges.
- Understand the default timeout. By default, sudo remembers your password for 15 minutes per terminal session. If you run another sudo command within that time, you won’t be prompted again. After that, you’ll need to re-enter your password.
- Use sudo with a different user. To run a command as another user, use the
-uoption. For example,sudo -u www-data whoamirunswhoamias thewww-datauser, outputtingwww-data. - Run a command as root with a shell. To get a root shell (where every command runs as root), use
sudo -i(login shell) orsudo -s(non-login shell). This is useful for a series of administrative tasks, but be careful—you’re operating as root. - Edit a file with elevated privileges. A common use is editing system files. For example,
sudo nano /etc/hostsopens the hosts file in the nano editor with write permission. You can also usesudo vimorsudo gedit. - Use sudo with environment variables. By default, sudo resets environment variables for security. If you need to preserve an environment variable, use
sudo -E(preserve environment) or specify it inline:sudo VAR=value command. - View the sudoers file (with caution). To see the configuration, run
sudo visudo. This opens the sudoers file in a safe editor (it checks syntax before saving). Do not edit it directly with a regular editor, as a syntax error can lock you out of sudo. - Run a command without password (temporarily). If you need to run a script that calls sudo multiple times and you don’t want to keep entering your password, you can use
sudo -vto update the timestamp without running a command, or usesudo -nto fail if a password is required (useful in scripts).
Common Mistakes
- Using sudo with pipes or redirection incorrectly. For example,
sudo echo "text" > /etc/filewill fail because the redirection is done by your shell, not by sudo. Instead, usetee:echo "text" | sudo tee /etc/file. - Using sudo for commands that don’t need it. Running a simple
lsorcatwith sudo is unnecessary and can cause permission issues with files you create later. - Forgetting the password prompt. If you don’t see a prompt, your terminal might be waiting for input. Type your password and press Enter.
- Using
sudo suinstead ofsudo -i.sudo suis a common but less clean way to get a root shell; it may not set up the environment correctly. Prefersudo -i. - Editing sudoers with a regular editor. If you make a syntax error, you could lose sudo access. Always use
visudo.
Tips
- Use
sudo !!to rerun the last command with sudo. For example, if you typeapt updateand get a permission error, typesudo !!to run it as root. - Check the command before running. Use
sudo -lto see what commands you’re allowed to run. This is especially important on shared systems. - Use the
-voption to validate your credentials. Runningsudo -vwill update your timestamp and ask for your password if needed, without executing a command. This is useful before a long script. - Use
sudo -kto invalidate the timestamp. This forces sudo to ask for your password on the next command. Good for security when you step away. - Be careful with wildcards. If you use
sudo rm -rf /some/dir/*, make sure the path is correct. A typo likesudo rm -rf / some/dir(space after /) can be catastrophic.
Troubleshooting
- “user is not in the sudoers file” – This means your user isn’t in the sudo group. Boot into recovery mode or use a live USB, mount your root partition, and add your user to the sudo group. On Debian/Ubuntu, run
adduser username sudoas root. Alternatively, if you have root access, useusermod -aG sudo username. - “sudo: command not found” – This means sudo isn’t installed. You may need to switch to root (if you know the root password) and install sudo:
apt install sudooryum install sudo. - Password prompt repeats after correct entry – This can happen if your system clock is wrong or if the timestamp file is corrupt. Try
sudo -kto reset, then try again. If it persists, check your system time. - sudo hangs or is slow – This is often due to DNS resolution issues. Check your
/etc/hostsfile to ensure your hostname resolves to 127.0.1.1 or similar. - You get “Permission denied” even with sudo – This can happen if the file system is mounted with the
noexecoption, or if the file has restrictive attributes. Check withmountandlsattr.
Frequently Asked Questions
What is the difference between sudo and su?
sudo runs a single command as root (or another user) with your own password, while su switches to another user (usually root) and requires that user’s password. sudo is more secure because it logs commands and allows fine-grained permissions.
How do I add a user to sudoers?
Use sudo visudo and add a line like username ALL=(ALL:ALL) ALL. Or, on many distributions, add the user to the sudo group: sudo usermod -aG sudo username.
Why does sudo ask for my password every time?
By default, sudo caches credentials for 15 minutes. If you’re asked every time, the timestamp might be disabled in sudoers, or you’re using a new terminal session each time. You can adjust the timeout with the timestamp_timeout setting.
How do I run sudo commands without a password?
Edit the sudoers file with sudo visudo and add a line like username ALL=(ALL) NOPASSWD: ALL. Be cautious—this removes the password requirement for all commands.
What does sudo -i do?
sudo -i gives you a root shell with the root user’s environment (like a login shell). It’s similar to su - but uses your password instead of root’s.
Can I use sudo with GUI applications?
Yes, you can run sudo gedit or sudo nautilus to open GUI apps as root. However, be aware that the app may not have the correct display permissions; using sudo -E or gksudo (for older systems) can help.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- How to Add Another Account on Telegram
- Ubuntu Printer Detected But Won’t Print: Fix
- Autoplay Not Working on Windows 11? Fix It
- Edit Messages on iPhone & View Edit History
- How to Clean Rust Off Chrome Dinette Legs
- Fix Disk Signature Collision Error in Windows