Linux System Administration

0 of 83 lessons complete (0%)

Starting up and Shutting Down

Peripherals, Kernel, Run-Level

You don’t have access to this lesson

Please register or sign in to access the course content.

1. Peripherals

During the startup process, the system initializes and configures various hardware peripherals. This includes detecting devices like keyboards, mice, hard drives, network cards, and graphics cards, and loading the necessary drivers for them to function correctly.

The BIOS/UEFI plays an initial role in recognizing basic hardware before passing control to the operating system’s bootloader.

Example:

When you power on your computer, the BIOS/UEFI performs a Power-On Self-Test (POST) to check essential hardware. You might see a brief message on the screen indicating memory checks or hard drive detection. Once the bootloader takes over, the Linux kernel continues to identify and load drivers for all connected peripherals.

2. Kernel Loading

The kernel is the core of the operating system. After the BIOS/UEFI and bootloader (like GRUB) complete their tasks, the bootloader loads the Linux kernel into memory. The kernel then takes over control of the system.

Example:

A typical GRUB boot menu might look something like this, allowing you to select which kernel to boot:

GNU GRUB version 2.06

*Ubuntu
Advanced options for Ubuntu
Memory test (memtest86+)
Memory test (memtest86+, serial console 115200)

Use the ^ and v keys to select which entry is highlighted.
Press enter to boot the selected OS, 'e' to edit the commands
before booting or 'c' for a command-line.

Once you select an option, GRUB loads the specified kernel image (e.g., /boot/vmlinuz-...) and the initial RAM disk (initramfs) into memory.

3. Console

The console refers to the primary interface for system interaction, typically a text-based interface. During startup, system messages and prompts are displayed on the console. It’s crucial for viewing boot messages and troubleshooting.

Example:

During boot, you’ll often see a stream of messages scrolling by on your screen. These are kernel messages and service startup messages. For instance, you might see:

[    0.000000] Linux version 6.5.0-28-generic (buildd@lcy02-amd64-009) (gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0, GNU ld (GNU Binutils for Ubuntu) 2.40) #29-Ubuntu SMP PREEMPT_DYNAMIC Mon Mar 4 19:42:09 UTC 2024
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-6.5.0-28-generic root=UUID=... ro quiet splash vt.handoff=7
[    1.234567] systemd[1]: Systemd 250.4-1ubuntu1 is running.
[    5.678901] ip_tables: (C) 2000-2006 Netfilter Core Team

You can usually switch to different virtual consoles using Ctrl+Alt+F1 to Ctrl+Alt+F6 (or F7 for the graphical environment).

4. The Scheduler

Once the kernel is loaded, the scheduler becomes active. It’s a critical part of the kernel responsible for managing the allocation of CPU time to various processes. It ensures that multiple tasks can run concurrently, giving the illusion of parallel execution on a single CPU.

Example:

When you have multiple applications open (e.g., a web browser, a text editor, and a music player), the scheduler is constantly working in the background. It rapidly switches between these applications, giving each a slice of CPU time. You can observe the effects of the scheduler using tools like top or htop, which show CPU utilization by different processes.

top - 23:15:30 up 2 days, 15:05,  1 user,  load average: 0.15, 0.20, 0.18
Tasks: 210 total,   1 running, 209 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.5 us,  0.5 sy,  0.0 ni, 98.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :  7930.5 total,  2450.0 free,  3500.0 used,  1980.5 buff/cache
MiB Swap:  2048.0 total,  2048.0 free,    0.0 used.  4000.0 avail Mem

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   1234 user      20   0 2500000 200000 100000 S   1.0   2.5   0:15.23 firefox
    567 root      20   0  150000  10000   5000 S   0.0   0.1   0:01.05 systemd

5. Init and Inittab File (System V Init)

In traditional System V Init systems, the init process (PID 1) is the first process launched by the kernel. It’s responsible for starting all other processes and managing the system’s state. The /etc/inittab file configured init, defining what processes to run for each run level.

Note: Most modern Linux distributions have transitioned from System V Init to systemd, which uses different configuration methods (unit files). However, understanding init and inittab is still valuable for older systems or historical context.

Example (System V Init – Historical):

A typical /etc/inittab file on a System V Init system might contain entries like:

# inittab is used by Init.

id:3:initdefault:

si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6

# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6

The id:3:initdefault: line sets the default run level to 3 (multi-user, text mode).

6. Run Levels (System V Init)

Run levels define the operating state of a Linux system in System V Init. Each run level specifies which services are started or stopped, allowing for different system configurations (e.g., single-user mode for maintenance, multi-user with networking, or graphical desktop).

  • Runlevel 0: Halt (System shutdown)
  • Runlevel 1 (S/s): Single-user mode (Maintenance mode)
  • Runlevel 2: Multi-user, without networking (Text mode)
  • Runlevel 3: Multi-user, with networking (Text mode)
  • Runlevel 4: Not used/User-definable
  • Runlevel 5: Multi-user, with networking and graphical display (X11)
  • Runlevel 6: Reboot (System restart)

Example (System V Init):

To check the current run level:

$ runlevel
N 5

This output indicates the previous run level was ‘N’ (none, meaning boot-up) and the current run level is 5.

To change to run level 3 (multi-user, text mode):

$ init 3

Or for reboot:

$ init 6

7. Run Level Scripts (System V Init)

In System V Init, services are started and stopped based on the current run level using scripts located in directories like /etc/rc.d/ or /etc/init.d/. These directories contain symbolic links to actual service scripts in /etc/init.d/. The links are prefixed with ‘S’ (start) or ‘K’ (kill) and a two-digit number indicating the order of execution.

Example (System V Init):

For run level 5, you might find a directory like /etc/rc5.d/ containing links:

/etc/rc5.d/
├── K01sendmail -> ../init.d/sendmail
├── S10network -> ../init.d/network
├── S20sshd -> ../init.d/sshd
└── S99xdm -> ../init.d/xdm
  • K01sendmail: The K indicates that the sendmail service should be killed (stopped) when entering run level 5. The 01 indicates the order of execution.
  • S10network: The S indicates that the network service should be started. The 10 indicates it will be started before services with higher numbers.
  • S99xdm: This would start the X Display Manager, leading to the graphical login screen.

When changing run levels, the init process would execute these scripts in the defined order.

Modern Linux (systemd) – Brief Mention

As mentioned, most modern Linux distributions (like Ubuntu, Fedora, Debian, CentOS 7+) use systemd instead of System V Init. While the core concepts of starting services and managing system states remain, the implementation differs significantly.

  • Init: Replaced by systemd (still PID 1).
  • Inittab: Replaced by unit files (e.g., .service, .target files) usually found in /etc/systemd/system/ and /usr/lib/systemd/system/.
  • Run Levels: Replaced by “targets” (e.g., multi-user.target, graphical.target).
  • Run Level Scripts: Replaced by unit files and the systemctl command.

Example (systemd):

To check the current target in systemd:

$ systemctl get-default
graphical.target

To change to multi-user (text) mode:

$ systemctl isolate multi-user.target

To start, stop, or check the status of a service:

$ systemctl start apache2.service
$ systemctl stop apache2.service
$ systemctl status apache2.service