Linux Basics Lesson 19 of 38
Root for a moment
9 minutes
Learning goals
- Run a single command as root with sudo and see what you may do with sudo -l.
- Explain the difference between sudo -i and su -.
- Say why sudo is better than staying root, and how Debian and RHEL differ.
One command as root
sudo COMMAND runs that command as root, if the rules in /etc/sudoers allow it. On Debian and Ubuntu, members of the group sudo may do everything. On RHEL and Rocky Linux, that group is called wheel.
sudo -l
Purpose: Runs one command as root and writes down who did it.
Syntax: sudo COMMAND | sudo -l
Options:
Example:
id -un
lerner
sudo id -un
root
sudo -l
Matching Defaults entries for lerner on web-01:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin, use_pty
User lerner may run the following commands on web-01:
(ALL : ALL) ALL
(ALL) NOPASSWD: ALL
sudo chown root handover.txt
ls -l handover.txt
-rw-r--r-- 1 root lerner 33 Oct 17 08:30 handover.txtThis practice machine does not ask for a password, because of the NOPASSWD line that sudo -l shows at the end. On a real server, sudo usually does ask, for your own password, and then remembers it for a few minutes.
You can read the rules with sudo cat, because they belong to root:
sudo cat /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# This fixes CVE-2005-4890 and possibly breaks some versions of kdesu
# (#1011624, https://bugs.kde.org/show_bug.cgi?id=452532)
Defaults use_pty
# This preserves proxy settings from user environments of root
# equivalent users (group sudo)
#Defaults:%sudo env_keep += "http_proxy https_proxy ftp_proxy all_proxy no_proxy"
# This allows running arbitrary commands, but so does ALL, and it means
# different sudoers have their choice of editor respected.
#Defaults:%sudo env_keep += "EDITOR"
# Completely harmless preservation of a user preference.
#Defaults:%sudo env_keep += "GREP_COLOR"
# While you shouldn't normally run git as root, you need to with etckeeper
#Defaults:%sudo env_keep += "GIT_AUTHOR_* GIT_COMMITTER_*"
# Per-user preferences; root won't have sensible values for them.
#Defaults:%sudo env_keep += "EMAIL DEBEMAIL DEBFULLNAME"
# "sudo scp" or "sudo rsync" should be able to use your SSH agent.
#Defaults:%sudo env_keep += "SSH_AGENT_PID SSH_AUTH_SOCK"
# Ditto for GPG agent
#Defaults:%sudo env_keep += "GPG_AGENT_INFO"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "@include" directives:
@includedir /etc/sudoers.d
The %sudo line lets the group sudo do everything. The @includedir line also pulls in the files from /etc/sudoers.d, and that is where the rule lives that spares this practice machine the password:
sudo cat /etc/sudoers.d/90-lerner
lerner ALL=(ALL) NOPASSWD: ALL
You change them with visudo only. It checks the syntax before saving, because a broken file can lock everyone out of sudo. visudo does not run in this simulator, so this is one to remember for a real machine.
A whole shell: sudo -i and su -
For a few commands in a row, sudo -i opens a shell as root with root's environment, and exit leaves it:
su -
Purpose: Opens a shell as another user. sudo -i does the same for root with your own password.
Syntax: su - [USER] | sudo -i
Options:
Example:
sudo -i
id -un
root
exit
logout
su -
Password:
su: Authentication failuresu - asks for the password of the target user, not yours. Root is locked here, as on a default Ubuntu or Debian with sudo, so it ends in Authentication failure with any password. That is why sudo -i is the way to root. su only skips the question if you are root already.
Try su - gast by hand: the password of the user gast on this machine is gast. Then id -un shows who you are, and exit takes you back. That is useful for reproducing a problem with the rights of a service user.
Practice
Terminal task 1/2
handover.txt belongs to you, and it should belong to root. Give it away with one command.
Task 1/2: handover.txt belongs to you, and it should belong to root. Give it away with one command.
handover.txt belongs to you, and it should belong to root. Give it away with one command.
Task complete
Solution
sudo chown root handover.txt Why: A single command as root, and the log knows who ran it. The rest of the session stays yours.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/2
Read the rule in /etc/sudoers.d/90-lerner that lets you use sudo without a password here.
Task 2/2: Read the rule in /etc/sudoers.d/90-lerner that lets you use sudo without a password here.
Read the rule in /etc/sudoers.d/90-lerner that lets you use sudo without a password here.
Task complete
Solution
sudo cat /etc/sudoers.d/90-lerner Why: Reading is allowed, editing is a job for visudo. Whoever may run everything without a password has root in practice, so such a line should be rare on a real machine.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Fill the gap
To see which commands you may run with sudo, you type: sudo
To see which commands you may run with sudo, you type: sudo …
Check works once you have typed an answer.
Predict the output
Command
sudo id -unBefore the command, in your home folder ~
- ~/.bashrc
- ~/.profile
- ~/.secret
- ~/Documents/
- ~/logs/
- ~/notes.txt
- ~/projects/
- ~/archive/
- ~/handover.txt
Check works once you have chosen an answer.
Put in order
A command answers Permission denied. Put the sensible steps in order.
Steps
- Check with sudo -l what you may do.
- Check owner and mode with ls -l.
- Read the error message and the path it names.
- Run this one command with sudo.
Next up: a script with 755 that still refuses to start.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 5 exercises, 3 quiz questions
What you can do now: sudo -l, su -
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: done
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
sudo <command>one command as root -
sudo -lwhat may I run -
sudo -ishell as root, leave with exit -
su - <user>shell as that user, asks for that user's password
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 18 KB)
- View as image (PNG, 52 KB)