Linux: Basics
The Linux open-source operating system was created by Linus Travolta in 1991. Linux has multiple distributions. A distributions is a system built on the Linux kernel, software packages, and package management system. Linux distributions usually follow two categories: RPM
based and dpkg
based. The difference between the two is the package managmement they use.
Linux is built on the priciple of FOSS
(Free and Open Source Software) that allows users to change, share, and distribute it freely.
RPM-based Distributions
RPM
= Red Hat Package Manager
includes RHEL, CentOS, Almalinux, Fedora, and OpenSUSE. They use dnf
command (previously yum
) to manage packages. Zypper
for OpenSUSE. RHEL
(Red Hat Entreprise Linux) is a commercial distribution that typically requires a subscription.
dpkg-based Distributions
They derive from Debian including Ubuntu
, Linux Mint
, and Kali Linux
. They use the dpkg
and apt
(Advanced Package Tool) package manager. The .deb
package format requires the dpkg
tool for installation. apt
automatically retrieves packages and their dependences from online repositories.
Linux Boot Process
The Linux Boot Process is how a computer using the Linux operating system starts up. It has 4 main components:
- Preboot Execution Environment (PXE): used for remote booting
- Bootloader: Loads the OS and prepares the system
- Initial RAM Disk (initrd): Provides essential drivers and tools before switching to the real root filesystem
- Kernel: The core of the OS, managing system resources
init
: The process in the system
Preboot Execution Environment (PXE)
It allows a computer to start up over a network connection rather than booting from its internal storage. it is useful where many system have to be setup simultaneously or remotely. Here are the steps for PXE Booting a computer:
- The network card activates to start the boot process
- The network card request an IP address from the DHCP server
- The DHCP server provides the location of the TFTP server
- The necessary files are transferred from the TFTP server
- The Bootloader is executed to load the operation system
Bootloader
There several Bootloaders but GRUB2 (Grand Unified Bootloader v2) is commonly used. It is responsible for selecting and lunching the Linux kernel. WHen the computer starts,
- The firmware hands control to GRUB2
- GRUB2 displays boot menu: The user select the OS or kernel if multiple operating systems are available
- GRUB2 reads configuration from
/boot/grub/grub.cfg
that contains kernel version and other options - GRUB2 loads
initrd
and the kernel and the system prepares for booting
Use update-grub
to modify GRUB configuration at /etc/default/grub
Initial RAM Disk (initrd)
initrd.img
is a temporary root filesystem that is loaded into memory before the real root filesystem is mounted. it contains the tools needed for booting. It is typically located at /boot/initrd.img
. It can be managed using tools like mkinitramfs
and dracut
depending on the Linux distro.
Kernel
The kernel is the main component of the operating system. It manages the hardware, processes, memory, and system resources. The kernel behavior can be adjusted using sysctl
to modify the kernel parameters at runtime. The change can be made permanent by updating the configuration file /etc/sysctl.conf
then apply the changes with sysctl -p
command. The kernel also start the first process: init
.
init
The init
system is the first process that start all other processes. It has a PID of 1. It is the parent of all other processes.
System Directories
The Filesystem Hierarchy Standard (FHS)
specifies a set of guidelines for the names of files and directories and their locations on Linux systems.
Here are some important directories in a Linux system:
/
the root directory. It is the top level directory from which all other directories branch out.
/bin
stores essential command line utilities and binaries
/boot
contains the files necessary to boot the Linux OS: /boot/initrd.img
, /boot/vmlinuz
, /boot/grub/grub.cfg
/dev
stores hardware and software device drivers (/dev/sdb
the first drive, /dev/tty
the first terminal interface, /dev/null
, /dev/random
)
/etc
stores system-wide configuration files. Important files include /etc/passwd
for user account details, /etc/shadow
for password information, /etc/fstab
for file system mount points, /etc/hosts
for hostname to ip mappings, and /etc/ssh/sshd_config
for SSH configuration.
/home
stores users' home directories, including personal files. By default, every user is assigned a sub directory in the /home
directory.
/lib
stores shared program libraries required by the kernel, command-line utilities, and binaries
/media
stores mount points for removable media such as CD-ROMs and floppy disks
/mnt
refers to the mount point for temporary mounting file systems
/opt
stores optional files for large software packages
/proc
represents continually updated kernel information to the user ina typical file format. For example we have: /proc/cpuinfo
for CPU usage, /proc/meminfo
for memory usage, /proc/uptime
for system uptime, /proc/[PID]/
for information about running processes. The /proc
directory is important for monitoring and troubleshooting system performance.
/root
represents the home directory of the root user
/sbin
stores binaries used for completing the booting process which are also used by the root user (/sbin/fsck
, /sbin/reboot
, /sbin/iptables
, /sbin/mount
)
/sys
stores information about devices
/tmp
stores temporary files that may be lost on system shutdown
/usr
is a read-only directory that stores small programs and files accessible to all users. /usr/bin
contains programs that can be executed by all users. /usr/local
contains custom build application that is contained here by default.
/var
stores variable files, or files that are expected to constantly change as the system runs. It includes /var/log
for logs, /var/mail
for mail storage, /var/www
for websites.
Environment Variables
The $
is used to recall environment variables. For example echo $USER
will print the username that is stored in a variable called USER
. The are few more usefull user variables in addtion to USER
: HOME
holds the user home directory path, SHEL
holds the command line interpreter running the current environment, PS1
defines the styles and content of the command line prompt.
PATH
and DISPLAY
are common system variable often seen in Linux systems. PATH
is used to find executables when a command is entered in the terminal. DISPLAY
tells to applications, which screen to send graphical output.
Input and Output Redirection
<
is used for redirecting file content as command input
<<
(Here-document) allows embeding multiple line input in a command
<<<
(Here-string) allows passing a single string input to a command
>
redirects output to a file and override the existing content
>>
appends output to a file content
2>
and 2>>
can be used to capture error output into a separate file
&>
can be used to capture both standard output and error into a file
Command history
history
allows displaying all commands entered in the current and previous session
!!
let us repeat the last command
!
can execute a command that start with the string provided
alias
allows personalizing shortcuts for long commands
More
uname
provides essentials details about the system such as operationg system, kernel version, and hardware architecture.
source
used to execute scripts in the current shell session. For example environment variable can be updated with source
without loging out users. Ex: source ~/.bashrc
to load environment variables