Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 95 ยท Getting Started

Linux Architecture: Kernel, Subsystems, Libraries and Shells

See how Linux is built: kernel types, the scheduler, memory manager, VFS, networking and IPC, user space versus kernel space, system libraries and the shell family.

When you type ls, a surprising amount of machinery wakes up. A shell reads your line, a C library prepares a request, the processor switches into a privileged mode, the kernel walks a file system, and the result travels back up the same stack. You can use Linux for years without thinking about these layers, but knowing them makes error messages, performance numbers and security advice suddenly make sense.

This lesson draws the map. You will learn what the kernel does and which subsystems it contains, how user space and kernel space are kept apart, what a system call is, where the C library fits, and how the family of shells (sh, bash, ksh, zsh, fish and friends) relates. The examples inspect a real kernel through /proc, so you see the layers in action.

The layers from top to bottom

Linux is organized as a stack in which each layer talks only to its neighbours:

TEXT
  Applications and scripts      (your code, ls, grep, a web server)
  ------------------------------------------------------------
  Shell                         (bash: reads and starts commands)
  ------------------------------------------------------------
  System libraries              (glibc or musl: wrappers for the kernel)
  ============  system call interface  ======== user space above
  Linux kernel                  (scheduler, memory, VFS, network, drivers)
  ------------------------------------------------------------
  Hardware                      (CPU, RAM, disks, network cards)

The double line is the most important border in the whole system. Everything above it is user space. Everything below it is kernel space.

User space and kernel space

The processor can run in at least two privilege levels. In the restricted one, a program can use its own memory and do arithmetic, but it cannot touch hardware, cannot read another program's memory and cannot change the page tables that define memory. The kernel runs in the privileged level and can do all of that.

This split gives you two things:

  • Protection. A buggy program crashes only itself. The kernel notices the illegal access, stops that process (a "segmentation fault") and carries on.
  • Control. Every access to a disk, a network card or another process is checked by the kernel against permissions. That is why file permissions can actually be enforced.

A program crosses the border with a system call: a controlled request such as open, read, write, fork, execve, mmap or kill. Linux offers a few hundred of them. You almost never call one directly. You call a library function such as fopen() or printf(), and the library performs the system call for you.

Bash Looking at the kernel from user space

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 read or cat), it gets the next line from this box.

Nothing in that script is special: awk, grep and cat are ordinary programs. The kernel builds /proc on the fly when they read it, which is why the files have size zero on disk but always show current numbers. You met this idea in The Linux Filesystem and Processes.

What is inside the kernel

The kernel is one large program, but it is organized into subsystems with different jobs:

Subsystem Job You can see it through
Process scheduler Decides which task runs on which CPU and for how long ps, top, nice, /proc/PID/sched
Memory manager Gives each process a private virtual address space, handles swap and the page cache free, vmstat, /proc/meminfo, /proc/PID/maps
Virtual File System (VFS) One interface for ext4, XFS, tmpfs, NFS and many more mount, df, /proc/filesystems
Network stack Sockets, TCP/IP, routing, firewall hooks ip, ss, /proc/net
Inter-process communication Pipes, signals, shared memory, Unix sockets, semaphores kill, ipcs, /proc/PID/fd
Device drivers Talk to disks, graphics cards, USB, sound lsmod, /dev
Security Permissions, capabilities, namespaces, cgroups, SELinux/AppArmor id, getcap, /proc/PID/ns

The VFS deserves a closer look. It lets cat read a file the same way no matter where the bytes live: on a local disk, in RAM, on a network server or inside the kernel itself (/proc). Each file system type implements the same small set of operations, and the VFS dispatches to it.

Monolithic kernel, microkernel and hybrid

Kernel designs fall into three groups:

  • Monolithic kernel. All the subsystems and most drivers run in kernel space as one program. Calls between them are cheap function calls, which makes it fast. A driver bug, however, can bring down everything. Linux is monolithic.
  • Microkernel. Only the bare minimum (scheduling, memory, messaging) runs in kernel space. File systems, drivers and networking run as separate user-space servers. It is more robust and easier to isolate, but every message between servers costs time. MINIX and QNX follow this model.
  • Hybrid kernel. A microkernel-style structure with extra components moved back into kernel space for speed. Windows NT and macOS (XNU) are usually described this way.

Linux softens the monolithic design with loadable kernel modules: drivers and file systems that can be loaded and unloaded while the system runs, using modprobe, insmod, rmmod and lsmod. A module runs in kernel space, with full privileges, so modules should be treated as seriously as the kernel itself.

System libraries

