Skip to main content

Understanding Linux System Architecture

1. Understanding Kernel vs User Space

Aspect Kernel Space User Space
Access Level Full access Limited access
Memory Access Direct memory access Virtual memory access via system calls
Execution Context Runs in privileged mode (ring 0) Runs in unprivileged mode (ring 3)
Resource Management Manages hardware resources directly Requests resources through system calls
Security High security risk if compromised More secure due to isolation
Examples Device drivers, memory management Applications, shells, GUIs

Example: System Call Flow

# User space application calls a system call
echo "Hello, World" | wc -c

# Kernel space handles the system call
# - reads from stdin (user space)
# - counts characters (kernel space)
# - returns result to user space
Note: The kernel space is responsible for core system functions like memory management, process scheduling, and hardware abstraction. User space applications interact with the kernel through system calls.

2. Understanding Linux File System Hierarchy

Directory Description Contents Notes
/ Root directory Base of the filesystem Every path starts here
/bin Essential user binaries ls, cp, mv, cat, etc. Required for system boot
/boot Boot loader files vmlinuz, initrd.img, grub/ Contains kernel and boot config
/dev Device files fd0, hd0, sda, null, zero Virtual filesystem for devices
/etc Configuration files hosts, passwd, group, network/ System-wide config files
/home User home directories user1/, user2/, ... Personal storage for users
/lib System libraries libc.so, libssl.so Shared libraries needed by binaries
/media Removable media mount points floppy/, cdrom/, usb/ Mount point for removable devices
/mnt Temporary mount points mount point for filesystems Used for temporary mounts
/opt Optional application software packages Third-party apps Used for optional software
/proc Process information pseudo-filesystem pid directories, cpuinfo, meminfo Virtual filesystem with runtime info
/root Root user home directory Files for root user Home directory of root user
/run Runtime data for running processes pid files, sockets Stores runtime data (since 2015)
/sbin System binaries ifconfig, route, mount Essential system administration binaries
/srv Service data www/, ftp/, mail/ Data for services provided by the system
/sys System information pseudo-filesystem devices/, bus/, class/ Virtual filesystem for kernel data
/tmp Temporary files Temporary files (cleared on reboot) Used for temporary storage
/usr Secondary hierarchy for user commands bin/, lib/, share/ Contains user binaries and data
/var Variable data files log/, mail/, spool/ Stores variable data like logs

Example: View file system hierarchy

ls -la /
Note: The FHS (Filesystem Hierarchy Standard) defines the structure of directories in Linux systems.

3. Understanding System Processes

Process Description Key Attributes
init First process that starts when the system boots Parent of all other processes, PID 1
fork() System call to create a new process Creates child process with copy of parent's memory space
exec() Replaces current process image with new program Used after fork() to load new executable
ps Command to report current processes Shows process ID, user, CPU usage, memory
kill Send signal to process by PID Signals: TERM (graceful shutdown), KILL (force kill)

Example: Process Management

ps aux | grep nginx
kill -TERM 12345
Note: Processes in Linux can be in various states: running, sleeping, stopped, or zombie. Zombie processes are those that have completed execution but still have an entry in the process table.

Process Lifecycle

  • Created via fork()
  • Executed using exec()
  • Managed with ps, kill, and signals
  • Terminated when parent calls wait()

4. Understanding Runlevels and Targets

Runlevel Description Services
0 System Halt Stops all processes and powers off the system
1 Single-user mode Minimal services, no network or multi-user support
2 Multi-user mode (without NFS) Basic services, no network filesystem support
3 Multi-user mode (full) Console-based multi-user environment with network support
4 Unused Not used in most distributions
5 Multi-user mode with GUI Full multi-user environment with graphical interface
6 System Reboot Stops all processes and reboots the system

Example: Changing Runlevel

# Change to runlevel 3 (multi-user console)
sudo telinit 3

# Or using systemctl
sudo systemctl isolate multi-user.target
Note: Modern Linux systems often use systemd targets instead of traditional runlevels. The equivalent of runlevel 3 is multi-user.target, and runlevel 5 is graphical.target.

Runlevel vs Target Mapping

  • 0 → poweroff.target
  • 1 → rescue.target
  • 2 → multi-user.target (without NFS)
  • 3 → multi-user.target (full console)
  • 4 → multi-user.target (unused)
  • 5 → graphical.target
  • 6 → reboot.target

5. Understanding Linux Boot Sequence

Stage Description Key Components
BIOS/UEFI Initial hardware self-test and firmware loading POST, boot order settings
Boot Loader (GRUB) Loads the kernel into memory grub.cfg, menu entries
Kernel Initialization Mounts root filesystem and starts init process initramfs, /sbin/init
Init Process (PID 1) Starts all other processes based on runlevel systemd, SysV init

