Shell FluencyLesson 1 of 4
Prompt, syntax and switching users
Read the prompt, build a correct command line, and move between users without losing track of who you are.
whoamiidhostnamepwdtypewhichsusudowho
After this lesson you can
- Read the prompt to know the user, host and directory, and tell a root shell from a normal one.
- Split any command line into command, options and arguments.
- Switch users with su, su - and sudo, and explain what each keeps from the old session.
The idea
A shell reads a line, splits it into words and runs the first word as a command. Everything after it is an option (starts with - or --) or an argument (what the command works on). Before the command runs, the shell rewrites the line: it expands wildcards, variables and substitutions. You will meet that later in this skill. For now, learn to read a line the way the shell does.
The prompt tells you three things at a glance. In [alex@rhel10 ~]$, you are alex, on host rhel10, in your home directory (~), and the final $ says you are a normal user. A root shell ends in #. Treat that character as a warning light: almost every mistake that wrecks a system was typed at a #.
Not every word you type is a file. type tells you what the shell will really run: a builtin (part of bash, like cd), a keyword (like if), an alias (a nickname) or a file found by searching the directories in PATH.
Try it
whoami
hostname
pwd
alex
rhel10
/home/alex
ls -la /etc/hostname
ls --all --format=long /etc/hostname
-rw-r--r--. 1 root root 6 Oct 28 09:12 /etc/hostname
-rw-r--r--. 1 root root 6 Oct 28 09:12 /etc/hostname
The dot after the permissions is an SELinux marker, you will decode it in the SELinux skill.
type cd
type ls
type if
cd is a shell builtin
ls is aliased to `ls --color=auto'
if is a shell keyword
Switching users
There are three ways to become another user, and they differ in what you carry over.
| Command | Password asked | Environment and directory |
|---|---|---|
su bob | bob’s | Keeps your current directory and most of your environment; sets HOME and USER to bob |
su - bob | bob’s | A full login shell: bob’s home, bob’s profile |
sudo -u bob cmd | yours | Runs one command as bob; needs permission in sudoers |
sudo -i | yours | A root login shell, without ever knowing root’s password |
sudo -u bob id
uid=1001(bob) gid=1001(bob) groups=1001(bob)
Going further
who shows who is logged in, w adds what they are doing, last shows past logins and tty prints your terminal device. On a server without a graphical session (the multi-user.target) you log in on a text console, and Ctrl+Alt+F2 through F6 give you extra virtual terminals on a physical or virtual console. You will set targets in the Services, Targets and Boot skill.
Practice
0 of 5 done
Do these on your own system. They get harder. Check each result, then tick it off.
- Warm-up
Say who and where you are
Without looking at the prompt, print your user name, the machine name and your current directory, each with its own command.
Check your result
whoami; hostname; pwdThe three lines match what the prompt shows:
alex, your host name, and a path such as/home/alex.Hint
Three short commands, separated by semicolons if you want them on one line.
Solution
whoami hostname pwd - Warm-up
Same command, four spellings
These four commands list the same thing. Run them and confirm the output is identical, then say which two options mean the same.
ls -l -a /etc/ssh ls -la /etc/ssh ls -al /etc/ssh ls --all -l /etc/sshCheck your result
Run each with
| md5sum. All four print the same checksum.-aand--allare the short and long form of one option, and short options can be bundled in any order.Hint
Pipe each command into
md5sumand compare.Solution
ls -l -a /etc/ssh | md5sum ls -la /etc/ssh | md5sum ls -al /etc/ssh | md5sum ls --all -l /etc/ssh | md5sum - Practice
Built-in, alias, file or keyword?
Use
typeto classifycd,ls,ifandpwd. Then usewhichoncdandlsand explain why the two tools disagree aboutcd.Check your result
cdis a shell builtin,ifis a shell keyword,lsis an alias on RHEL (or a file in/usr/bin), andpwdis both a builtin and a file.which cdfinds nothing or a file that is not the one your shell runs, becausewhichonly searchesPATHand knows nothing about builtins.Hint
type -a namelists every match in the order the shell would try them.Solution
type cd ls if pwd type -a pwd which cd ls - Practice
su versus su -
Create a user
bob(sudo useradd -m bob) and give them a password (sudo passwd bob). From your own shell, runsu bob -c 'pwd; echo $HOME'and thensu - bob -c 'pwd; echo $HOME'. Explain the difference in one sentence.Check your result
The first form (no dash) sets
$HOMEto bob's home but leaves you in the directory you were in, sopwdprints your own directory andecho $HOMEprints/home/bob. The second form starts a login shell: it moves to bob's home and loads bob's profile, so both lines print/home/bob.Hint
su -is the same assu -l: a login shell, as if bob had logged in.Solution
sudo useradd -m bob sudo passwd bob su bob -c 'pwd; echo $HOME' su - bob -c 'pwd; echo $HOME' - Challenge
Prove which user ran it
Using one command each, show your user ID as alex, as bob via
sudo -u, and as root viasudo -i. Then list who is logged in on the machine and on which terminal you are. State whatexitdoes in each of the nested shells.Check your result
The three
idoutputs show UIDs 1000 (or your own), bob's UID and 0.wholists your session andttyprints a device such as/dev/pts/0. Eachexitreturns you to the shell you came from, never logs you out of the machine until the last one.Hint
sudo -u bob id,sudo id, andsudo -ifor an interactive root login shell.ttyprints the current terminal.Solution
id sudo -u bob id sudo id sudo -i id exit who tty