Between the programs and the kernel sits the C library. On most distributions that is glibc; Alpine Linux uses the smaller musl. The library does three jobs:

  1. It wraps system calls in friendly functions (write(), fork()).
  2. It adds services that are not system calls at all: buffered I/O (printf), string handling, malloc, locale support, DNS lookups.
  3. It loads shared libraries at start-up. A dynamically linked program such as ls is only a small file that says "I need libc, please map it in", which saves disk space and memory because many programs share one copy.

You can list what a program needs with ldd /bin/ls. This also explains a classic failure: a program built against a newer glibc will not start on an older system, and a static binary (all libraries inside) avoids the problem at the cost of size.

The shell

The shell is a user-space program that reads command lines, expands them (wildcards, variables, quotes), starts programs with fork and exec, wires up pipes and redirections, and offers a small programming language. It is not part of the kernel, and you can swap it without changing the system. Your login shell is stored in the last field of your line in /etc/passwd.

The main shells, in rough historical order:

Shell Program Character
Bourne shell sh The original (1979). Minimal, great for scripts, no comfort features.
C shell, tcsh csh, tcsh C-like syntax, history and job control. Pleasant to type, awkward for scripts.
Korn shell ksh Combines Bourne scripting with csh comforts. Many ideas later landed in Bash.
Bash bash "Bourne-again shell". The default on most Linux distributions, a superset of sh.
Dash dash A tiny, fast POSIX shell. Debian and Ubuntu use it as /bin/sh.
Zsh zsh Bash-like with stronger completion and themes. macOS default since 2019.
Fish fish Friendly and modern, with autosuggestions, but not POSIX compatible.

The shell you type into and the shell that runs a script can differ. A script's first line, the shebang (#!/bin/bash or #!/bin/sh), picks the interpreter. This matters because /bin/sh is often not Bash. The sandbox below runs the same line through dash and bash.

Bash Same line, two shells

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 read or cat), it gets the next line from this box.

[[ ... ]] and arrays are bashisms: Bash extensions that a strict POSIX shell rejects. If a script must run everywhere, write for sh; if it uses Bash features, say #!/usr/bin/env bash. The Portability lesson goes deeper.

Kernel versus distribution. The architecture above is the same on every distribution. What changes between Debian, Fedora and Arch is the package manager, the default shell and tools, and the update policy. The next lesson, Linux Distributions and Unix Heritage, compares them.

Following one command through the stack

Putting it together, here is what happens when you run cat notes.txt:

  1. Shell: Bash splits the line, finds /usr/bin/cat by searching PATH, and calls fork(). The scheduler creates a child process.
  2. Child: it calls execve("/usr/bin/cat", ...). The kernel replaces the child's memory with the program and the dynamic loader maps in glibc.
  3. Library: cat calls open(), then repeatedly read() and write(). These are system calls that cross into kernel space.
  4. Kernel: the VFS finds the file's file system, the permission check runs, the page cache supplies the bytes, and the memory manager copies them into cat's buffer. A write to the terminal goes through the terminal driver.
  5. Exit: cat calls exit(). The kernel tells Bash through a signal and an exit status, and the prompt comes back.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the double line (system call interface) in the Linux layer diagram separate?

    Programs, libraries and shells run in user space with limited privileges. The kernel runs in kernel space. System calls are the only controlled gate between them.
  2. 2. Which kernel design does Linux use?

    Most of the kernel, including the drivers, runs in kernel space as one program. Loadable modules let you add or remove drivers at run time without rebooting.
  3. 3. What is the role of the Virtual File System (VFS)?

    The VFS lets tools like cat read any file in the same way. Each file system type implements the same operations and the VFS routes calls to the right one.
  4. 4. A script written with [[ ... ]] fails when started as sh script.sh on Debian, but works with bash script.sh. Why?

    On Debian and Ubuntu /bin/sh points to dash, a minimal POSIX shell. [[ and arrays are Bash features, so use a bash shebang or write portable POSIX code.
  5. 5. Which component turns a call to fopen() into the open system call?

    The C library provides the friendly functions and performs the actual system calls on behalf of the program.

Summary

  • Linux is a stack: applications, shell, system libraries, then the kernel on top of the hardware. The system call interface is the border between user space and kernel space.
  • User space is restricted; the kernel is privileged. A program asks the kernel for services through system calls, usually via a C library such as glibc or musl.
  • Linux is a monolithic kernel with loadable modules. Its subsystems are the scheduler, memory manager, VFS, network stack, IPC, drivers and security features.
  • The VFS gives every file system, including /proc, the same interface.
  • The shell is an ordinary user-space program. Shells differ: sh and dash are minimal POSIX shells, bash is the common default, ksh and zsh add features, fish is friendly but not POSIX.
  • Choose the interpreter of a script with the shebang, and do not assume /bin/sh is Bash.

Next: Linux Distributions and Unix Heritage