Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 53 ยท Building a Safe Lab

Linux and Command-Line Skills for Security Work

Learn the Linux shell habits security work needs: permissions, processes, logs and text tools like grep, sort and awk, plus a Node stand-in that parses a sample log.

Most servers, most security tools and most attack targets run Linux or something close to it. Whether you defend a company or practice in a lab, you will spend much of your time in a terminal: reading logs, checking who can access a file, finding which process listens on a port, and slicing large text files into answers. Comfort at the command line is not a bonus skill in security. It is the working environment.

This lesson is a security-flavored tour of that environment. It does not replace the full Linux tutorial, which teaches the shell from scratch, and it points to the matching lessons as it goes. The commands in the read-only blocks are meant for a Linux VM in your home lab. The runnable stand-in analyses a small sample log with Node, doing in code what grep, sort and uniq do in a pipeline.

Four habits for security work

Look before you act List, read and check first.
Least privilege Use root only when needed.
Read the logs Evidence lives in text files.
Chain small tools Pipes turn text into answers.
1 / 2

Shell habits that keep you safe

A few habits prevent most costly mistakes:

  • Work as a normal user. Switch to administrator rights with sudo for one command at a time, and read the command before pressing Enter. A typo as root can erase a system.
  • Preview destructive commands. Run ls with the same pattern before rm. Use rm -i when learning.
  • Quote variables and paths ("$file"), because spaces and special characters change what a command does. Quoting explains the rules.
  • Record what you did. Keep notes with timestamps; history and the script command help. In real engagements, your command log is evidence.
  • Know where you are. pwd, whoami and hostname take a second and prevent running the right command on the wrong machine. Many incidents happen because someone typed a command into the production terminal instead of the test one.

Permissions and ownership

Every file has an owner, a group and permission bits for owner, group and others. The listing from ls -l shows them:

TEXT
-rw-r----- 1 alice staff  1204 Mar  2 10:14 report.txt
drwxr-xr-x 2 root  root   4096 Mar  1 09:00 /etc/ssl
-rwsr-xr-x 1 root  root  59976 Jan 18  2024 /usr/bin/passwd

The first character is the type (- file, d directory, l link); the next nine are rwx for owner, group, others. Read means view, write means change, execute means run (or, for a directory, enter). Octal notation sums the bits: r=4, w=2, x=1, so 640 is rw-r-----. The details are in File Permissions and Users and Permissions.

For security, a few patterns matter:

  • World-writable files (-rw-rw-rw-) let any user change them. A world-writable script run by root is an escalation path.
  • Private keys and secrets should be 600 (owner only). SSH refuses a private key that others can read.
  • The s in -rwsr-xr-x is the setuid bit: the program runs with its owner's rights, here root. Setuid programs must be carefully written, so auditing them is a standard hardening task (more in Privilege Escalation Concepts and OS Hardening).

Commands for auditing your own lab machine look like this (read-only here):

TEXT
ls -l /etc/shadow
find / -xdev -type f -perm -0002 2>/dev/null
find / -xdev -type f -perm -4000 2>/dev/null
stat -c '%U %a %n' ~/.ssh/id_ed25519

The first checks that the password hash file is not readable by ordinary users; the find commands list world-writable files and setuid programs on the machine's main filesystem. Run these only on systems you own.

Processes and listening ports

A running program is a process. To understand a machine you ask which processes run, as which user, and what they listen on:

TEXT
ps aux --sort=-%cpu | head
ps -eo pid,user,comm | grep ssh
ss -tulpn

ss -tulpn lists TCP and UDP sockets that are listening, with the owning process (needs elevated rights to show other users' processes). Every listening port is an attack surface: a door someone could knock on. A fresh server should listen only on what it needs. If you find an unexpected listener, find its process, find which package or script started it, and decide whether to remove it. Processes and Network Troubleshooting cover the tools in more detail.

Logs: where the evidence lives

Linux records events as text. Typical places are /var/log/auth.log (Debian-family) or /var/log/secure (Red Hat-family) for logins and sudo, web server logs under /var/log/nginx/ or /var/log/apache2/, and the systemd journal read with journalctl. See Logging with journald.

TEXT
sudo tail -f /var/log/auth.log
sudo journalctl -u ssh --since "1 hour ago"
sudo grep "Failed password" /var/log/auth.log | wc -l

A burst of "Failed password" lines from one address is the classic sign of a password-guessing attempt. You will build detection like this in Logging, Monitoring and SIEM Basics.

Text tools: turning logs into answers

Security data is mostly text, and a handful of tools do most of the analysis:

Tool Job Example
grep Keep lines that match grep "Failed password" auth.log
awk Pick fields, do arithmetic awk '{print $11}' auth.log
sort Order lines sort -rn
uniq -c Count identical neighbors sort | uniq -c
cut Cut columns cut -d' ' -f1
wc -l Count lines grep ... | wc -l
head, tail First or last lines tail -n 20

The pattern "filter, extract, sort, count, sort again" answers an enormous number of questions. For example, "which addresses failed to log in most often?" is:

TEXT
grep "Failed password" auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn | head

Read it left to right as a pipeline: keep the failures, take the field that holds the address, group identical addresses, count them, and show the biggest. The exact field position depends on the log format, which is why you look at a few real lines first. The lessons on grep, pipes and awk go deep on each piece.

The site's Bash runner exists for practice with these tools, but real system logs do not exist in this sandbox, so the next model uses an invented log embedded in the program. Every address uses the documentation ranges (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24) reserved for examples.

Stand-in: analyzing a sample log

The Node program below does what the pipeline above does: it filters failed logins, extracts the address and user, counts per address and reports the top offenders and the users they tried. It runs on the server-side runner (Node 24), parsing text with a regular expression. It is a model of the pipeline's logic, not a replacement for your terminal skills.

Node.js Count failed logins per address

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 node:readline or process.stdin), it gets the next line from this box.

Look at what the output tells an analyst. One address tried several user names, including root and admin, many times: a guessing attempt. Another tried the made-up users test and guest: scripted probing of common names. The address of alice failed once and then succeeded, which is probably a typo and not an attack. A human still decides; the tool only surfaced the pattern.

Stand-in: a permissions audit

Here is the same idea for permissions. The Node model reads a few lines in ls -l style and flags the risky ones: world-writable files, setuid files outside a known list, and secrets that others can read.

Node.js Flag risky permissions

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 node:readline or process.stdin), it gets the next line from this box.

The stand-in reads text, but the real task is the same: ask a question of many files at once. In a real audit you would generate this listing with find and ls -l, from your own machine.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is it safer to work as a normal user and use sudo for single commands?

    Least privilege limits the blast radius of mistakes and compromises.
  2. 2. What does the s in -rwsr-xr-x mean?

    Setuid programs run with the file owner's rights, so unexpected ones are an audit priority.
  3. 3. Which pipeline finds the most frequent lines in a file?

    Sorting groups identical lines, uniq -c counts them, and a second numeric reverse sort puts the largest counts first.
  4. 4. Why look at a few real log lines before writing a field-extraction command?

    awk fields and regular expressions depend on the exact format, and guessing leads to silently wrong results.

Summary

  • Security work happens in the terminal: use least privilege, preview destructive commands, quote variables and keep notes.
  • Permission bits and ownership show who can read, change or run a file; world-writable files, setuid programs and readable private keys deserve scrutiny.
  • Every listening port is attack surface; list processes and sockets and justify each one.
  • Logs are text evidence, from auth.log to the journal.
  • grep, awk, sort and uniq -c turn logs into answers, and the same logic can be done in code.
  • Run audits only on machines you own.

Next: Networking Essentials for Security.