Learn › Linux Foundations › User Switching
Read account identity, run one command as another ordinary account, and use real permission refusals to understand access. Practice runs in your browser. Administrator commands are introduced as a later topic.
You are about to make a copy of a settings file for the person who operates this machine. They need to own their copy so they can edit it. First find out who would create it if you ran the copy command right now.
Every running command has an effective user ID, the account number Linux uses for its ordinary file permission checks. The prompt can show a name someone chose to display. Ask the running command for its identity. Here is a recording from the practice shell.
Running whoami produced:
admin
Running id -u produced:
1000
The first command named the account running it. The second printed the number behind that name. Both ran in the same shell, so this is the name and number of one account. A file newly created by an ordinary copy command here would belong to user 1000, admin.
A different question is which account a name refers to. Adding a name after id looks up that account's record. Looking up a name leaves your shell running as before. You will use both questions before making a copy as the ordinary account operator.
An account is the identity a command runs with. To find out who would create a new file, we ran whoami on this practice machine. It printed admin, so an ordinary copy made at that prompt would be owned by admin. Try the check with help. At the prompt, type whoami, then press Enter. Read the name that appears on the line below your command. If the practice machine is unavailable, the written example and the rehearsal terminal show the same check.
prompt: admin@server1:~$ answer: whoami output: admin hint: Type whoami as one word, then press Enter.
The returned name is the evidence you can use before changing a file. The dollar sign at the next prompt just says the shell is ready for another command.
Imagine you run id operator from your current shell. It prints the account record for the name you supplied. Then you run whoami again. You can revisit the worked example or read the explanation if you are unsure.
>>> Looking up a record has no effect on the identity of the shell. Later you will ask Linux to run one command as operator and compare the two cases.
A UID is a user ID. A GID is a group ID. A group is a named set of accounts that can share access. Run id with no account name to inspect the identity used by this shell.
prompt: admin@server1:~$ answer: id output: uid=1000(admin) gid=1000(admin) groups=1000(admin),1100(webteam) hint: Type id with nothing after it.
The UID field names admin. Its primary group, shown by gid, is also named admin. The complete group list includes webteam as well. A file may grant access to its owner, its group or other accounts. Group membership can therefore matter even when you do not own the file.
An operator checking a numeric file owner may need just a UID. With id, the -u option selects the effective user ID. Compare the number to the first field you saw in the full record.
prompt: admin@server1:~$ answer: id -u ||| id --user output: 1000 hint: Add the user option -u after id.
This is the same 1000 shown next to admin in the full identity record.
Ask for the primary group with -g. Add -n to print its name. The options fit together as -gn.
prompt: admin@server1:~$ answer: id -gn ||| id -ng ||| id -g -n ||| id -n -g output: admin hint: Use id -gn. The lowercase g selects the primary group.
This account and its primary group have the same name. The field you requested is what tells you this particular answer describes a group.
The requested copy belongs in another account's home directory. Before running anything as that account, check that the name resolves and read its user and group IDs.
prompt: admin@server1:~$ answer: id operator output: uid=1001(operator) gid=37(operator) groups=37(operator),100(users) hint: Put operator after id.
The record identifies user 1001 and primary group 37. Your shell still runs as admin. The supplied name determined which record id read.
You may see the name root on a system file. It names the account with UID 0. Looking up its identity here is an ordinary read of account data. Stay at your own prompt.
prompt: admin@server1:~$ answer: id root output: uid=0(root) gid=0(root) groups=0(root) hint: Type id root. This only looks up the named account.
The result identifies UID 0. Your account does not gain any of its permissions by asking for its name or number.
If a file grants access to a group, inspect the groups of the account that will read it. Uppercase -G asks for all groups. Add -n for names and the account name for the lookup.
prompt: admin@server1:~$ answer: id -Gn operator ||| id -nG operator ||| id -G -n operator ||| id -n -G operator output: operator users hint: Use the uppercase G, lowercase n, then operator.
operator and users are group names here. Compare them with your earlier id result before attributing access to a shared group.
The program su asks the system to start a shell with another account's identity. On this prepared machine, admin may switch to the ordinary account operator without supplying a password. On another machine, the policy can differ. If this practice shows a password prompt, cancel it with Ctrl+C.
The login form su - operator opens a shell prepared for operator in operator's home directory. In that inner shell, whoami checks the new identity. exit from that inner shell returns to the waiting shell, where whoami checks the return. Typing exit at your original prompt can end your connection.
The examples here use -c to give that login shell one command instead. It runs that command and returns to the original prompt on its own. Here are the two observations, in order, from our practice machine.
Running su - operator -c whoami produced:
operator
Running whoami produced:
admin
Only the first command ran as operator. You can select a different identity for one specific task while keeping the rest of your session under your own name.
Repeat the worked switch. The lone hyphen asks for operator's login environment. After -c, give it the command whoami.
prompt: admin@server1:~$ answer: su - operator -c whoami ||| su - operator -c 'whoami' ||| su - operator -c "whoami" ||| su -l operator -c whoami output: operator hint: Type su - operator -c whoami at the admin prompt.
The answer is operator because whoami ran as operator. The one-command shell then finished.
A login shell begins in the target account's home directory on this machine. Keep the same switch and change the command it runs from whoami to pwd.
prompt: admin@server1:~$ answer: su - operator -c pwd output: /home/operator hint: Replace whoami in the previous command with pwd.
The directory is /home/operator. Inside that login shell, a path beginning with ~/ points into operator's home.
The operator command has finished. At the prompt you have now, ask which account would create a new file.
prompt: admin@server1:~$ answer: whoami output: admin hint: Run whoami without su.
You are back in admin's shell. That explains why an ordinary copy from here would have the wrong owner for the handoff.
The operator needs a personal working copy of the public site settings. Leave the original in place. Run the copy as operator so the new file has the intended owner from its creation. The text in double quotes is the one command passed to the operator shell. That shell expands the leading tilde in its destination path.
prompt: admin@server1:~$ answer: su - operator -c "cp /etc/server1/site.conf ~/site.conf" output:
hint: Inside the quoted command give cp the source /etc/server1/site.conf and the destination ~/site.conf.
A successful copy printed nothing. Check the new file's owner on the next card. Silence alone does not identify its owner.
Run stat as operator on the copy. Its -c option selects fields to print. %U is the owner name and %G is the group name. A colon separates the two.
If you reloaded the page, your new machine may have no copy yet. In that case run su - operator -c "cp /etc/server1/site.conf ~/site.conf" yourself, then retry the check.
prompt: admin@server1:~$ answer: su - operator -c "stat -c %U:%G ~/site.conf" output: operator:operator hint: Use the same operator shell as for the copy, with stat -c %U:%G ~/site.conf inside the quotes.
The first operator is the owner and the second is the group. The file supplies evidence of which account created it on this fresh machine. A matching name on a prompt would not tell you who owns the file.
You have used an ordinary account explicitly permitted for this exercise. Administrators can also use a program called sudo. Its policy decides who may run a particular command as a specified account. Privileged administration is practiced in a later course. Here you can explain a refusal by comparing the caller, the file's owner and its permission bits.
A public listing can describe a protected file even when your account cannot read its contents. Ask stat to show the owner, group and numeric permission mode of the practice credentials file. The three digits in this example describe access for the owner, group and others. For a regular file, 4 means read and 2 means write, so 6 combines read and write. A zero grants no access in that position.
prompt: admin@server1:~$ answer: stat -c "%U %G %a" /etc/server1/credentials.conf output: root root 600 hint: The format inside the quotes is %U %G %a. Then give the absolute path /etc/server1/credentials.conf.
root owns this file. Mode 600 gives its owner read and write access. Neither the group nor other accounts receive access through these mode bits. Your earlier id result identified admin, an ordinary account.
Based on that mode and your current account, predict whether cat will print any file contents. You can read the previous card again first. Now try the ordinary read.
prompt: admin@server1:~$ answer: cat /etc/server1/credentials.conf output: cat: /etc/server1/credentials.conf: Permission denied hint: Type cat followed by /etc/server1/credentials.conf. A refusal is the expected observation here.
The message came from cat. The shell is ready again, and no credential contents were printed. The preceding stat already showed this file existed, so here you can distinguish a permission refusal from an absent file.
The operator also keeps a private note. You are still admin. Try a plain read first. Keep the exact diagnostic so you can compare it with the authorized read on the next card.
prompt: admin@server1:~$ answer: cat /etc/server1/operator-notes.txt output: cat: /etc/server1/operator-notes.txt: Permission denied hint: Use cat with /etc/server1/operator-notes.txt; keep the refusal.
The result says that admin could not read through this path. It does not tell you who owns the note. You can inspect that fact separately.
You have permission to perform this exercise as operator. You need only the heading from operator's note. Keep the file private and run head -n 1 under its owner's ordinary account. Quotes pass that command and its arguments together.
prompt: admin@server1:~$ answer: su - operator -c "head -n 1 /etc/server1/operator-notes.txt" output: server1: notes kept by the operator account. hint: Give su - operator -c the quoted command head -n 1 /etc/server1/operator-notes.txt.
That line came from the same file that refused admin. You changed which account performed this one read. You did not change permissions on the file.
Inspect the owner and the mode of the same note after the successful read. Use your original account for this check.
prompt: admin@server1:~$ answer: stat -c "%U %G %a" /etc/server1/operator-notes.txt output: operator operator 600 hint: Run stat as admin, with %U %G %a and the operator-notes.txt path.
The owner and group are operator and the mode is 600. Group and other accounts still have zero mode bits. The read took place without making the note public.
A teammate suggests using the authentication log to check which account performed a task. On this image the log exists at /var/log/auth.log. Try to read it as yourself, and use the message to decide whether this account can provide that evidence.
prompt: admin@server1:~$ answer: cat /var/log/auth.log output: cat: /var/log/auth.log: Permission denied hint: Type cat /var/log/auth.log. The exercise asks you to observe the refusal.
The refusal says nothing about whether the event happened or what the log contains. Record the limitation. If this were an incident, ask the responsible operator for the needed record.
Return to this case on a later visit. A handoff report can store a numeric owner but cannot store an account name. Collect evidence that the command for the handoff actually runs with operator's numeric UID. Looking up the name and running a command as that name are different operations. Decide what measurements you would compare before reading on. Reopen the earlier examples if you want support.
>>> Use an account lookup as the expected number. Compare it with the number reported by a command that actually ran as operator. The measurements answer different questions even when the numbers agree.
Collect the expected number without changing accounts. You already used the user-only option on your own record. Apply it to the named account this time.
prompt: admin@server1:~$ answer: id -u operator output: 1001 hint: After id -u, add operator as the account whose record you want.
Keep 1001 for comparison. This lookup by itself cannot establish which identity a later command used.
Now collect the numeric UID from a command that runs as operator. Compare it with the previous result and explain in your own words what each number measured.
prompt: admin@server1:~$ answer: su - operator -c "id -u" output: 1001 hint: Run id -u as the single command of the operator login shell.
The command also reported 1001. The lookup identified the target and the second measurement observed that identity in use. In a handoff, state which command supplied each observation.
The browser cards are practice. For a handoff, save the caller's identity, the owner and mode you observed, the action you took, and a check of what stayed protected. A denied read belongs in the record when it limits what you can conclude.
An assessment has its own machine state and grading. A finished practice card, a recorded example or a request for a hint does not establish an assessment result.
Practice Chapter 7. User Switching in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.