terminal.cab

Linux Basics Lesson 19 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Root for a moment

9 minutes

Learning goals

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:

  • -l list what you may run
  • -u USER run it as that user instead of root

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.txt

in the glossary: sudo

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

  • - a full login with the environment of that user

Example:

sudo -i
id -un
root
exit
logout
su -
Password: 
su: Authentication failure

in the glossary: su

su - 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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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 -un

Before the command, in your home folder ~

  • ~/.bashrc
  • ~/.profile
  • ~/.secret
  • ~/Documents/
  • ~/logs/
  • ~/notes.txt
  • ~/projects/
  • ~/archive/
  • ~/handover.txt
What does sudo id -un print?

Check works once you have chosen an answer.

Put in order

A command answers Permission denied. Put the sensible steps in order.

Steps

  1. Check with sudo -l what you may do.
  2. Check owner and mode with ls -l.
  3. Read the error message and the path it names.
  4. Run this one command with sudo.

Next up: a script with 755 that still refuses to start.

Quiz

  1. Question 1 of 3Whose password does su - ask for?

    Check works once you have chosen an answer.

  2. Question 2 of 3On a Rocky Linux server you are not in the group sudo, but in wheel. Can you use sudo?

    Check works once you have chosen an answer.

  3. Question 3 of 3What speaks against working as root all day?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

  3. 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

Next: lesson 20, 755 and still Permission denied (8 min)

What you can do now: sudo -l, su -

+0 XP in this lesson

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: open
  11. Lesson 11: open
  12. Lesson 12: open
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: done
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 20, 755 and still Permission denied (8 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.