Linux - Essential concepts

See https://wiki.archlinux.org/title/Arch_boot_process

Linux - Essential concepts

Source: Youtube - Fireship - 100+ Linux Things you Need to Know

If you’re a programmer or developer, you need to know Linux. That’s where your code will eventually run and fail. If you can’t SSH into a Linux terminal and fix it, you are screwed. Lets learn everything we need to know about Linux by looking at some essential concepts.

Before Linux

Before one can understand Linux, one must recognize what came before. Its Unix, an operating system developed at AT&T Bell labs in the 70s. Its development led to a standardization called posix, or portable operating system interface, to ensure that different systems would be compatible with each other. Its influence remains strong today with MacOS, Android, FreeBSD, and most Linux distributions being posix compliant.

In 1987, an OS called Minix for academic use was developed. But redistribution of its code was restricted. This inspired a Finnish computer science student named Linus Toralds to develop Linux in 1991. Importantly, it’s free, open source software, licensed under GPL 2.0. And by free, I mean free is in Freedom. It’s free to distribute, modify, and make money off of.

The kernel

Now, what I’m referring to as Linux is in fact not an operating system, but rather, an operating system kernel. It’s written in the C programming language and is the black magic that sits between your software applications and the hardware.

Firmware Type

Boot Mode

A boot mode is the system setting that determines how a computer’s firmware (BIOS or UEFI) interacts with hardware and starts an operating system. The two primary boot modes are Legacy BIOS and UEFI. UEFI is a modern, more secure, and faster firmware that replaces the older Legacy BIOS, which provides a traditional boot environment. To change boot mode, you generally access your computer’s setup utility (BIOS/UEFI) during startup by pressing a specific key like F2 or Del, or by restarting from Windows’ Advanced Startup options.

Understanding the Two Main Modes

  1. UEFI (Unified Extensible Firmware Interface):
    1. The newer, standard firmware that offers faster boot times, improved security features like Secure Boot, and support for larger hard drives.
  2. Legacy BIOS:
    1. An older firmware that provides a traditional boot environment. It is often referred to as CSM (Compatibility Support Module) mode in modern systems.

Why You Might Change Boot Mode

  1. Operating System Compatibility:
    1. Some older operating systems or bootable tools are only compatible with Legacy BIOS, while modern operating systems like Windows 11 are best supported by UEFI.
  2. Security Features:
    1. UEFI mode enables advanced security features such as Secure Boot, which helps prevent unauthorized operating systems from loading at startup.
  3. Hardware Support:
    1. UEFI mode is required for certain hardware features and boot devices, such as booting from a UEFI-compatible USB drive or using specific network boot options.

How to Access and Change Boot Mode

There are two primary ways to access the boot mode settings:

  1. During Startup (Recommended)
    1. Restart your computer.
    2. Press a specific key repeatedly (e.g., F2, Del, F10, F12, Esc) to enter the BIOS/UEFI setup menu. The exact key varies by manufacturer and model, but often a message will appear on the screen during startup telling you which key to press.
    3. Navigate to the “Boot” or “Boot Maintenance Manager” section.
    4. Select “UEFI/BIOS Boot Mode” or a similar option.
    5. Choose between “UEFI” or “Legacy BIOS” mode.
    6. Save your changes by pressing F10 and confirming, then exit.
  2. From Windows (If UEFI/BIOS Firmware Settings is an option)
    1. Open the Windows Start menu.
    2. Select Power ( ) > Restart.
    3. Hold down the Shift key while clicking Restart.
    4. Select Troubleshoot > Advanced options > UEFI Firmware Settings > Restart to boot directly into the UEFI settings.

The boot loader

https://wiki.archlinux.org/title/Category:Boot_loaders

When you hit the power button on your computer, the boot loader, which is usually grub, will load the Linux kernel into random access memory.

GRUB (GRand Unified Bootloader) is a bootloader for Linux and other operating systems that runs after the BIOS/UEFI and loads the operating system kernel. It is a feature-rich, versatile tool that supports multiple file systems, network booting, and can be used to dual-boot different operating systems. GRUB presents a menu to the user, allowing them to select which operating system to boot, and can be configured to load a specific OS after a timeout period.

https://www.gnu.org/software/grub/

The init system

From there, it detects hardware and starts the init system, which is typically a tool called Systemd, although alternatives do exist.

Linux - Init systems

User spaces

Once initialized, the kernel will start up applications in user space, which will typically bring the user to a login screen. As the user starts doing stuff, the kernel has a lot of responsibility. It allocates and deallocates memory for processes, and can even create virtual memory to use more memory than is physically available by tapping into your hard drive.

The file system

Speaking of the hard drive, the kernel also provides a virtual file system to interact with files on different systems. The fourth extended file system (ext4) is the most common default on Linux. But it’s not the only option.

The drivers

The kernel also interacts with all these peripheral devices via drivers.

CPU’s protection ring

Pretty cool. But you can’t just walk up and mess around with the kernel. And that’s because it’s surrounded by the CPU’S protection ring. At ring zero, we have the kernel with the highest level of privilege. While most of us normies in user space live in ring three, with the lowest level of privilege.

System calls

But often, you’ll want to do something that requires access to the kernel, like, write a file to the file system. And that’s where system calls come in. In C code (or, code in any language), if you call to write a statement to the console, which will transition from ring three to ring zero, to output some text to the console.

GNU standard library, glibc

However, write itself is not a system call. It’s actually a wrapper provided by glibc, which is the GNU standard library for C, and provides all kinds of wrappers for making system calls that can do almost anything on your OS.

But wait a minute. What is GNU? It’s a project that predates the Linux kernel itself and was started all the way back in 1983 by Richard Stalman. It provides all the core utilities for Linux, which are all the software utilities that make the kernel useful to humans.

