DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · INTERNALS · 8 OF 187

Linux Kernel vs User Space and System Calls

What an operating system really does, shown with strace on Ubuntu 24.04: the kernel and user space, why programs cannot touch hardware directly, system calls as the only door between them (ls / made 71 of them in our run, 6 of which failed harmlessly), what cat /etc/hostname asks the kernel for, the C library in between, and why every DevOps debugging tool starts here.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Kernel: privileged, manages hardware
  • user space: limited
  • syscalls: the one door
  • strace shows them

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.

Without an OS
  • 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.

The Linux kernel manages
  • 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.

User space
  • 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.

A system call
  • 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.

strace -c ls / (our run)
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.

strace cat /etc/hostname
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.

Familiar errors, decoded
  • 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.

What you actually install
  • 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.

Avoid these
  • "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.

In the interview
  • "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."

Interview questions

Q1What is the difference between kernel space and user space?

The kernel runs in a privileged CPU mode with full control of hardware and memory; user-space programs run in a restricted mode, can only see their own memory, and must ask the kernel for resources through system calls.

Q2What is a system call?

The interface through which a user-space program asks the kernel to do privileged work, such as openat, read, write, mmap, clone, execve, socket or kill; the CPU switches to kernel mode, the kernel checks and performs it, and returns a result or error.

Q3What is strace and when do you use it?

A tool that traces the system calls (and signals) of a process. Use it when a program fails with a vague error or hangs: it shows which call failed, on which file or address, with which errno.

Q4What is a file descriptor?

A small integer the kernel returns when a process opens a file, socket or pipe; later calls like read and write use it. 0, 1 and 2 are standard input, output and error.

Q5What does EACCES, ENOENT and EMFILE mean?

Permission denied, no such file or directory, and too many open files for the process (the file-descriptor limit).

Q6Is Linux an operating system or a kernel?

Strictly the kernel. A distribution such as Ubuntu or RHEL adds a C library, init system, shell, tools and a package manager to make a full operating system.

Q7What role does glibc play?

The C library provides functions like open, printf and malloc and turns them into system calls; Alpine uses musl instead, which can change behaviour such as DNS resolution.

Q8Do containers have their own kernel?

No. Containers are processes on the host kernel, isolated with namespaces and cgroups; virtual machines run their own kernel.