Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 31 ยท The Kali Environment

Command Line Essentials for Security Work

The shell skills security work leans on: pipes, grep and awk on tool output, file permissions, sudo, services and redirection, with a runnable filter.

Security tools produce text: scan results, logs, hashes, captured headers. The people who work fastest are the ones who can filter and reshape that text on the command line. You do not need to become a shell wizard, but a handful of skills carry most of the weight.

This lesson is a focused refresher for security work. Each topic has a full treatment in the Linux tutorial, linked below, so use this page as a map and go deeper where you feel shaky. The runnable stand-ins at the end reproduce the filtering logic in Python on sample tool output, because the real shell commands need a Kali system.

Five shell skills you will use daily

Pipes Feed one tool's output into the next.
grep and awk Pick lines and columns out of output.
Permissions Know who can read, write and run.
sudo Use privilege only when you need it.

Saving results to files comes with redirection.

1 / 2

Pipes and redirection

A pipe sends the output of one command into the next. Redirection sends output to a file. Together they let you build small pipelines. See Pipes for the details.

Terminal
# Read-only here. Typical shapes of pipelines:
ls /etc | wc -l                         # count entries
cat scan.txt | grep open | sort | uniq  # filter, sort, remove duplicates
some-tool > results.txt                 # overwrite a file with output
some-tool >> results.txt                # append
some-tool 2> errors.txt                 # only error messages
some-tool > all.txt 2>&1                # both streams to one file
some-tool | tee results.txt             # see output and save it

tee is handy in security work: you watch the output live and also keep a record. Always save raw tool output; you will need it as evidence later.

grep: find the lines that matter

grep prints lines that match a pattern. Common options:

Option Meaning
-i Ignore case
-v Invert: show lines that do not match
-E Extended regular expressions
-c Count matching lines
-n Show line numbers
-r Search directories recursively
Terminal
grep -i "failed password" /var/log/auth.log
grep -c "open" scan.txt
grep -E "22/tcp|80/tcp|443/tcp" scan.txt
grep -rn "password" ./config-backup/

More in grep and Regular expressions.

awk: pick columns and count

awk splits each line into fields. Use it when the useful value is in a column.

Terminal
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head    # top client addresses
awk '$9 == 404 {print $7}' access.log                             # paths that returned 404
awk -F: '{print $1}' /etc/passwd                                  # first field, colon separator

The pattern sort | uniq -c | sort -rn | head is the classic "top N" idiom. Learn it once and use it constantly. Details live in awk.

Permissions and ownership

Security work often comes down to "who can read this file?" Every file has an owner, a group and three permission sets (user, group, others), each with read, write and execute.

Terminal
ls -l /etc/shadow /etc/passwd /tmp
# -rw-r----- 1 root shadow 1450 ... /etc/shadow   (not readable by ordinary users)
# -rw-r--r-- 1 root root   2870 ... /etc/passwd   (readable by everyone)

chmod 600 secrets.txt      # owner read/write only
chmod 700 scripts/         # owner full access, nobody else
chown user:group file      # change owner and group
umask                      # default permissions mask for new files

World-writable files (permission ending in w for others) and unexpected SUID binaries are classic findings in a hardening review. See Permissions for the full model.

sudo: privilege with a paper trail

sudo runs one command with the privileges of another user, normally root, and logs it. Prefer it to a root shell.

Terminal
sudo whoami                 # root
sudo -l                     # what am I allowed to run?
sudo less /var/log/auth.log

Habits worth keeping: type the full command rather than sudo !! on autopilot, never paste commands you do not understand into a root shell, and remember that many scanning features need root only for raw packets. If a tool works without root, run it without root. Policy details are in PAM and sudoers.

Services

Kali ships with network services switched off by default, so you start only what you need. Many lab tasks (a web server for a practice target, an SSH service) use systemd:

Terminal
sudo systemctl start ssh
systemctl status ssh
sudo systemctl stop ssh
sudo systemctl enable ssh      # start at boot (usually not wanted on a security workstation)
ss -tlnp                       # which programs are listening on which ports?

ss -tlnp is a defender's first question: what is listening on this machine? Start services only for the time you need them and stop them afterward. Review Packages and services and Network configuration.

Stand-in: filtering sample tool output

Imagine a scan result file like this. It is fake data in a made-up format, similar in spirit to what scanners print.

Python Filter, count and rank sample output

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each Python line matches one pipeline stage from earlier, which is a good way to understand what the shell is doing under the hood.