The terminal or The shell

The best way to start exploring these core libs is to open up the terminal, which is a graphical user interface that allows you to send commands via the shell. Now, they call this thing a shell because it provides a layer of protection between user space and the kernel. There are many different flavors of shells (bash, zsh, fish). But the most common is Bash. Let’s say hello to the Linux kernel by running the GNU shell utility echo and providing a string argument to it. This command takes our message and prints it to the standard output. Pretty simple. But what actually happened under the hood is that a system call was made to the kernel, which checked permissions and manage drivers to turn those ones and zeros into pixels on the screen. As an end user though, I don’t care.

The man pages

My friend told me about a cool command called touch but I have no idea what it does. The cool thing about Linux is that you can pull up the manual for any command by hitting up my main man, man. It looks like this command is used to create a new file.

The ls command

Now, by creating a new text file, it looks like nothing happened. But I promise it worked. I can prove it by using the ls command to list out the files in this directory. And there it is.

The cat command

And we can read the file contents with the cat command. But again nothing happens, because there’s no data in this file.

The stat command

However, there is a bunch of metadata, like timestamps, that we can access with the stat command. When we run it, we know this file’s birth time, when it was modified, and when it was last accessed. That’s useful. But we can also get more information from the ls command by appending flags to it. Like the L flag to list more details and the H flag to make them human readable. When we run that command, we can now see the exact size of every file. And we can also combine flags in Linux to make this command more concise. I don’t want an empty file though.

The rm command

So I’m just going to remove it with the RM command.

Combining commands

The cool thing about the Linux terminal is that it’s really easy to combine commands. like, I can take Echo and use this angle bracket to redirect its output to a new file.

echo "hi, mom" > new_file.txt

In addition, I can flip this angle bracket around to also redirect the input of a file.

echo "$(< new_file.txt)"

That’s cool. But pipes are even cooler. They allow you to take the output of one command, and pass it off to another command. For example, if we have a log file of our broken code we might first use cat to read that file, but then we could pipe the output to sort which would sort it line by line, and then unique to remove any duplicates.

cat error.log | sort | unique

There’s so much more we could do just from the terminal. But if you find yourself doing the same thing over and over again, it might be time to write a bash script in its own dedicated file. At the top of the file, we’ll add a shebang

#!/bin/bash

That tells Linux to use the bash interpreter. Then we can add as much bash code as we want. We might use Echo. And then read to read a value from the standard input. And then Echo to once again Echo it back. And now if we save this file, we can execute it by simply entering the file path in the terminal.

whoami, usernames, The root user, sudo

One major drawback of using Linux is that it might trigger an existential crisis and you might ask yourself whoami. When you enter that command, it’s going to return your Linux username. In addition, every user has a unique uuid that can be viewed with the ID command

id -u

My ID is 1000, but there is one special user with a uid of zero, called root AKA admin, super user, or daddy. The root has the highest level of privilege and you can switch to the root user with the su (substitute user) command or prefix any command with sudo, to run it with elevated privilege. Any user can be granted sudo privilege and you can check your sudo right now by running sudo -l

groups

In addition to users Linux also has groups. Groups have group IDs and make it easier to manage permissions for multiple users. Before we talk about permissions though let’s explore the file system.

bashrc file

With ./bashrc, we can also do things like customize the ps1 environment variable to change the terminal prompt.

File permissions

Use ls -l on any file to view permissions and notice these cryptic nine characters. These are called symbolic permissions. The first triplet represents the owner. The middle the group. And the last triplet is for everyone else. Each one contains a letter that represents read, write and execute privileges. If the letter is present, it means access granted. But if there’s a dash, it means permission denied. These can also be represented as numbers in Octal notation. For example, 777 lets anybody do anything to a file. 777 is good on slot machines. But in Linux, it’s generally a bad idea because, you want to always follow the principle of lease privilege. Grant access to things only when necessary and trust no one. Now you can modify the permissions on a file with the chmod command. You can use it to grant read access to a document for everybody. We can also change the owner of a file with chown. Or assign groups with chgrp. And now that we know what all these things, permissions aren’t so cryptic.

Processes

Now anytime you run a command or execute a program, it creates a process on the CPU, which is managed by the Linux kernel. You can view these processes with the command ps -ef. Notice how each one has a unique process ID along with the user who created it. Or better yet, use htop to get an interactive breakdown of processes that can be filtered. Some of these are just system demons that run in the background. In fact, if you have a long running script, you can even create your own background process by adding & to the end of it. Or if you want a script run on a specific schedule, like a reminder to do something at 4:20 p.m. today, you can accomplish that by adding your script to the cron tab. That’s cool. But occasionally, you’ll have a bad process that needs to be killed. The kill command can do that by gracefully sending a sig term signal to the process. If that doesn’t do the trick though, use the nine flag to forcefully kill it with sig kill.

Utilities

Other utilities you should know about include grep for searching through text, sed for modifying text, gzip for making files smaller, and tar for archiving directories.

Distributions

The Linux experience varies wildly when talking about different dros. A Linux distribution is just a complete operating system built on the Linux kernel. And each distro has a highly opinionated set of default software for their target audience. Some are designed for beginners, others for hardcore hackers and everything in between. Distros can have different package managers to install new software like apt, yum and pacman. And they might also have different release schedules. Some have a predictable fixed release date. While others have rolling releases that keep their software on the cutting edge at all times.

Another concept is desktop environments. If using Linux as a PC, your distro will have a default desktop environment like gnome or KDE plasma and that makes a huge difference in the experience. Some distro families you should know about include slackware, the Original Gangster from the 90s, Debian, which is a popular one, famous for its open philosophy and ease of use, red hat the distro of choice for Enterprise, for its long-term support plans, and finally the Arch family.


Links to this note