Example: GRUB Configuration

# /boot/grub/grub.cfg
menuentry "Ubuntu" {
    linux /vmlinuz-5.15.0-102-generic root=/dev/sda1 ro
    initrd /initrd.img-5.15.0-102-generic
}
Note: The boot sequence can vary depending on the distribution and init system used (e.g., systemd vs SysV init).

6. Understanding Shell Types

Shell Type Description Key Features Use Cases
Bourne Shell (sh) Original Unix shell, the foundation for most modern shells Basic scripting capabilities, POSIX-compliant System initialization scripts, basic automation tasks
Bourne Again Shell (bash) Most widely used shell in Linux, an extension of sh Advanced scripting, command history, tab completion Interactive use, complex automation scripts, system administration
Z Shell (zsh) Powerful shell with extensive customization options Advanced completion, theming, plugin support Power users, developers requiring advanced features
Fish Shell User-friendly shell with syntax highlighting and auto-completion Intuitive, web-based configuration, smart suggestions Beginners, developers valuing ease of use

Example: Checking Current Shell

$ echo $SHELL
/bin/bash

Example: Switching Shells

$ chsh -s /bin/zsh
Password:
$ echo $SHELL
/bin/zsh

7. Viewing System Information

Command Description Example Output
uname Display system information Linux hostname 5.15.0-1028-oem #33-Ubuntu SMP...
uptime Show how long the system has been running 14:23:45 up 2 days, 3:15, 1 user, load average: 0.15, 0.20, 0.25
df -h Display disk space usage in human-readable format
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G   12G  6.5G  65% /
free -h Show memory usage in human-readable format
total        used        free      shared  buff/cache   available
Mem:           7.7G        2.1G        3.2G        156M        2.4G        5.2G
lsb_release -a Display Linux Standard Base (LSB) information
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.3 LTS
Release:        22.04

Example: Check system uptime and load average

$ uptime
 14:23:45 up 2 days,  3:15,  1 user,  load average: 0.15, 0.20, 0.25

Example: View detailed system information

$ uname -a
Linux hostname 5.15.0-1028-oem #33-Ubuntu SMP PREEMPT Wed Apr 5 10:24:32 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Example: Display disk usage

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G   12G  6.5G  65% /
tmpfs           3.8G     0  3.8G   0% /dev/shm
/dev/sda15      105M   34M   71M  33% /boot/efi
Note: The df -h command shows disk space in a human-readable format (e.g., GB, MB), while df -h displays the same information in kilobytes or bytes.
Warning: The free -h command might show different values than what is shown in the system monitor due to caching and other factors.

8. Checking Distribution Version

Command Description Example Output
lsb_release -a Displays all available information about the Linux distribution
LSB Version:    n/a
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.3 LTS
Release:        22.04
Codename:       jammy
cat /etc/os-release Displays OS release information from the os-release file
NAME="Ubuntu"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 22.04.3 LTS"
VERSION_ID="22.04"
cat /etc/issue Displays system information at login prompt Ubuntu 22.04.3 LTS \n \l
uname -a Displays kernel version and system information Linux hostname 5.15.0-86-generic #96-Ubuntu SMP Thu Aug 17 17:05:23 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Note: The /etc/os-release file is the modern standard for identifying Linux distributions, while lsb_release provides additional LSB (Linux Standard Base) information.

9. Viewing Kernel Version

Command Description Example Output
uname -r Displays the kernel release version 5.15.0-1028-oem
uname -v Displays the kernel version with build information #30-Ubuntu SMP PREEMPT Mon Apr 18 17:25:36 UTC 2022
cat /proc/version Shows kernel version and system information Linux version 5.15.0-1028-oem (buildd@lcy02-arm64-015) (gcc ...)
cat /proc/sys/kernel/osrelease Displays only the kernel release string 5.15.0-1028-oem

Example: Check kernel version

$ uname -r
5.15.0-1028-oem

10. Viewing System Uptime and Load Average

Command Description Example Output
uptime Displays system uptime, number of users, and load averages 10:25:43 up 2 days, 3:45, 2 users, load average: 0.15, 0.20, 0.25
uptime -p Displays system uptime in a human-readable format up 2 days, 3 hours and 45 minutes

Example: Using uptime command

uptime
 10:25:43 up 2 days,  3:45,  2 users,  load average: 0.15, 0.20, 0.25
Note: The load average represents the average number of processes in the run queue over 1, 5, and 15-minute intervals.

Was this article helpful?