Skip to lesson

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.

Beginner to intermediate·10 min·Users and permissions·Updated 2026-11-24

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 #:

text
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:

text
sudo apt update

The 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 -l lists what you are allowed to run.
  • sudo -u maria command runs as the user maria.
  • sudo !! repeats the previous command with sudo (the shell history shortcut).
  • sudo -i or sudo su - opens a root shell. Use it rarely, and leave it with exit.

If a normal user tries an administrative action, the error shows why:

bash
touch /etc/example-file
result
touch: cannot touch '/etc/example-file': Permission denied

The 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:

text
sudo usermod -aG sudo maria

The redirect trap

Redirection is done by your shell, before sudo starts, so this fails even with sudo:

text
sudo echo "setting" > /etc/example.conf

The 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:

text
echo "setting" | sudo tee /etc/example.conf

Use tee -a to append.

Safe habits

  1. Stay a normal user. Use sudo only for the one command that needs it.
  2. Read the command first. Check every option and path, especially in anything that deletes or changes permissions.
  3. 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.
  4. Avoid sudo with 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.
  5. Do not use sudo to silence "Permission denied" without understanding the cause.
  6. Do not use sudo for ordinary development tools. Running sudo npm install or sudo pip install often leaves root-owned files in your project that cause new errors.
  7. 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.

0 / 4 understood
01 Whose password does sudo ask for by default?
02 What does a # at the end of the prompt usually mean?
03 How should you edit the sudoers file?
04 Why avoid running everything with sudo?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses