Ethical Hacking & Cyber Defense · Unit 3 of 8
Unit 3: Linux and the command line
The shell skills every security job uses: files, users and permissions, processes, services and logs, and fast log searches with grep, pipes and scripts.
Opens the first lesson of this unit in Ethical Hacking & Cyber Defense. Every lesson is open from the start.
Lesson 1: Finding your way around the shell
Goal: Move around a Linux file system and read files with the core commands.
Most servers on the internet run Linux, and most security tools are built for it, so comfort at the command line is a core job skill for both testers and defenders. The shell is the program that reads the commands you type. You do not need to memorize hundreds of commands: about twenty cover most daily work, and every command has a manual page you can read with man.
| Command | What it does | Example |
|---|---|---|
pwd | Print working directory: where am I? | pwd → /var/www/reports |
ls -la | List all files, including hidden ones, with details | ls -la /etc |
cd | Change directory | cd /var/log |
cat / less | Show a file / page through a long file | less auth.log |
head / tail -f | First lines / last lines, following new ones live | tail -f /var/log/syslog |
find | Search for files by name, size, owner or age | find /home -name "*.sh" |
man | Read the manual for a command | man ls |
Linux paths start at the root, /. A few folders matter constantly in security work: /etc holds configuration files, /var/log holds logs, /home holds users' files, /tmp is a scratch area anyone can write to, and /root is the administrator's home folder. Files whose names start with a dot, like .bash_history, are hidden from a plain ls but shown with ls -a, and attackers and defenders both know to look there.
Worked example: checking what changed recently
- You are investigating a server that started behaving strangely yesterday.
find /etc -mtime -1 -type flists configuration files modified in the last day.- Two files changed: the user list (/etc/passwd) and the SSH server settings. Neither change was in a change ticket, so both need a closer look.
$ cd /etc
$ find /etc -mtime -1 -type f
/etc/passwd
/etc/ssh/sshd_configA single find narrowed thousands of files to two suspects. Small commands, used with a clear question, are powerful.
- Shell
- The program that reads and runs the commands you type at a command line, such as Bash.
- Root directory
- The top of the Linux file system, written as a single slash: /.
- /var/log
- The Linux folder where system and application logs are usually stored.
- Hidden file
- On Linux, a file whose name starts with a dot; shown only with ls -a.
Remember: pwd, ls -la, cd, less, tail -f, find, man. Twenty commands carry most of the job.
Lesson 2: Users, groups and permissions
Goal: Read and set Linux file permissions in letters and numbers, and explain least privilege and why sudo and SUID files matter.
Every Linux file has an owner, a group and permissions for three classes of people: the owner (u), the group (g) and everyone else (o). Each class can have read (r), write (w) and execute (x) permission. ls -l shows them as ten characters, such as -rwxr-x---: a file type character, then three letters each for owner, group and others.
Permissions are also written as numbers. Read is 4, write is 2 and execute is 1, and you add them for each class. So rwx = 4+2+1 = 7, r-x = 4+0+1 = 5 and r-- = 4. The three digits are owner, group, others: chmod 755 script.sh gives rwxr-xr-x. chmod 600 secrets.txt gives rw-------: only the owner can read or change it, which is right for private keys and password files.
| Number | Letters | Typical use |
|---|---|---|
| 755 | rwxr-xr-x | Programs and scripts everyone may run but only the owner may change |
| 644 | rw-r--r-- | Ordinary files everyone may read |
| 600 | rw------- | Private keys, password files: owner only |
| 777 | rwxrwxrwx | Anyone can change it: almost always a security finding |
Least privilege means every user and program gets only the access it needs. The root account can do anything, so admins use sudo to run single commands as root instead of logging in as root all day. Two classic weaknesses come from ignoring least privilege: users allowed to run powerful programs through sudo without need, and programs with the SUID bit set, which run with their owner's rights (often root) no matter who starts them. Auditors list both and ask “does this really need to be this powerful?”
Worked example: decoding a risky permission
- The script is owned by root and runs every night as root to make backups.
- The permissions are rwxrwxrwx (777): anyone on the system can edit it.
- Any user could add their own commands to the script, and root would run them at 3 a.m. That is a privilege escalation path.
- The fix:
chmod 755(or stricter) so only root can change it, and review who can log in to the server at all.
$ ls -l /opt/backup/run-backup.sh
-rwxrwxrwx 1 root root 812 Nov 2 03:00 run-backup.shOne wrong number turned an ordinary backup job into a way for any user to become root.
- Least privilege
- Giving each user and program only the access it needs to do its job, and no more.
- chmod
- The Linux command that changes a file's permissions, for example chmod 640 report.txt.
- sudo
- A command that lets an approved user run a single command with administrator (root) rights.
- SUID
- A permission bit that makes a program run with its owner's rights, often root, whoever starts it.
- root
- The all-powerful administrator account on Linux and other Unix-like systems.
Remember: r=4, w=2, x=1; add per class. 600 for secrets, 777 is a red flag.
Lesson 3: Processes, services and logs
Goal: Find running processes, listening services and the logs that record logins and errors.
A process is a running program. ps aux lists every process with its owner and the command that started it, and top shows them live, sorted by CPU use. Analysts look for things that do not belong: a process with a random name running from /tmp, a program using all the CPU at 3 a.m. (a classic sign of cryptocurrency mining malware), or a shell started by the web server account.
A service (or daemon) is a process that runs in the background and usually listens on a port. On most modern Linux systems, systemctl manages services: systemctl status ssh shows whether SSH is running and its recent log lines. To see which ports are listening and which program owns each one, use ss -tulpn. Comparing that list with what *should* be running is one of the fastest ways to spot a backdoor or a forgotten service.
$ sudo ss -tulpn
Netid State Local Address:Port Process
tcp LISTEN 0.0.0.0:22 users:(("sshd",pid=812))
tcp LISTEN 0.0.0.0:443 users:(("nginx",pid=1044))
tcp LISTEN 0.0.0.0:4444 users:(("python3",pid=3391))
Logs are the memory of a system. On Debian and Ubuntu, logins, sudo use and SSH activity go to /var/log/auth.log (Red Hat-style systems use /var/log/secure). General system messages go to /var/log/syslog or the systemd journal, read with journalctl. Web servers keep access and error logs, often under /var/log/nginx or /var/log/apache2. Because attackers often try to delete logs, mature teams send copies to a central server as they are written.
Worked example: the unexpected listener
ss -tulpnon a web server shows SSH on 22 and the web server on 443, both expected.- It also shows python3 listening on port 4444, started from a file in /tmp. Nobody on the team set that up.
ps aux | grep 3391shows the full command, and auth.log shows a login from an unfamiliar address shortly before the process started.- The analyst does not kill the process yet: they follow the incident response plan, preserve evidence and escalate.
Comparing “what is listening” with “what should be listening” turned up a likely intruder in under a minute.
- Process
- A running instance of a program, with its own ID (PID) and owner.
- Daemon
- A background service process on Linux, such as sshd or nginx.
- ss -tulpn
- A Linux command that lists listening TCP and UDP ports and the programs that own them.
- auth.log
- The Debian and Ubuntu log file that records logins, sudo use and other authentication events.
Remember: ps aux for what runs, ss -tulpn for what listens, auth.log for who logged in.
Lesson 4: Searching logs fast: grep, pipes and loops
Goal: Combine grep, pipes and sort to answer questions from logs, and read a simple Bash loop.
Logs can have millions of lines, so analysts search them with grep, which prints only the lines that match a pattern. grep "Failed password" /var/log/auth.log shows every failed SSH login. Useful options: -i ignores case, -v shows lines that do *not* match, -c counts matches, and -E allows extended patterns.
The pipe symbol | sends one command's output into the next. That is how small tools combine into powerful answers. The classic analyst pipeline is “filter, cut, sort, count”: keep the lines you care about, cut out the interesting field, sort it so duplicates sit together, count each unique value with uniq -c, and sort the counts from highest to lowest.
Worked example: which address is guessing passwords?
grep "Failed password"keeps only failed logins.grep -oE "from [0-9.]+"keeps just the “from address” part of each line.sort | uniq -cgroups identical addresses and counts them;sort -nrputs the biggest count first;head -3shows the top three.
$ grep "Failed password" /var/log/auth.log | grep -oE "from [0-9.]+" | sort | uniq -c | sort -nr | head -3
1874 from 203.0.113.77
12 from 198.51.100.4
2 from 192.0.2.30203.0.113.77 failed 1,874 times: a password-guessing attack. The next question is whether any login from that address ever succeeded (search for “Accepted password”).
When the same task must be repeated, a short Bash script saves time and mistakes. A for loop runs commands once per item. Scripts like the one below are common in security teams for quick checks, and reading them is a skill you will use from day one, including when you review scripts attackers left behind.
#!/bin/bash
# Count failed logins for each log file in the archive
for f in /var/log/auth.log*; do
echo "$f: $(grep -c "Failed password" "$f")"
done
- grep
- A command that prints the lines of text that match a pattern.
- Pipe
- The | symbol, which sends the output of one command into the input of the next.
- uniq -c
- A command that collapses repeated adjacent lines and counts each one; usually used after sort.
- Bash script
- A text file of shell commands that runs them in order, with loops and conditions.
Remember: Filter, cut, sort, count: grep | grep -o | sort | uniq -c | sort -nr.
Practise it in Ethical Hacking & Cyber Defense
Each lesson ends with three questions that check you can use the idea, not just recognise it. Wrong answers explain why and come back in Review. The unit ends with a 6-question quiz; pass it with 80% or more to master the unit.
Every lesson is free: no ads, no lives and no paywall.