Learn / Stage 2: Linux / Lesson 2.1

$ lesson 2.1 --stage linux

Linux Fundamentals: Theory

Theory first, then the terminal. Understand how Linux is organised, how permissions really work and where it keeps its evidence, and every command you learn afterwards will make sense instead of being memorised.

  • Stage 2 Linux
  • Level Beginner
  • 60 min
  • 7 sections, quiz, lab

Start the lesson Jump to the lab

By the end of this lesson you can

  • Explain what Linux, GNU and a distribution are and pick a distro for a job
  • Navigate the file hierarchy and know what lives in /etc, /var/log, /tmp and /proc
  • Use pipes, redirection and chaining to answer questions from logs
  • Read and set permissions in symbolic and octal form, and explain SUID, SGID and sticky
  • Manage packages, processes and services, and know Linux persistence spots
2.1.01

Why Linux runs the world, and where it came from

If you work in security you will work on Linux. It runs the large majority of web servers and cloud instances, nearly every supercomputer, most routers, smart TVs and CCTV recorders, and the Android kernel on most phones in your college. Most security tools are written for it first.

A short history that explains a lot

  • 1969: Ken Thompson and Dennis Ritchie at Bell Labs create Unix. Its ideas (small tools that do one job, plain text files, everything is a file, a multiuser permission model) still shape Linux.
  • 1983: Richard Stallman launches the GNU project to build a free Unix like system. By the early 1990s it had a compiler, shell and tools, but no finished kernel.
  • 1991: Linus Torvalds, a student in Helsinki, announces a hobby kernel on 25 August. Combined with the GNU tools it became a complete free operating system. The kernel is released under the GPL licence, so anyone can read, change and share it.

Strictly, Linux is only the kernel. What you install is a distribution: the kernel plus GNU tools, a package manager, a desktop or server setup and thousands of programs.

2.1.02

Distributions: which one, and why

Linux distribution families
FamilyExamplesPackage toolWhere you meet it
DebianDebian, Ubuntu, Linux Mint, Kali Linuxapt, .deb filesmost tutorials, many cloud servers, security labs
Red HatRHEL, Fedora, Rocky Linux, AlmaLinux, CentOS Streamdnf, .rpm filesbanks, enterprises, government data centres
ArchArch Linux, Manjaropacmanenthusiasts who like to build their own system
AlpineAlpine Linuxapktiny Docker containers
SUSEopenSUSE, SLESzyppersome enterprises, SAP systems

Ubuntu LTS (long term support) releases come every two years in April and get five years of standard security updates, which is why servers use them. Kali Linux is a Debian based toolkit for penetration testing by Offensive Security. It is excellent inside a lab VM, but it is not meant to be your everyday desktop.

2.1.03

The file hierarchy: everything is a file

Linux has one tree starting at /, laid out by the Filesystem Hierarchy Standard. Learn it once and you can find your way on any distribution, which is exactly what an investigator or attacker does on an unknown server.

Explore the Linux tree

Pick a folder to see what lives there and why security people care.

/

The root of the single directory tree. There are no drive letters; other disks are mounted into folders inside this tree.

/etc

System wide configuration as plain text: users (/etc/passwd), password hashes (/etc/shadow), SSH server settings, sudo rules, scheduled jobs. Attackers read it to learn the system and write to it to persist; defenders watch it for changes.

/home

One folder per user: documents, shell history (~/.bash_history), SSH keys (~/.ssh) and startup files like ~/.bashrc. History files often reveal passwords typed by mistake.

/root

The root user's home folder, separate from /home so it is available even if /home is on a failed disk.

/var/log

Logs. On Debian and Ubuntu auth.log records logins, sudo and SSH; on RHEL family systems it is /var/log/secure. Also syslog or messages, and web server logs. Systemd also keeps a binary journal you read with journalctl.

/tmp

Temporary files, writable by every user, with the sticky bit set so users cannot delete each other's files. Because anyone can write here, attackers often drop tools in /tmp or /dev/shm. Many systems clear it at reboot.

/bin, /usr/bin

Programs (binaries) for all users: ls, cat, bash, python3. On modern distributions /bin is just a link to /usr/bin. /sbin and /usr/sbin hold system admin tools.

