15.1 What sudo Does: Running Commands as Another User
Right, let’s talk about sudo. It’s not just a magic prefix that makes things work; it’s the Swiss Army knife of privilege delegation. You use it when you need to run a command as another user, most commonly the all-powerful root user, without having to log out and log back in as them. It’s the difference between handing someone the keys to the server room and just letting them flip a specific light switch. The former is su (which we’ll get to), the latter is sudo, and it’s infinitely more secure and auditable.
Think of it this way: when you prepend a command with sudo, you’re telling the system, “Hey, I, a mere mortal user, have been granted the explicit permission by the system’s administrators (which might also be me, let’s be honest) to execute this one specific command with the powers of a god.” The system then checks its little black book of rules (the /etc/sudoers file) to see if you’re telling the truth. If you are, it asks for your password as a final confirmation—not the root password—to make sure it’s really you and not some jerk who wandered away from their unlocked terminal. And then, poof, the command runs with elevated privileges.
The Core Syntax: It’s All About Who and What
The basic syntax is deceptively simple:
sudo [option] [command]
But the real power is in the options. The -u flag lets you specify which user you want to run the command as. If you omit it, sudo defaults to root, which is 99% of its use cases.
# Classic: run whoami as root. It will say 'root', not your username.
sudo whoami
# Run a command as another user, like the 'www-data' user that runs a web server.
sudo -u www-data whoami
# Want to see what you're allowed to do? The -l flag lists your privileges.
sudo -l
Running that last command (sudo -l) is like asking the bouncer, “Am I on the list?” It’s the first thing you should do on a new system to see how much trouble you can get into.
How It Actually Works (The Magic Behind the Curtain)
When you run sudo, it kicks off a beautifully simple yet secure process:
- The Ask: You type
sudo vi /etc/hosts. - The Check: The
sudobinary checks the/etc/sudoersfile (and any files in/etc/sudoers.d/) to see if your user or a group you’re in is allowed to runvias root. - The Gate: It prompts you for your password. This is a crucial security feature. It ensures that a passerby can’t just run commands on your behalf if you left your session open. By default, it will cache your authentication for 15 minutes, so you don’t have to type it for every single command. (You can change this timeout, but don’t set it to infinite unless you enjoy living dangerously).
- The Execution: If all checks pass,
sudospawns a child process as the target user (root) and executes the command within it. The originalsudoprocess then waits for its privileged child to finish and relays the output back to your unprivilebled shell.
sudo vs. su: Picking the Right Tool
This is a classic point of confusion. su (substitute user) is the old-school way. By itself, it switches you to the root user (or another user) and drops you into a new shell as that user. You are now root. Every command you run from that shell is run as root until you type exit. This is like being given the master keyring. It’s powerful but incredibly blunt and auditably terrible—if five people su to root, how do you know who ran rm -rf /?
sudo, by contrast, grants temporary, command-specific superpowers. It’s an audit trail’s best friend. The system logs who ran which command when. This is why any sane modern system disables direct root login and uses sudo instead.
You can mimic su with sudo by running sudo -i (for a root login shell) or sudo su (which is redundant, but common). But you shouldn’t make a habit of it. The whole point is to avoid having a root shell open where you can make a catastrophic typo.
Common Pitfalls and The “Why”
The TTY Quirk: Some commands, notably
vimorless, get fussy when run undersudobecause they expect a full terminal (TTY). You’ll see weird output or errors. The fix is to use the-soption to run a shell or, better yet, usesudoedit(a safe wrapper for editing files) instead ofsudo vim.# This might get weird sudo vim /etc/hosts # This is the sanctioned, safer way sudoedit /etc/hostsEnvironment Variable Shenanigans: By default,
sudoresets most of your user’s environment variables for security. This is Good™. It prevents privilege escalation through cleverly setPATHor other variables. But sometimes it breaks things. You might need a customPATHto find a binary. This is where you’d useenv_keepin thesudoersfile, but that’s an advanced tune-up.The Absolute Path Rule: This is a big one. You can’t do
sudo cd /root. Why? Becausecdis a shell builtin, not an actual binary on the filesystem.sudocan only run executable programs. To get around this, you run a shell that then does thecd:sudo bash -c "cd /root && do_something".
The real genius—and complexity—of sudo lies in the sudoers file, where you define exactly who can do what. But that’s a topic for another section. For now, just remember: it’s the best way to temporarily borrow power without causing an audit trail black hole.