02. Linux core concept


Q. What is kernel?
Ans*:* Kernel is a major component in an operating system and is the core interface between a computer’s hardware and its processes. It communicates between the two managing resources as efficiently as possible.
The kernel is responsible for 4 major tasks:
Memory management: keep track of how much memory is used to store what and where.
Process management: determines which processes can be use the CPU when and how long.
Device Drivers: act as a mediator or an interpreter between the hardware and processes.
System calls and security: receive requests for service from the processes.
→ Linux kernel is monolithic, which means that the kernel carries out CPU scheduling, memory management, and several other operations by itself.
→ The kernel is also modular, which means it can extend its capabilities through the use of dynamically loaded kernel modules.
uname
to display about the kernel
uname -r
or, uname -a
to display about the kernel version
Kernel and User space
Memory is divided into two areas: kernel space (kernel mode) and user space (user mode).
In kernel space:- kernel executes and provides its services. A process running in the kernel space is unrestricted access to the hardware. It is strictly reserved for the kernel code, kernel extensions, and most device drivers
User space gets access to the data by making special requests to the kernel called system calls.
- Common system calls are close(), get PID, read directory, string length, and close directory.
Working with Hardware
dmesg
is a tool used to display messages from an area of kernel called the ring buffer. When system boots up it detects the hardware devices and show the logs and provide a good indication whether it can configure them.
dmesg | grep -i usb
searching specific keywords using grep.
udevadm
utility is a management tool for udev
udevadm info --query=path --name=/dev/sda5
udevadm info command queries the udev database for device information. In this example, we are querying the hard disk attached to the system.
udevadm monitor
listens to the kernel uevents. Detects and shows the device name and path to the screen.
lspci
List the PCI. Display all PCI devices that are configured in the system.
Examples of PCI devices are ethernet cards, raid controllers, video cards, and wireless adaptors that are directly connected to the computer motherboard
PCI: Peripheral Component Interconnect
lsblk
list information about block devices.
‘Disk’ refers to the whole physical disk, and the ‘partitions’ are referred to a reusable disk space lies in the physical disk. In the command output, we can see the major and minor (m:n) associated with each device.
The major number identifies the type of device driver associated with the device. Here, 8 refers to a block sdisk device.
The minor number is used to differentiate among devices that are similar and share the same major number. Here, 0-5 help to identify the different partitions of the disk sda.
Commonly used devices along with their major number.
lscpu
to display the CPU architecture
Sockets are the physical slots on the motherboard where an individual can insert a physical CPU.
lsmem
or, lsmem --summary
to display the memory on the device.
free -m
also shows the memory in used and free format.
- m for MB, k for KB, and g for GB
lshw
display detailed information about hardware configuration on the machine. use sudo
Boot sequence
BIOS POST → Boot Loader or GRUB2 → Kernel Initialization → Service initialization using systemd
BIOS POST has very little to do with Linux itself. POST (Power On Self Test). BIOS runs a POST test to ensure the hardware components attached to the device are functioning correctly. If POST fails, the computer may not be operable, and the system will not proceed to the second stage of boot process.
After successful POST test, the BIOS loads and executes the boot code from the boot device which is located in the first section of the hard disk.
In Linux, it is located in the /Boot File System.
The boot loader provides user with the boot screen often with multiple option to boot into; such as Microsoft Windows OS or an Ubuntu 18.04 OS, in an example of dual boot system.
Once the selection made, the boot loader loads the kernel into memory, supplies it with some perimeters and hands over the control to the kernel. A popular example of boot loader is GRUB2 (Grand Unified Bootloader version 2) [It is the primary boot loader for most of the current Linux distributions]
After the selected kernel is loaded into the memory, it is usually decompressed. The kernel then loaded into the memory and start executing. During this space the kernel carries out the task, such as initializing hardware and memory management task among other things. Once it is completely operational, the kernel looks for a init process to run, which sets up the user space and processes needed for the user environment.
In most of the Linux system, the init process then calls the systemd deamon. The systemd is responsible for bringing the Linux host to a usable state. systemd is responsible to mounting file system, starting and managing system services.
Previously System V was use as system initialization process. Also called SysV. e.g - RHEL 6, CentOS 6
systemd reduces system startup time over SysV by parallelizing the startup of services.
ls -l /sbin/init
to check init system used.
Runlevels
We can set to boot our system into different runlevels such as graphical, poweroff, reboot, nongraphical view.
There are different modes exist in which Linux can run, sets by run level.
runlevel
see the operation mode set in the system.
During boot, the init process checks the runlevel. It ensures all the program need to get the system operational in that mode are started. For example, the graphical user mode required to display manager service to run for the GUI to work. However, this services is not required for the non graphical mode.
systemd runlevel are called targets. Runlevel 5 is called the graphical target, whereas runlevel 3 called as multi user target.
systemctl get-default
to see the default runlevel.
ls -ltr /etc/systemd/system/default.target
or, ls -ltr /lib/systemd/system/default.target
to see the default runlevel file located at.
systemctl set-default multi-user.target
to change the default target to desired runlevel. Here, graphical to multi user (non graphical) [5 to 3].
The term runlevels is used in the sysV init systems. These have been replaced by systemd targets, in systemd based systems.
The complete list of runlevels and the corresponding systemd targets can be seen below:
runlevel 0 -> poweroff.target
runlevel 1 -> rescue.target
runlevel 2 -> multi-user.target
runlevel 3 -> multi-user.target
runlevel 4 -> multi-user.target
runlevel 5 -> graphical.target
runlevel 6 -> reboot.target
File types
Everything is a file in linux
- Every object in linux can be consider as a type of file. Even a directory, for example, is a special type of file.
Types of File:
Regular File → text, data, or images (e.g., configuration files, shell script or code, JPG files)
Directory → is a type of file that stores other files, and directories within (e.g, home directory)
Special Files
Character Files → represent devices under the /dev file system. allows the OS to communicate with IO devices serially (e.g., mouse and keyboard)
Block Files → representing block devices also located under /dev. A block device reads from and writes to the device in blocks or chunks of data. saw in
lsblk
command. (e.g., Hard disk and RAM).Links → is a way to associate tow or more file names to the same set of file data.
Hard Links → associate two or more file names that share the same block of data on the physical disk. Although behave as independent files, deleting one link will delete the data.
Soft Links → symbolic link or symlink can be loosely compared to a shortcut in windows. Act as pointers to another file. Deleting a symlink does not effect the data in the actual file.
Sockets Files → is a special file that enables the communication between two processes.
Named Pipes → is a special type of file that allows connecting one process as an input to another. Data flow in the pipes is unidirectional from the first process to the second.
file /home/arindam/
Identify different file types
Similar can be done using the following command ls -ld /home/arindam
Checking the first letter from the output of ls -l
command.
File System Hierarchy
/home → directories for the all user except for the root user.
/root → root user home directory located at.
/opt → want to install any third-party programs put them into /opt file system. (e.g., ideal location to install web application).
/mnt → used to mount file systems temporarily in the system.
/tmp → used to store temporary data
/media → all external media is mounted under the /media file system, such as USB drive.
df -hP
prints out details about all the mounted file systems.
/dev → contains the special block and character device file, such as external hard disk, mouse, keyboard.
/bin → basic programs and binaries such as ‘cp, mv, mkdir, date ..’ are located at.
/etc → stores most of the configuration file
/lib & /lib64 → stores shared libraries to be imported into your program.
/usr → in older systems, /usr was used for user home directories. For the modern linux system, all the UserLand applications and their data reside into /usr location. (e.g., Thunderbird mail client, Mozilla Firefox, and VI Test Editor, etc.)
/var → stores logs, and cached data
Resources
Subscribe to my newsletter
Read articles from Arindam Baidya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
