The summary was both created and read through sudo. The effect of sudo ends with a single command, and this step shows why.
A file's owner is the user of the process that created it; the directory has no say in it. A root process created the summary, so root owns it.
sudo runs one command as another user, root by default. In sudo cat summary.txt, the cat process runs with UID 0, so the summary can be read.
Once cat exits, the shell carries on as student. sudo does not switch the terminal: the next command without sudo runs as you again.
On many servers sudo asks for a password and then skips the prompt for a few minutes. That is sudo remembering the confirmation; it does not change which user runs commands typed without sudo.
On the training machine student runs sudo without a password; that is how the training image is set up. On a production server a command may ask for a password or be refused: sudo -l shows what you are allowed to run.
What does the line sudo id -u; id -u print?
-
sudo runs the command written right after it, the first one on the line. So 0 comes first:
sudo id -uruns as root. -
A remembered password saves you from typing it again, nothing more. A command without
sudoruns as student, andid -uprints your UID. -
The semicolon separates two commands, and sudo runs the first of them. Run the line yourself: the second line of output is your UID.
sudo runs one command as root, the one written right after it; after ;, your shell runs the next one itself. So the first line of output is 0 and the second is your UID.
Key points
-
sudoruns one command as another user; your shell stays yours. -
A remembered password saves retyping it; it does not change the account of commands without
sudo. -
The sudo policy decides what you may run through sudo;
sudo -lshows it.