Notes
The problem
Imagine a computer with no operating system. Every program would have to drive the disk, the network card and the memory itself. One buggy program could overwrite another's memory, or the whole disk. Nothing would decide who gets the CPU, and nothing would enforce permissions. An operating system exists to manage shared hardware fairly and to protect programs, and users, from each other.
- every program would drive the disk, network card and screen itself
- one bug could overwrite another program's memory
- no way to share the CPU
- no permissions
- an OS manages and protects
The kernel
At the centre is the kernel, the one program with full control of the machine. Linux is, strictly speaking, just this kernel. It schedules processes on the CPU, hands out memory, manages files and disks, runs the network stack, talks to devices through drivers, and enforces users and permissions. It runs in a privileged CPU mode called kernel mode, where any instruction is allowed.
- processes: who runs, when memory: who owns which bytes
- files and disks
- networking: sockets, packets
- devices: drivers
- security: users, permissions
- runs in kernel mode (privileged CPU mode)
User space
Everything else lives in user space: the shell, ls, nginx, Node, your application. These programs run in user mode, where the CPU refuses dangerous instructions. A process can only see its own memory, so a crash kills one process, not the machine. This split is why a misbehaving app can usually be killed and restarted without rebooting, which is something you will do often in this job.
- every other program: shells, ls, nginx, node, your app
- runs in user mode: cannot touch hardware or other programs' memory
- each process sees only its own memory
- a crash kills one process, not the machine
System calls
So how does a program open a file or send a packet? It asks the kernel, with a system call. The CPU switches into kernel mode, the kernel checks the request, does the work, and returns a result. There are a few hundred system calls: openat, read, write, mmap, clone, execve, socket, connect, kill. Programs usually reach them through the C library, glibc, so when code calls open, glibc makes the openat system call underneath.
- the program asks; the kernel checks and does the work
- a few hundred on x86-64: openat, read, write, close, mmap, clone, execve, socket, connect, accept, kill
- usually through the C library (glibc), not called directly
Watch it: ls
Let's watch. strace runs a program and prints every system call it makes. With the summary option on ls, listing the root folder made seventy one system calls in our run on Ubuntu twenty four point zero four. Seventeen mmaps, mostly to load shared libraries into memory, eight closes, seven fstats, six openats. Six calls failed, and that is fine: ls looks for optional files, like translation catalogues, that are not there.
calls syscall 17 mmap 8 close 7 fstat 6 openat ... 71 total, 6 errors the errors: files ls looks for and does not need
Watch it: cat
Now the simplest program there is. cat of the hostname file. It opens the file and gets back the number three, a file descriptor, the kernel's ticket for that open file. It reads up to a hundred and thirty one thousand bytes and gets three: v, m, and a newline. It writes those three bytes to file descriptor one, standard output. It reads again, gets zero, meaning end of file, and exits with status zero. That is all cat really is.
openat(AT_FDCWD, "/etc/hostname",
O_RDONLY) = 3
read(3, "vm\n", 131072) = 3
write(1, "vm\n", 3) = 3
read(3, "", 131072) = 0
+++ exited with 0 +++
3 = a file descriptor
1 = standard output Why DevOps cares
Why does this matter for DevOps? Because the errors you meet every day are system call errors. Permission denied is EACCES. No such file or directory is ENOENT. Too many open files is EMFILE. Address already in use comes from bind, and connection refused from connect. When an app's own error message is vague, strace shows exactly which call failed, on which path, with which error. Part one will use it again and again.
- Permission denied = EACCES
- No such file = ENOENT
- Too many open files = EMFILE
- Address already in use = EADDRINUSE on bind()
- Connection refused = ECONNREFUSED on connect()
- strace shows which call failed
Kernel and distro
One more distinction. Linux is only the kernel. What you install on a server is a distribution: the kernel plus a C library, an init system such as systemd, a shell and the core tools, and a package manager like apt or dnf. Ubuntu, Debian, Red Hat Enterprise Linux and Alpine are distributions. Alpine, popular in containers, uses a smaller C library called musl, which occasionally changes how programs behave.
- kernel: Linux 6.x
- + C library: glibc or musl
- + init system: systemd
- + shell and tools: bash, coreutils
- + package manager: apt, dnf
- = a distribution: Ubuntu, Debian, RHEL, Alpine
Common mistakes
Mistakes. Calling Ubuntu Linux, when Linux is the kernel and Ubuntu is a distribution. Reading only an app's vague error, never the system call underneath. Thinking root skips all checks; the kernel still checks capabilities and security modules. Forgetting that containers share the host's kernel, which part six explains. And blaming hardware before reading what the kernel said.
- "Linux" = Ubuntu: it is the kernel, Ubuntu is a distro
- reading only the app's error, never the system call
- thinking root can do anything without the kernel checking
- forgetting containers share the host's kernel
- blaming hardware first
Say it like this
In the interview: the kernel runs in a privileged mode and manages the CPU, memory, files, network and devices. Programs run in user space and must ask the kernel for anything, through system calls like openat, read, write, clone and execve, usually via glibc. When a program fails with a vague error, I run strace to see exactly which call failed, on what path, and with which error code.
- "The kernel runs privileged and manages hardware; programs run
- in user space and must ask it
- for anything, through system
- calls like openat, read, write, clone and execve. When a tool
- fails vaguely, I strace it to see which call failed and why."