/usr

Read only software and data installed by the package manager: programs, libraries (/usr/lib), documentation (/usr/share). Locally built software goes in /usr/local.

/opt

Add on software installed as a whole folder, often third party or commercial tools.

/dev

Device files: /dev/sda is the first disk, /dev/null discards anything written to it. Everything is a file, even hardware.

/proc, /sys

Not real files on disk. The kernel shows live information here: /proc/<PID>/ for each process (its command line, open files, memory map), /proc/cpuinfo, and kernel settings under /proc/sys. Forensics tools read these heavily.

/boot

The kernel (vmlinuz), the initial RAM disk and the boot loader (GRUB) configuration.

/mnt, /media

Mount points for temporary file systems and removable media such as USB drives.

Paths are either absolute, starting from / (/var/log/auth.log), or relative to where you are (../logs). ~ means your home folder, . the current folder, .. the parent. Names are case sensitive: Report.txt and report.txt are different files. Names starting with a dot are hidden from a plain ls.

2.1.04

The shell: talking to Linux

The terminal is the window; the shell is the program inside it that reads your commands. Most systems use bash; macOS and many power users use zsh. A command has a simple shape:

$ ls -la /etc
# command  options  argument
  • Options change behaviour: short ones like -l can be combined (-la); long ones look like --all.
  • The shell finds programs by searching each folder in the PATH variable in order. which ls shows which file will run.
  • Help is built in: man ls, ls --help, and tldr ls if installed.

Pipes and redirection: the real superpower

Every program has three standard streams: input (0, stdin), output (1, stdout) and errors (2, stderr). You can connect them:

Pipes and redirection
SyntaxMeaningExample
a | bsend a's output into b's inputcat auth.log | grep Failed
> filewrite output to a file (overwrites)ls > list.txt
>> fileappend output to a fileecho done >> log.txt
2>/dev/nullthrow error messages awayfind / -name '*.conf' 2>/dev/null
a && brun b only if a succeededsudo apt update && sudo apt upgrade
*, ?wildcards matching file namesls *.log

Chaining small tools is how analysts answer questions fast. This line counts failed SSH logins per IP address from a log:

$ grep 'Failed password' /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn
2.1.05

Users, groups and permissions

Linux is multiuser from the ground up. Users are listed in /etc/passwd (readable by everyone, no passwords inside despite the name), password hashes live in /etc/shadow (readable only by root), and groups in /etc/group.

$ grep student /etc/passwd
student:x:1000:1000:Student,,,:/home/student:/bin/bash
# name : x (hash is in shadow) : UID : GID : comment : home : shell

UID 0 is root, all powerful. Normal users start at 1000 on most distributions; lower numbers are system and service accounts, usually with the shell /usr/sbin/nologin. Modern Ubuntu and Debian store passwords as yescrypt hashes (they start with $y$); older systems used SHA-512 ($6$).

Reading and setting permissions

Breakdown of the permission string -rwxr-x---: owner read write execute, group read execute, others nothing, which is 750-rwxr-x---typeowner: rwx = 7group: r-x = 5others: --- = 0r = 4 w = 2 x = 1 so -rwxr-x--- is chmod 750first character: - file, d directory, l symbolic link
Each file has an owner and a group, and three sets of read, write and execute bits.

For a directory the bits mean something slightly different: r lets you list names, w lets you create and delete files inside, and x lets you enter it and reach files by name.

chmod calculator

Tick what each class of user may do, or type three octal digits. The symbolic form is what ls -l shows you.

Owner (u)
Group (g)
Others (o)

Common:

Special permission bits
Special bitOctalOn a fileOn a directory
SUID4000runs with the file owner's privileges (often root); shown as s in the owner x spotignored on Linux
SGID2000runs with the file's groupnew files inherit the directory's group
Sticky1000no effect todayonly a file's owner can delete it, as on /tmp (shown as t)

sudo lets permitted users run single commands as root, with rules in /etc/sudoers (always edit it with visudo). New files get default permissions from the umask, typically 022 for system accounts and 002 on some desktop setups, giving 644 files and 755 directories.

2.1.06

Packages: installing software safely

