Users, sudo and Installing Software
Understand root and regular users, use sudo safely, install and update software with apt or dnf, and learn how the shell finds commands.
Welcome back!
Step 1 of 13Learn
Root and the principle of least privilege
Every Linux system has one account with unlimited power: root, user ID 0. Root can read any file, kill any process, change any password and delete the operating system itself. Permission checks simply don't apply to it.
That power is exactly why you should rarely be logged in as root. A typo as a regular user damages your own files; the same typo as root can damage the whole machine. Malware that runs under your account is limited to your files; under root, it owns the system. Security people call this the principle of least privilege: every person and program should have only the access it needs, for only as long as it needs it.
Linux follows the principle with accounts:
- Regular users like
lenahave a home directory and control over their own files. - System users like
www-data,mysqlorsshdexist only so a service can run with limited rights. Nobody logs in as them. - Groups give shared access: adding a user to the
dockerordevsgroup grants what that group can do.
You can inspect accounts with a few commands: whoami prints your user name, id prints your user ID and groups, and /etc/passwd lists all accounts, one per line (despite the name, password hashes live in the root-only file /etc/shadow).
Step 2 of 13Practice · Diagram
Kinds of accounts
Which branch does the account sshd belong to?
Tap the shape that answers the question, then press Check.
- Accounts on a Linux system
- root (UID 0)
Unlimited power; permission checks do not apply.
- Regular users
People, with a home directory: lena, sofia.
- System users
Exist so a service runs with limited rights. Nobody logs in as them.
- lena
- www-data
- mysql
Step 3 of 13Learn
sudo: borrowing root for one command
Instead of logging in as root, administrators use sudo ("superuser do"). Put it in front of a single command, and that command runs with root privileges:
$ apt update
E: Could not open lock file ... (13: Permission denied)
$ sudo apt update
[sudo] password for lena:sudo asks for your own password, not root's, and then remembers it for a few minutes. Only users listed in the sudo configuration, often through membership in the sudo or wheel group, can use it. Every sudo command is logged, so there is a record of who did what.
Useful patterns:
sudo !!reruns the previous command with sudo.sudo -u postgres psqlruns a command as a different user, not root.sudo -iopens a full root shell. Use it sparingly and exit as soon as you are done; the prompt changes to#as a reminder.sudo visudosafely edits the sudo configuration file, checking it for errors before saving so you can't lock yourself out.
A redirect trap: sudo echo "127.0.0.1 test" >> /etc/hosts fails with "Permission denied". sudo applies to echo, but the >> redirect is performed by your own, non-root shell. The usual fix is echo "127.0.0.1 test" | sudo tee -a /etc/hosts.
Step 4 of 13Diagram
What happens when you type sudo apt update
Tap a shape with to see more.
- sudo apt update
- Asks for YOUR password
Not root's. It is remembered for a few minutes.
- Checks you are allowed
Usually by membership in the sudo or wheel group.
- Runs apt as root
Only this one command gets root privileges.
- Logs who did it
Every sudo command leaves a record.
Step 5 of 13Practice · True or false
When you run a command with sudo, you type the root account's password.
Step 6 of 13Practice · Spot the mistake
A beginner wrote these notes about sudo and users. Tap the two lines that are wrong.
Step 7 of 13Learn
Installing software with a package manager
On Linux you rarely download installers from websites. Each distribution has a package manager that installs software from trusted repositories, resolves dependencies automatically and keeps everything updated with one command.
| Task | Debian, Ubuntu (apt) | Fedora, RHEL (dnf) |
|---|---|---|
| Refresh the list of available packages | sudo apt update | (automatic) |
| Install a package | sudo apt install htop | sudo dnf install htop |
| Upgrade everything installed | sudo apt upgrade | sudo dnf upgrade |
| Remove a package | sudo apt remove htop | sudo dnf remove htop |
| Search for a package | apt search nginx | dnf search nginx |
The apt pair confuses people: apt update only refreshes the catalog of what is available; it changes no software. apt upgrade actually installs the newer versions. You normally run them together: sudo apt update && sudo apt upgrade.
Installing needs sudo because packages write to system directories like /usr/bin and /etc. Searching does not.
Regular updates matter. Most successful attacks on servers use known vulnerabilities that already have patches. A server that hasn't been upgraded in a year is running software with publicly documented holes. Many teams turn on automatic security updates for exactly this reason.
Step 8 of 13Practice · Sort into groups
Sort each command by whether it changes installed software or only reads information.
Tap an item, then tap its group (or drag it there).
Step 9 of 13Compare
apt update vs apt upgrade
| Aspect | apt update | apt upgrade |
|---|---|---|
| What it changes | The catalog of available packages | The installed software itself |
| Installs anything? | No | Yes, newer versions |
| Needs sudo | Yes (writes the catalog) | Yes |
| Order | Run first | Run second: sudo apt update && sudo apt upgrade |
Step 10 of 13Learn
How the shell finds a command: PATH
When you type htop, how does the shell know where the program is? It checks a list of directories stored in the PATH environment variable, in order, and runs the first match it finds.
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin
$ which htop
/usr/bin/htopThe directories are separated by colons. which htop (or type htop) shows the exact file that would run. If two versions of a program exist, the one in the directory listed earlier wins, which explains many "but I installed the new version!" puzzles.
"command not found" means no directory on PATH contains a program with that name. Either it isn't installed, the name is misspelled, or it lives somewhere that isn't on PATH.
This is also why Aisha typed ./deploy.sh and not deploy.sh. The current directory is deliberately not on PATH, so the shell won't run a program just because it happens to be in the folder you are in. That protects you from a malicious file named, say, ls sitting in a downloaded directory. ./ says explicitly: run the file in this directory.
Step 11 of 13Diagram
How the shell finds htop
With PATH=/usr/local/bin:/usr/bin:/bin the shell checks each directory in order and stops at the first match.
Tap a shape with to see more.
- You type htop
- /usr/local/bin? No
Not found here, keep looking.
- /usr/bin? Yes
Found /usr/bin/htop. The search stops.
- Run /usr/bin/htop
If no directory had it, you would see "command not found".
Step 12 of 13Practice · Fill in the blanks
Fill in the blanks
To see which file actually runs when you type python3, use ` python3. To run a script called setup.sh` that sits in your current directory, type .
Step 13 of 13Practice · Choose one
You install a tool, but typing its name gives "command not found". ls /opt/tool/bin shows the program is there. What is the most likely cause?
Lesson complete!