Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

What is sudo in Linux and why it matters

· 6 min read · Home Tech

In this article
  1. Why sudo exists instead of just using root
  2. How sudo actually works
  3. How to use sudo: common commands and syntax
  4. sudo vs su: what’s the difference?
  5. Common problems and how to fix them
  6. Security best practices with sudo
  7. Alternatives to sudo
  8. Frequently Asked Questions

The short answer: sudo is a standard Linux/Unix command that lets a permitted user run a command as another user (usually the system administrator, root) while keeping a log of what was run. It matters because it gives you administrative power without forcing you to log in as root all the time, which is both safer and more auditable. The name comes from “superuser do” — though some joke it stands for “su do” — and it was first written in 1980 by Bob Coggeshall and Cliff Spencer at the University of New York at Buffalo. It is not slang; it is a real, standard command found in virtually every Linux distribution and in macOS.

Why sudo exists instead of just using root

In early Unix systems, you either logged in as a normal user or as root (the all-powerful administrator). Logging in as root for everyday work is dangerous: one mistyped command like rm -rf / can wipe the system, and any malware running under your session gets full control. Sudo solves that by letting you stay as a normal user and only escalate privileges for a single command. That means:

  • You only get root power for the exact command you type, not for the whole session.
  • Every sudo command is logged in /var/log/auth.log (or /var/log/secure on some distros), so you can see who ran what and when.
  • You can grant specific users permission to run specific commands without giving them full root access.
  • You need to re-enter your password after a timeout (default 15 minutes) so a forgotten terminal can’t be used by someone else.

How sudo actually works

When you type sudo apt update, the system checks a configuration file called /etc/sudoers to see if your username (or your group) is allowed to run that command. If yes, it prompts for your password (not root’s), verifies you, and then runs the command with root’s user ID (UID 0) and privileges. The command runs exactly once; after it finishes, you are back to your normal user.

The /etc/sudoers file is not meant to be edited with a normal text editor because a syntax error can lock you out of admin access. Instead, you use the visudo command, which opens the file in a safe editor and checks syntax before saving. A typical line looks like:

alice ALL=(ALL:ALL) ALL

That means user alice can run any command on any host as any user. A more restrictive example:

bob ALL=(root) /usr/bin/apt, /usr/bin/systemctl

That lets bob run only apt and systemctl as root, and nothing else.

How to use sudo: common commands and syntax

The most common form is simply sudo <command>. For example:

  • sudo apt update – refresh package lists (Debian/Ubuntu).
  • sudo systemctl restart sshd – restart a service.
  • sudo nano /etc/hosts – edit a system file.
  • sudo -i – start a root shell (like logging in as root).
  • sudo -u www-data ls /var/www – run a command as a different user (here, www-data).

You can also run a command with elevated privileges but without changing the environment by using sudo -E (preserves your environment variables). If you need to run several commands as root in a script, you can use sudo bash -c 'command1; command2'.

sudo vs su: what’s the difference?

su (short for substitute user) is the older command. Typing su alone switches you to root but requires the root password, and it stays in a root shell until you type exit. That means you are root for many commands, not just one. Sudo, on the other hand, uses your own password and only elevates the single command. Most modern distros (like Ubuntu) disable the root account entirely and rely on sudo. If you ever see instructions saying “use su”, they are usually for older systems or for switching to a specific user like su - postgres.

Common problems and how to fix them

The most frequent issue is the “user is not in the sudoers file” error. That happens when your account was not added to the sudo group during installation. To fix it, you need to log in as root (if you have the root password) or boot into recovery mode and run:

usermod -aG sudo yourusername

Another common problem is forgetting your password. Sudo uses your own password, so if you forgot it, you need to reset it via recovery mode or a live USB — there is no “sudo password” separate from your login password.

A third issue is the “sudo: command not found” error on minimal systems. Some minimal containers or servers don’t have sudo installed. You can install it as root with apt install sudo (Debian/Ubuntu) or dnf install sudo (Fedora).

If sudo suddenly stops working after you edited /etc/sudoers, you likely have a syntax error. Boot into recovery mode, or use a live USB, and fix the file with visudo — never edit it directly with a regular editor.

Security best practices with sudo

Using sudo is safer than logging in as root, but it still needs care:

  • Always use sudo for the smallest command needed. Don’t use sudo -i if a single sudo command will do.
  • Keep your password strong, because anyone who knows it can run sudo commands (if they have permission).
  • Review /var/log/auth.log regularly to spot unusual sudo usage.
  • Use the NOPASSWD directive sparingly — it lets a user run sudo without a password, which is convenient but less secure.
  • For servers, consider disabling root login over SSH and using sudo only. That way, an attacker has to guess both a username and a password, and you get a log of every admin action.

Alternatives to sudo

While sudo is the standard, there are a few alternatives:

  • doas – a minimal replacement from OpenBSD, now available on Linux. It’s simpler to configure and has a smaller attack surface, but it lacks some of sudo’s advanced features like fine-grained command restrictions.
  • su – still used on some systems, but requires the root password and gives a full root shell.
  • pkexec – part of PolicyKit, used by graphical applications to request elevation. It’s what you see when a GUI app asks for your password.

For most users, sudo is the best choice because it’s pre-installed, well-documented, and supported everywhere.

Frequently Asked Questions

Is sudo a word or an acronym?

It’s an acronym for “superuser do”. It’s not slang; it’s a standard command name. You use it in a sentence like: “Run sudo apt upgrade to update your system.”

Do I need sudo for every command?

No. Only commands that need root privileges — like installing software, changing system files, or managing services — require sudo. Normal commands like ls, cd, and cat work without it.

What happens if I type sudo without a password?

If your user is in the sudoers file, you’ll be prompted for your password. If you’re not in the file, you’ll get an error. Some systems have a NOPASSWD entry that skips the password, but that’s rare.

Can I use sudo on macOS?

Yes, macOS includes sudo by default. It works the same way as on Linux, though the configuration file is still /etc/sudoers.

Is sudo the same as “run as administrator” in Windows?

Conceptually yes — both elevate privileges for a single action — but technically they’re different. Windows uses User Account Control (UAC) with a separate admin account, while sudo uses your own account with a one-time password check.

Can I give someone sudo access without giving them the root password?

Yes. That’s the whole point of sudo. You add them to the sudo group or add a specific rule in /etc/sudoers that limits which commands they can run.