Linux installs software from repositories: online collections run by the distribution. Every package is cryptographically signed, and the package manager checks the signature before installing, which is a big security advantage over downloading random installers.

Package commands
TaskDebian and UbuntuRHEL, Fedora, Rocky
Refresh the package listsudo apt updatesudo dnf check-update
Install updatessudo apt upgradesudo dnf upgrade
Install a packagesudo apt install nmapsudo dnf install nmap
Removesudo apt remove nmapsudo dnf remove nmap
Searchapt search wiresharkdnf search wireshark
What owns this file?dpkg -S /usr/bin/sshrpm -qf /usr/bin/ssh

Note that apt update only refreshes the list; apt upgrade actually installs updates. Ubuntu can install security updates automatically through unattended-upgrades. Snap and Flatpak are newer formats that bundle an app with its dependencies.

2.1.07

Processes, services, scheduled jobs and logs

Process commands
Want toCommand
see every processps aux or ps -ef --forest
watch live usagetop or htop
stop a process politelykill PID (sends SIGTERM, 15)
force stopkill -9 PID (SIGKILL, cannot be caught)
see network listeners and their programssudo ss -tulnp
see open files of a processsudo lsof -p PID

Most distributions use systemd as PID 1. It starts and supervises services (units):

$ systemctl status ssh
$ sudo systemctl enable --now ssh
$ systemctl list-units --type=service --state=running
$ journalctl -u ssh --since '1 hour ago'

Scheduled jobs run through cron (crontab -l for your own, /etc/crontab and /etc/cron.d/ for the system) and systemd timers (systemctl list-timers).

student@cwm-lab: Linux theory sandbox

Missions 0/6

    Simulated in your browser. Nothing runs on a real machine.

    Flashcards: do you remember?

    Tap a card to flip it. Try to answer in your head first; recalling is what makes it stick.

    Quick quiz

    Instant feedback on every answer. Score 70% or more to pass this lesson's quiz.

    Q1Which file can every user read but contains no password hashes?

    Q2A script shows -rwxr-x--x. What octal mode is that?

    Q3Why is /tmp interesting during an investigation?

    Q4What does 2>/dev/null do?

    Q5A user's sudo rule allows NOPASSWD: /usr/bin/vim. Why is that dangerous?

    Q6Which is a common Linux persistence location?

    0 / 6

    Hands-on lab

    lab30 minutesyour own machines only

    Lab: theory into muscle memory

    You need: Your Ubuntu VM. The full hands-on lab is lesson 2.2; this one checks the theory.

    1. Walk the tree

      $ cd / && ls
      $ ls -la /etc | head -20
      $ ls /var/log
      $ cat /proc/version

      For each top level folder, say in one line what it holds without looking at this page.

    2. Read the account files

      $ cat /etc/passwd
      $ awk -F: '$3==0' /etc/passwd
      $ sudo head -3 /etc/shadow
      $ getent group sudo

      Count accounts with a real login shell versus nologin. Check that only root has UID 0. Note the $y$ hash prefix.

    3. Audit permissions

      $ find / -perm -4000 -type f 2>/dev/null
      $ sudo -l
      $ ls -ld /tmp

      Write down the SUID programs you find (passwd, sudo, su and mount are normal) and the t at the end of /tmp's permissions.

    4. Check persistence spots on your own VM

      $ crontab -l
      $ ls /etc/cron.d /etc/cron.daily
      $ systemctl list-unit-files --state=enabled | head -30
      $ ls -la ~/.ssh 2>/dev/null

      This is the baseline of a clean machine. Save the output to a file; comparing against a baseline is how you spot changes later.

    You are done when: You can explain the purpose of each top level folder, read a passwd line, convert a permission string to octal, and list where Linux persistence hides.

    Download the lab checklist (.txt)

    Key takeaways

    • Linux is a kernel; distributions turn it into a usable system.
    • /etc for config, /var/log for evidence, /tmp for things that should not be there.
    • Pipes turn small tools into an analysis engine.
    • rwx for owner, group and others; SUID and bad sudo rules are the classic escalation paths.
    • cron, systemd, .bashrc and authorized_keys are where intruders persist.

    Finished the sections, quiz and lab? Mark the lesson complete. Progress is saved only in this browser.