Linux Command Line · Lesson 25 of 43
sudo, root and working safely
Understand the root account and sudo, how to run single commands with administrator rights, and the habits that keep a Linux system safe.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will know what administrator rights are, how to use them for exactly one command, and the habits that keep a machine safe. Many beginner disasters, from broken installs to deleted folders, come from using more power than the task needed.
The commands in this lesson change the system, so we show them as text and do not run them on the practice machine. Try them only on a computer you own and that has nothing important on it.
The root account
root is the administrator account, with user ID 0. It ignores all permission checks: it can read any file, change any setting, stop any program and delete the entire system. Its home folder is /root, and its prompt ends in #:
root@server:~#Because one slip as root can be fatal, many distributions, such as Ubuntu, lock the root account for direct login. Instead, they give a normal user the right to use sudo.
sudo: one command with extra rights
sudo (superuser do) runs a single command as root, or as another user you name. Put it in front of the command:
sudo apt updateThe first time, and again after a few minutes of inactivity, it asks for your own password, not root's. You type it blindly: by default no characters or stars are shown. The command then runs with full rights, and the rest of your session stays an ordinary user session. sudo also logs each use, which makes administration accountable.
Useful forms:
sudo -llists what you are allowed to run.sudo -u maria commandruns as the usermaria.sudo !!repeats the previous command with sudo (the shell history shortcut).sudo -iorsudo su -opens a root shell. Use it rarely, and leave it withexit.
If a normal user tries an administrative action, the error shows why:
touch /etc/example-filetouch: cannot touch '/etc/example-file': Permission deniedThe right response here is to ask: should this file really be there? If so, sudo touch /etc/example-file would work.
Who may use sudo?
Two things control it. On Debian and Ubuntu, members of the sudo group may use it; on Fedora and Red Hat systems, the group is wheel. The detailed rules live in /etc/sudoers and the files in /etc/sudoers.d/. Never edit them with a normal text editor. Use visudo, which checks the syntax before saving, because a broken sudoers file can lock everyone out of administrator access. To give a user sudo rights:
sudo usermod -aG sudo mariaThe redirect trap
Redirection is done by your shell, before sudo starts, so this fails even with sudo:
sudo echo "setting" > /etc/example.confThe shell tries to open the file as you and is refused. Run the whole command in a root shell, or use tee, which opens the file itself with root's rights:
echo "setting" | sudo tee /etc/example.confUse tee -a to append.
Safe habits
- Stay a normal user. Use
sudoonly for the one command that needs it. - Read the command first. Check every option and path, especially in anything that deletes or changes permissions.
- Do not paste commands you do not understand. Blogs and forums contain commands that look harmless and are not. A famous example downloads a script and runs it as root in one line.
- Avoid
sudowith wildcards and relative paths when unsure. A pattern expands in your folder at the moment you run it, and relative paths depend on where you are. - Do not use
sudoto silence "Permission denied" without understanding the cause. - Do not use
sudofor ordinary development tools. Runningsudo npm installorsudo pip installoften leaves root-owned files in your project that cause new errors. - Keep a backup before large changes, and test destructive commands on a copy.
Common mistakes
Believing sudo asks for the root password. It asks for yours.
Using sudo with cd. sudo cd /root fails, because cd is a shell builtin and cannot run as a separate program.
Editing files in /etc without a copy. Copy it first, for example sudo cp file file.bak.
Forgetting that files created with sudo belong to root. You may later be unable to edit them without sudo. Use chown to give them back.
Exercise
On a practice machine, run sudo -l and read the list of commands you may run. Try touch /etc/example-file without and with sudo, then remove it with sudo rm. Write one sentence explaining why sudo echo hi > /etc/example.conf fails, and rewrite it with tee.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.