The Terminal and the Shell
When you exploit a vulnerability you don’t get a desktop: you get a command line, sometimes with no history, no tab completion and no Ctrl+C. And on your own machine, the terminal is where you chain tools together to turn thousands of lines of output into the one piece of data you need. This page covers the basics: what each piece is, how to read a command and how commands are combined.
Concepts
Section titled “Concepts”Three different pieces
Section titled “Three different pieces”| Piece | What it is | Examples |
|---|---|---|
| Terminal emulator | The window: it takes keystrokes and draws text. It doesn’t understand commands. | GNOME Terminal, iTerm2, Ghostty, Windows Terminal |
| Shell | The interpreter: it reads the line, expands it, finds the program and launches it. | bash, zsh, sh/dash, fish; on Windows, PowerShell and cmd |
| Program | The executable that does the work. | /usr/bin/ls, /usr/bin/nmap |
- bash is what almost every tutorial assumes; zsh is the default shell on Kali and macOS; on Debian and Kali,
/bin/shis dash, which is simpler and lacks many bash features. - Some commands aren’t programs but shell builtins (
cd,export,umask,alias): that’s whywhich cdreturns nothing.
Anatomy of a command
Section titled “Anatomy of a command”ls -la /var/log│ │ └ argument: what it acts on│ └ options: short ones can be grouped (-l -a = -la); long ones use two dashes (--all)└ command| To find out… | Use |
|---|---|
| What a command does and all its options | man ls (/text searches, n next, q quits) |
| The quick summary | ls --help |
| Which command does something | apropos "copy files" |
| Where the executable is | which nmap |
| What it really is (program, builtin, alias, function) | type ls |
- Absolute: starts with
/and always leads to the same place (/etc/passwd). - Relative: depends on where you are (
pwdtells you). - Shortcuts:
.the current directory (that’s why you run./script.sh),..the one above,~your home folder,cd -goes back to the previous one. - Wildcards are expanded by the shell before the program runs:
*anything,?one character,[abc]one of those.
The three channels
Section titled “The three channels”Every process has 0 input (stdin), 1 output (stdout) and 2 errors (stderr). By default, output and errors both go to the screen, mixed together.
| Syntax | What it does |
|---|---|
> f · >> f |
Output to a file, overwriting · appending |
2> f |
Only errors to the file |
> f 2>&1 · &> f |
Output and errors to the same file |
2>/dev/null |
Throw errors away (/dev/null discards everything) |
< f |
Use a file as input |
a | b |
Pipe: a’s output is b’s input. Errors don’t go through |
a ; b |
b always |
a && b · a || b |
b only if a succeeds · only if it fails |
Every command ends with an exit code: 0 is success, any other number is failure. It’s in $? right afterwards.
flowchart LR
A["find / -perm -4000"] -->|"stdout"| B["grep -v snap"]
A -->|"stderr"| N["2>/dev/null"]
B -->|"stdout"| C["sort"]
C -->|">"| D["suid.txt"]
Variables and PATH
Section titled “Variables and PATH”VAR=value(no spaces around=) creates a variable in the current shell;export VARmakes programs you launch inherit it;envlists them.- Double quotes
"$VAR"expand the variable; single quotes'$VAR'keep it literal. - The PATH is the ordered list of directories where the shell looks for programs. The current directory is not in it (that’s why
./is needed). Extend it withexport PATH="$PATH:$HOME/bin"and, to make it stick, in your shell’s startup file.
Text filters
Section titled “Text filters”| Tool | What for | Example |
|---|---|---|
grep |
Filter lines: -i case-insensitive, -v invert, -r recursive, -l names only, -E regex |
grep -ri password /etc 2>/dev/null |
cut |
Cut columns by delimiter | cut -d: -f1 /etc/passwd |
sort · uniq |
Sort · remove consecutive duplicates (-c counts) |
sort | uniq -c | sort -rn |
awk |
Columns and simple logic | awk '{print $1}' access.log |
sed |
Replace text on the fly | sed 's/http:/https:/g' |
wc |
Count lines, words, bytes | wc -l hosts.txt |
find |
Search files by name, type, size, owner, permissions | find / -perm -4000 -type f 2>/dev/null |
head · tail · less |
Start · end (-f follows the file) · page through |
tail -f /var/log/auth.log |
For finer regular expressions, see regex.
Victim shells: from “dumb” to interactive
Section titled “Victim shells: from “dumb” to interactive”A freshly caught reverse shell usually has no TTY: no history or tab completion, Ctrl+C kills it, and sudo or su don’t work properly. The classic upgrade:
# 1. In the victim's shell: create a pseudo-terminalpython3 -c 'import pty; pty.spawn("/bin/bash")' # or: script -qc /bin/bash /dev/null# 2. Background it with Ctrl+Z and, in YOUR terminal:stty raw -echo; fg# 3. Back in the victim:export TERM=xtermstty rows 40 columns 160 # match your window (in your terminal: stty size)More variants and how to catch them in reverse shells.
In practice
Section titled “In practice”# Get your bearings on a new machinewhoami; id; hostnameuname -a; cat /etc/os-releaseip -br a; df -h; free -h
# A typical chain: users with a valid shell, sortedgrep -vE 'nologin|false' /etc/passwd | cut -d: -f1,7 | sort
# How many accounts use each shellcut -d: -f7 /etc/passwd | sort | uniq -c | sort -rn
# Save output and see it at the same timenmap -sV 10.10.10.5 | tee nmap.txtNext offensive step
Section titled “Next offensive step”- Read the system you’ve just compromised: Linux: files, users and permissions.
- Catch and stabilise a shell: reverse shells.
- Automate the repetitive stuff: Bash scripting.
Cheat sheet
Section titled “Cheat sheet”| Concept | What to remember |
|---|---|
| Terminal · shell · program | Window · interpreter · executable. |
| Builtin | Part of the shell (cd, export): which can’t find it; type can. |
.zshrc · .bashrc |
Your shell’s one. Kali uses zsh. |
> · >> |
Overwrites · appends. |
2>/dev/null |
Gets rid of error noise. |
| |
Only passes output, not errors. |
&& · ; |
Only on success · always. |
$? |
Exit code: 0 = success. |
sudo and > |
Your shell does the redirection. Use sudo tee. |
uniq |
Only looks at the previous line: always after sort. |
| PATH | No .. “Command not found” is usually the PATH. |
| Dumb shell | pty.spawn → Ctrl+Z → stty raw -echo; fg → TERM. |
Review questions
Section titled “Review questions”-
Why does
which cdreturn nothing?Answer
Because
cdis a shell builtin, not a program on disk.type cdidentifies it. It has to be a builtin: an external program couldn’t change the directory of the shell that launched it. -
With
ls /etc /nonexistent > f.txt, what stays on screen and what goes to the file? And withls /etc /nonexistent > f.txt 2>&1?Answer
In the first, the
/etclisting goes to the file and the/nonexistenterror shows on screen. In the second, both go to the file and nothing shows on screen. -
Why does
sudo echo hello > /etc/testgive “permission denied”, and how do you fix it?Answer
Because your unprivileged shell performs the redirection before running
sudo. Fix it withecho hello | sudo tee /etc/test. -
sort -uandsort | uniqgive the same result, butuniqalone, withoutsort, doesn’t remove all duplicates. Why?Answer
Because
uniqonly compares each line with the previous one: it removes consecutive duplicates. If the input isn’t sorted, separated repeats remain. -
You installed a tool with
go installand get “command not found”. What do you check?Answer
That the binary is in
~/go/binand that the directory is in the PATH. If it isn’t, add it withexport PATH="$PATH:$HOME/go/bin"in your shell’s startup file. -
You catch a reverse shell and pressing
Ctrl+Ccloses it. What’s missing, and how do you get it?Answer
A TTY. Create a pseudo-terminal on the victim (
python3 -c 'import pty; pty.spawn("/bin/bash")'), background it withCtrl+Z, runstty raw -echo; fgin your terminal, and setTERMand the size.