1 of 4

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.

  • whoami
  • id
  • hostname
  • pwd
  • type
  • which
  • su
  • sudo
  • who

30 min5 exercises

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.

CommandPassword askedEnvironment and directory
su bobbob’sKeeps your current directory and most of your environment; sets HOME and USER to bob
su - bobbob’sA full login shell: bob’s home, bob’s profile
sudo -u bob cmdyoursRuns one command as bob; needs permission in sudoers
sudo -iyoursA 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.

  1. 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; pwd

    The 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
  2. 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/ssh
    Check your result

    Run each with | md5sum. All four print the same checksum. -a and --all are the short and long form of one option, and short options can be bundled in any order.

    Hint

    Pipe each command into md5sum and compare.

    Solution
    ls -l -a /etc/ssh | md5sum
    ls -la /etc/ssh | md5sum
    ls -al /etc/ssh | md5sum
    ls --all -l /etc/ssh | md5sum
  3. Practice

    Built-in, alias, file or keyword?

    Use type to classify cd, ls, if and pwd. Then use which on cd and ls and explain why the two tools disagree about cd.

    Check your result

    cd is a shell builtin, if is a shell keyword, ls is an alias on RHEL (or a file in /usr/bin), and pwd is both a builtin and a file. which cd finds nothing or a file that is not the one your shell runs, because which only searches PATH and knows nothing about builtins.

    Hint

    type -a name lists every match in the order the shell would try them.

    Solution
    type cd ls if pwd
    type -a pwd
    which cd ls
  4. Practice

    su versus su -

    Create a user bob (sudo useradd -m bob) and give them a password (sudo passwd bob). From your own shell, run su bob -c 'pwd; echo $HOME' and then su - bob -c 'pwd; echo $HOME'. Explain the difference in one sentence.

    Check your result

    The first form (no dash) sets $HOME to bob's home but leaves you in the directory you were in, so pwd prints your own directory and echo $HOME prints /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 as su -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'
  5. Challenge

    Prove which user ran it

    Using one command each, show your user ID as alex, as bob via sudo -u, and as root via sudo -i. Then list who is logged in on the machine and on which terminal you are. State what exit does in each of the nested shells.

    Check your result

    The three id outputs show UIDs 1000 (or your own), bob's UID and 0. who lists your session and tty prints a device such as /dev/pts/0. Each exit returns 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, and sudo -i for an interactive root login shell. tty prints the current terminal.

    Solution
    id
    sudo -u bob id
    sudo id
    sudo -i
    id
    exit
    who
    tty