root vs sudo: Why You Shouldn't Work as root
You Were Told to Add sudo, but What Is root?
When you follow a tutorial online, you keep seeing commands that start with sudo, like sudo apt update. Maybe you treat it as a magic word you add to make things work. Dig deeper and you hit warnings like "don't work as root" and "use least privilege," which only adds to the confusion.
root is the all-powerful administrator account on Linux, and sudo is a way to borrow that power for a single command. In this guide, Lina and Linny-senpai build an intuition for why you stay a normal user and reach for sudo only when needed. By the end, sudo becomes a safety device you understand, not a spell you chant.
What You'll Learn
- That
root(the superuser) is a special account that can do anything - Why staying logged in as root is dangerous
- That
sudoborrows privileges for just one command - The difference between
suandsudo - The basic form
sudo command - The idea behind the "principle of least privilege"
1. What Is root, Really?
Conclusion: root is the one special administrator account allowed to do everything on Linux. It bypasses all permission checks and can read, write, or delete any file on the system.
sudo in front of commands. But what is root in the first place?root is the most powerful account on Linux—the one that can do anything. In English it's also called the "superuser" or "administrator."root = an account exempt from permission checks
- Its username is
root, and its user ID (UID) is0 - It can read, write, and delete everything regardless of a file's permissions (
rwx) - It can perform system-wide actions (installing packages, restarting services, adding users)
It's more accurate to think "the limits are removed" than "it's strong." For permissions themselves, see A Mental Model for Permissions.
2. Why Is Working as root Dangerous?
Conclusion: Because root skips permission checks, a single typo can break the whole system. The real danger is that mistakes and malware reach "every area" instead of being contained.
Mistakes as root can be fatal
With the permission-check safety device removed, root makes these accidents easy.
- A mistyped delete command removes system files you never meant to touch
- An accidentally overwritten config file leaves the machine unable to boot
- A malicious script run as root rewrites the entire system
Do your everyday work as a normal user, and borrow admin rights only for the moment you need them. That's how you keep the blast radius small.
3. What Does the sudo Command Do?
Conclusion: sudo runs just one command with root's privileges. You stay a normal user and borrow power only when you need it.
sudo comes in. sudo stands for "superuser do," and it asks the system to "run just this one command as the administrator."sudo apt update, only that apt update command runs with root's privileges. The moment it finishes, you're back to your normal user. You don't stay as root.sudo = borrow privileges for "just one command"
- Only the command prefixed with
sudoruns with root privileges - You return to your normal user immediately afterward (you don't stay as root)
- The first time, it asks for your own password (not root's password)
- It logs who ran what and when
Not "administrator forever," but "administrator for the single moment you need it." That's the heart of staying safe.
4. How Are su and sudo Different?
Conclusion: su switches you into root and stays there; sudo borrows privileges for one command and returns them immediately. For everyday use, sudo is the safe choice.
su. How is it different from sudo?su (switch user) switches users, and with no argument it switches you to root. Once you switch, you stay as root until you exit.su makes you root, every command you type runs with root privileges. The no-brake state continues. With sudo, it's per command, so you spend only a moment in the dangerous state.| Item | su |
sudo |
|---|---|---|
| What it does | Switch to root and stay there | Borrow privileges for one command |
| Scope of power | root until you exit |
Only while that command runs |
| Password asked | root's password | your own (the running user's) |
| Logging | Hard to track | Records who did what |
| Everyday use | Discouraged | Recommended |
sudo su exists too, but start with sudo command
There's also sudo su or sudo -i to "enter a root shell via sudo." It's used when you need to run several admin tasks in a row, but it carries the same risk as su because you sit in a root shell. While you're a beginner, it's safer to get used to the basic form: add sudo to each command.
5. How Do You Use sudo? (The Basic Form)
Conclusion: Just put
sudoin front of the command that needs admin rights. The basic form issudo <command>, and it asks for your own password the first time.
sudo in front of a command that needs admin rights. For example, to refresh the package list, you type this.sudo apt update
[sudo] password for lina: Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease ...
You can check who you are right now with whoami.
whoami
lina
Only inside the sudo-prefixed command do you act as root; once it ends, you're back to lina.
Check what you're allowed to do with sudo -l
Which users may use sudo, and which commands they can run, is managed in a config file called /etc/sudoers. To list the sudo actions allowed to you, run this.
sudo -l
Knowing "what I can run with admin rights" makes it faster to diagnose a Permission denied dead end.
6. What Is the Principle of Least Privilege?
Conclusion: The idea of holding only the minimum privileges needed for the task. Stay a normal user day to day and borrow admin rights only for the moment you need them, reducing both accidents and damage.
sudo only for the one moment you need it.Least privilege becomes a habit
- Log in as a normal user day to day (don't live as root)
- Add
sudoonly to commands that need admin rights - Before adding
sudo, pause and ask "does this command truly need admin rights?" - Don't copy-paste and run a
sudocommand you don't understand
Make "give strong privileges back quickly" a habit, and you'll prevent most accidents.
7. How Do You Try It Hands-On?
Conclusion: Check who you are with
whoami, then actually type commands with and withoutsudoto see how access changes. That's the fastest way to understand.
whoami, then try a privileged action with and without sudo—you'll feel "ah, this is where admin rights are needed."whoami and the sense of privileges without worrying about breaking anything.whoami id
lina uid=1000(lina) gid=1000(lina) groups=1000(lina),27(sudo)
If the id output includes a sudo group, that user can use sudo. If uid is anything other than 0 (like uid=1000), you're a normal user right now, not root.
Type whoami and id in the Penguin Gym Linux terminal and confirm "who am I right now." Once you grasp the sense of privileges, deciding whether to add sudo becomes second nature.
Summary
rootis the do-anything administrator account exempt from permission checks- Living as root lets mistakes and malware reach the entire system
sudoborrows privileges for one command and returns them immediately (with a log of responsibility)sukeeps you as root;sudois per command—usesudofor everyday work- The basic form is
sudo <command>; check what you're allowed withsudo -l - Underneath it all is the principle of least privilege: be a normal user by default, an administrator only for the moment you need it