Stand-in: finding risky permissions

The second stand-in reads a sample ls -l-style listing and flags world-writable files, a typical hardening check.

Python Flag world-writable files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

World-writable files and SUID programs are not always wrong (/tmp should be world-writable with the sticky bit), but each one deserves a deliberate reason.

Desktop and terminal shortcuts

The default Kali desktop is Xfce. These shortcuts save time in a lab session:

Keys Action
Ctrl + Alt + T Open a terminal
Ctrl + Alt + D Show the desktop
Alt + F4 Close the window
Alt + Tab Switch window
Alt + F7 Move the window (then use the arrow keys)
Alt + F8 Resize the window
Alt + F10 Maximize or restore the window
Ctrl + Alt + Left/Right Switch workspace
Ctrl + Alt + Shift + Left/Right Move the window to another workspace

In the shell itself: Ctrl + C stops a program, Ctrl + Z suspends it, Ctrl + L clears the screen, Ctrl + R searches history, Ctrl + A / Ctrl + E jump to line start and end, !! repeats the last command and !command repeats the most recent command starting with that word (for example !ping).

Network and system commands you will meet

Read-only reference; run them on your own lab machine. The table covers the options most often needed.

Terminal
curl -s https://lab.example.test/           # -s: silent, no progress meter
curl -u user:pass https://lab.example.test/api   # basic authentication (lab credentials only)
curl --limit-rate 100k -O https://lab.example.test/big.iso   # cap the download speed
curl -T notes.txt ftp://lab.example.test/upload/   # upload a file (-T)
curl --libcurl demo.c https://lab.example.test/    # write C code that repeats this request
curl -I https://lab.example.test/           # headers only

sudo route add -net 10.10.0.0 netmask 255.255.0.0 gw 192.168.1.1 dev eth0 metric 10
route -n                                    # show the routing table without name lookups
sudo ifup eth0                              # bring an interface up (--force ignores its state)
sudo ifdown eth0

hostnamectl status                          # static, transient and pretty names
sudo hostnamectl set-hostname kali-lab --static
sudo hostnamectl set-hostname "Kali Lab" --pretty
hostnamectl set-chassis vm                  # laptop, desktop, server, vm ...

ipcs -a                                     # list shared memory, queues, semaphores
ipcrm -m 32768                              # remove shared memory by id (-M by key)
ipcrm -q 12                                 # message queue by id (-Q by key)
ipcrm -s 7                                  # semaphore by id (-S by key)

Three name kinds exist: the static name (stored in /etc/hostname), the transient name handed out by DHCP or mDNS, and the pretty free-form name. curl --libcurl is handy for turning a working command into program code. The route line reads: destination network, netmask, next-hop gw, dev interface and metric (lower is preferred); the modern equivalent is ip route add, see Network Configuration.

Ownership options worth knowing, all read-only here:

Terminal
sudo chown alice:staff report.txt           # owner and group
sudo chown :staff report.txt                # group only
sudo chown -R alice /srv/lab                # recursive
sudo chown --from=bob alice report.txt      # change only if the current owner is bob
sudo chown --reference=template.txt new.txt # copy owner and group from another file
sudo rm -r --one-file-system /mnt/scratch   # do not cross into other mounted filesystems

rm --no-preserve-root removes / and is never appropriate; modern rm refuses to do it by default for good reason.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command saves a tool's output to a file and also shows it on screen?

    tee copies its input to the screen and to a file. A plain > redirect hides the output from the screen.
  2. 2. What does grep -v open do?

    -v inverts the match, so lines without the pattern are printed.
  3. 3. Why prefer sudo for one command over a permanent root shell?

    sudo applies privilege to a single command and records it, which reduces accident risk and helps auditing.
  4. 4. What does ss -tlnp help you answer?

    ss -tlnp lists listening TCP sockets with numeric ports and the owning processes, a core exposure check.

Summary

  • Pipes and redirection chain small tools; tee keeps raw output as evidence.
  • grep selects lines, awk selects columns, and sort | uniq -c | sort -rn | head ranks them.
  • Permissions answer "who can read, write or run this", and world-writable or SUID items need a reason.
  • Use sudo for single commands and run tools without root when possible.
  • Services are off by default on Kali; start them deliberately, check with ss -tlnp, and stop them afterward.

Next up: Reconnaissance: Passive vs Active.