Hurry! 20% Off Ends SoonRegister Now

Linux Kernel Tutorial

Rating: 4.5
21265

If you are looking to gain knowledge on the Linux kernel and the different aspects associated with it, then you have landed at the right place. In this tutorial, we are going to look into the details of various topics that are associated with the Linux kernel.

Linux Kernel Tutorial - Table of Content

If you want to enrich your career and become a professional in Linux, then visit Mindmajix - a global online training platform: "Linux Online Certification" This course will help you to achieve excellence in this domain.

Linux Kernel Tutorial for Beginners

Before going into the details of the kernel tutorial, let's have a glance over the basics which would help us in acquiring a better understanding of the entire topic.

What is an Operating system?

An operating system is a software platform that creates an environment where a user can run different applications on a computing device. The operating system acts as a bridge between the software programs and the hardware components of a system.

It is utilized by different devices such as Mobiles, Tabs, desktops, web servers, video game consoles, etc. There are various Operating Systems available in the market, and Windows, Linux, Unix, and Mac OS X  are some of the examples.

Let's have a look at the components which are required for the functioning of an operating system.

  • The Bootloader: It takes care of the boot process of your device.
  • The Shell: A shell is a programming language that controls other files, processes, and controls all other programs as well.
  • The kernel: It is the main component of the OS and manages memory, CPU, and other related components.
  • Desktop Environment: Where a user interacts with.
  • Graphical server: It is a subsystem of OS and used for showing graphics on your screen
  • Applications: These are the set of programs that perform different user tasks such as word, excel, etc.            
  • Daemons: Provider of background services.

Learn Linux Tutorial for Beginners

What is Kernel?

A kernel is the critical component of an operating system. It works as a bridge between the applications and data processing at the hardware level with the help of its interprocess communication and system calls.

Whenever an operating system is loaded into memory, firstly, the kernel is loaded and stays there till the operating system gets shut down. The kernel is responsible for taking care of low-level tasks such as task management, memory management, risk management, etc.  

The kernel is responsible for:

  • Process management for application execution.
  • Memory and I/O (input/output) management.
  • System call control (core act of Kernel).  
  • Device management with the help of device drivers.

Linux operating system

Linux is an open-source platform developed

in 1991 by Linus Torvalds. It is used in a wide variety of devices as an operating system, like in computers, servers, mobiles, mainframes, and other embedded devices. As it is open-source software, users can customize this operating system according to their needs.

It supports almost every major computer platform such as ARM, x86, SPARK, etc. The most common usage of Linux is for Server, but it is also used in Desktop computers, ebook readers, smartphones, etc.

The Linux Kernel

A Kernel is the core component of any Linux based operating system. It represents the core aspect of the Linux distributions for Desktop computers and servers. It has a monolithic architecture, and the operating system operates entirely in the kernel space.

The monolithic kernel not only encircles the Central Processing Unit, IPC, and memory, but also has system server calls, device drivers, and file system management. Linux kernel works as a layer between the software and hardware of a device.

[ Related Article: Operating System in Linux

 MindMajix YouTube Channel

Operations of a Kernel

The kernel is termed as the heart of any operating system as it controls all other programs in a system. When a device starts, the kernel goes through a process called the initialization function, such as checking memory. It takes care of the memory allocation part and creates an environment for running the applications without any disturbances.

Kernel works as a service provider, so the programs can request the kernel for accomplishing multiple tasks such as requesting the use of the disk, network card, or other pieces of hardware, and also, kernel sets interrupt for the CPU to enable multitasking.

It protects the computational environment by not letting the faulty programs enter into the operational functions of others. It stops the unauthorized programs at the entrance by not allowing the memory space and limits the CPU time that they consume.    

Types of kernels

In general, we have three types of kernels, and they are,

  • Monolithic kernel: It contains many device drivers that create a communication interface between the hardware and software of a device.
  • Microkernel: It could only execute basic functionality.
  • Hybrid kernel: it combines the aspects of Monolithic kernel and Microkernel.
Types of Kernel

1. Monolithic kernel

It is a widely used kernel by operating systems. In a Monolithic architecture, the kernel consists of various modules that can dynamically be loaded and unloaded.  This kind of architecture would extend the capabilities of the OS and allows easy extensions to the kernel.

Maintenance of a kernel becomes easy with monolithic architecture because it allows a concerned module to load and unload when there is a need to fix a bug in a particular module.

So, it eliminates the tedious task of bringing down and recompiling the whole kernel for little changes. It is easier in the monolithic kernel to unload the module that is no more in usage.

2. MicroKernel

Microkernel has evolved as an alternative to the monolithic kernel to address the issue of the ever-growing size of kernel code which the monolithic kernel failed to do. This architecture allows some basic services like protocol stack, device driver management, file system, etc., to run in userspace. This could enhance the capability of OS with minimum code, improved security, and ensures stability.

It restricts the damages to the impacted areas by leaving the rest of the system to function correctly without any interruptions. In Microkernel architecture, all the basic OS services are available to programs via interprocess communication (IPC).  Microkernel allows direct interaction between the device drivers and hardware.

3. Hybrid kernel

The hybrid kernel can decide what it wants to run in user mode, and in supervisor mode. In the hybrid kernel environment, usually, things like device drivers, file system I/O would run in user mode, whereas server calls and IPC are kept in supervisor mode. It gives the best experience of both worlds.

Linux Interview Questions & Answers for Beginners

Kernel space and Userspace?

In the Linux operating system, the system memory is divided into two different regions: kernel space and userspace. Let's look into each region of memory and know the functionalities of both.

1. Kernel space   

Kernel space is found in an elevated state which provides full access to the hardware devices and protects the memory space. This memory space and user space together called Kernel-space. In a kernel space environment, core access to the system services and hardware are maintained and provided as a service to the rest of the system.   

2. User Space

The userspace or userland is a code that runs outside the operating system kernel environment.

Userspace is defined as various applications or programs or libraries that an operating system uses to connect with the kernel. Because of the complicated process to access the memory, malicious functions can be restricted only to the user system.  

What is Linux ABI?

It is nothing but kernel userspace ABI (application binary user interface). It exists between program modules. ABIs are used to access the codes that are compiled and ready for usage.

ABI is an interface between two binary program modules: one of these modules is an operating system facility or library, and the second one is a program run by a user.

The Linux Loadable Kernel Module

If you want to add code to the Linux kernel, the first thing you need to do is to add some source files to the kernel source tree. There may be situations where you are required to add code to the kernels while it is running, this process is called a loadable kernel module.

The benefits of LKMs (Linux Loadable Kernel Module)

  • LKM saves time and avoids errors.
  • It helps in finding the bugs quickly.
  • LKMs save the memory because they are loaded into memory only when required.
  • It offers faster maintenance and debugging time.

Linux kernel interfaces:

The Linux Kernel provides different interfaces to the user-space applications that execute different tasks and have different properties.  It consists of two separate Application Programming Interfaces (APIs): one is kernel userspace, and the other is kernel internal.

Kernel user space is the Linux API userspace and allows the programs in the user space into system services and resources of the kernel.  

Major Subsystems of the Linux Kernel

Below mentioned are some of the subsystems of the Linux kernel. Let's discuss them one by one in detail.

System call interface:

A system call is a programmatic process in which a program requests a service from the kernel of an operating system. It includes various hardware services such as connecting with hardware devices and creating a communication interface among the integral parts of the Kernel. System call creates an efficient interface between an operating system and a process.   

Process management:

The Kernel takes care of creating and destroying the different processes and monitors their connection to the outside world such as input and output.  It handles the communication between different methods via signals, interprocess communication primitive, or pipes. In addition to all these, it also has a scheduler that controls the processes in sharing the CPU.  

1. Memory management:

Memory is a vital component of an Operating system and the kernel takes care of it.  Linux manages the available memory and hardware mechanisms for virtual and physical mappings.

Memory management isn't just managing 4KB buffers, and it is much more than that. Linux also provides abstractions other than 4kb buffers, known as a slab allocator.

 Slab allocator uses the 4kb buffer as its base but then allocates structures from inside by monitoring things like, which pages are full, empty, and partially used.

This allows the scheme to grow dynamically and in supporting the more significant needs of the system.  

2. Virtual file system:

Virtual file system (VFS) is an important integral part of the kernel and facilitates common interface abstraction for the file system. The VFS creates a switching layer between the file system supported by the kernel and SCI (System Call Interface).

In addition to the above things, Linux supports various types of file systems that require different ways of organizing data to store in physical format. For instance, a disk can be formatted with the commonly used FAT file system, or Linux standard ext3 file system, or several others.

3. Device Drivers:

A vast part of the source code of the kernel is stored in the device drivers, and that makes a specific hardware device usable. The Linux provides a driver subdirectory that is further divided into various devices that are supported, such as I2C, Bluetooth, serial, etc.

4. Architecture-dependent code:

Even though much of the Linux runs on its independent architecture, some elements should be considered for the architecture efficiency and normal operation.

Linux has many subdirectories, and each architecture subdirectory has many numbers of other subsidiaries. And, these subdirectories focus on the specific tasks of the kernel such as memory management, boot, kernel, etc.

5. Upgrading the Kernel:

As we are aware of the concept called an update, we do have that option in the kernel to update it from the older version to a newer one.

The retention of old configurations is significant, and to achieve this, one has to back up the configuration file in the kernel source directory. If anything goes wrong while updating the kernel, follow the below steps.

  • Download the latest source code from the kernel.org main page.  
  • Apply variations to the old version tree to make it a new one.
  • Reconfigure the kernel on the basis of the older kernel configuration file that you had backed up.
  • Develop the new kernel.
  • Now, you can install the latest kernel in your system.

Linux Kernel system (file system)

The files and systems in the Linux kernel system are where most of the users find difficulties, majorly because it is hard to tell which files are in which directories if you don’t have the knowledge. For this reason, we shall try to look at the organization of the file systems.

We shall also learn how to create, delete, move, and rename directories. Additionally, we shall learn how to edit files and change permissions.

The file system layout

The UNIX file system can aptly fit into a one-line description; “Everything on a UNIX system that is not a process is a file”. This statement holds true for files that are a little bit more than just files. Therefore, a Linux system does not differentiate between a file and directory mainly because the directory, in essence, is a file containing names of other files, services, texts, images, and programs.

Additionally, a Linux system also treats input and output devices as files. The general understanding is that the files are a sort of in a tree structure on the main hard drive; this is for easy management and order. Most of the files on a Linux system are regular files, regardless of the data they hold, be it programs, executable files, or normal data.

While we have already said that everything in a Linux system is a file, there is a general understanding that there are some exceptions. For instance:

(a) Directories: A file list of other files

(b) Special files: These are the mechanisms used for input and output. Special files are in /dev.

(c) Links: This is a system to make a file, including a directory visible in various parts of the “system tree”.

(d) Domain (sockets): These are special types of files similar to the IP/TCP sockets. These files are protected by the file system access control and they provide inter-process networking.

Named pipes: These types of files are the bridge between processes. They are more or less the same as sockets and enhance communication between processes without the use of networks or sockets semantics.

Remember that I had indicated that most computer users generalize that the file system is more or less like a tree, here is a good example of a Linux file system tree.

Domain (sockets)

It is important to note that depending on the UNIX system in use, the file system tree may change; some files and directories may change.

The file system tree starts at the slash or the trunk, which, if you look at our table is the (/) forward slash. This is what we call the root directory; it is the underlying directory for all files.

Directories are one level below the slash or root directory often have the slash in their proceeding names to indicate their position and to prevent confusion with other files or directories with similar names.

A question that plagues most Linux users is where programs and program files are stored when they are installed on the system. Let us examine this for a minute.

Linux uses two partitions: The data partition where the system data, including the root directories and all system resources required to start the system, are located, and the swap partition, which is an expansion of the physical memory on the computer.

All files (including programs) are stored in this root directory in accordance with the Linux tree file system we have already looked at.

[ Related Article: Linux Functions & Commands

Manipulating files

To show file names, properties, date of creation, permission, type, size, link files, and owners, the Is command is the easiest way.

Creating and deleting files and directories on your system is very important when you want to create new files or delete redundant directories to free up space. Because the graphical interface is much or less than MS-DOS, creating files is not that difficult. Deleting files, on the other hand, is moderately difficult.

There are some popular file managers for the GNU/Linux, with most of them being executable files that are accessible from the desktop manager, home directory icon, or the command line using the following commands.

[ Related Article: Best examples of Linux networking commands ]

Managing files

Nautilus: This is the default file manager in the Gnome GNU desktop. There are very useful resources on how to use this tool online. 

Konqueror: This file manager is typical in KDE desktops. 

MC: Code named Midnight Commander is fashioned from the Norton Commander. 

For easier file management, the above applications are worth the time of reading through the documentation and the effort.

It is also important to note that there are many more file management applications, but these are the most popular and have a moderate difficulty level. Additionally, these tools optimize the UNIX commands in a specific manner.

To keep files and things in one place, you must allocate specific file default locations by creating directories and subdirectories for them. You can do this by using the mkdir command. For instance:

john:~> cd archive
 john:~/archive> mkdir 1999 2000 2001
 john:~/archive>ls
 1999/ 2000/ 2001/
 john:~/archive>mkdir 2001/reports/Suppliers-Industrial/
 mkdir: cannot create directory `2001/reports/Suppliers-Industrial/':

Additionally, you can create subdirectories easily in one-step by using the – p option. For instance:

john:~> cd archive
 john:~/archive> mkdir 1999 2000 2001
 john:~/archive>ls
 1999/ 2000/ 2001/
 john:~/archive>mkdir 2001/reports/Suppliers-Industrial/
 mkdir: cannot create directory `2001/reports/Suppliers-Industrial/':

No such file or directory

john:~/archive>mkdir -p 2001/reports/Suppliers-Industrial/
 john:~/archive>ls 2001/reports/
 Suppliers-Industrial/:

File permissions

In some instances, you will find that the file needs more or other permission not included in the file creation permission; this is called an access right.

Access rights are set using the same mkdir command. It is important to note that there are rules on how to name a directory. For instance, in one directory, you cannot have two files with the same name.

However, it is important to note that Linux, as well as UNIX, are case sensitive systems (you can have two file names with you and YOU in the same directory).

Additionally, there are no limits to the length of a filename, so naming files should be a breeze. You can also use special characters in the file names as long as those characters do not hold a special meaning to the shell.

Moving files

Moving unclassified files uses the mv command.

john:~/archive> mv ../report[1-4].doc reports/Suppliers-Industrial/

The same command is also in use when we are renaming files

The command can also come in handy if you want to rename files:

                                       Command

In the above example, we can see that only the file name changes, and all the other properties don’t change.

Copying files

The cp command is used to copy directories and files. There is also a very useful option of copying all underlying subdirectories and files (recursive copy) which uses the –R. Here is a look at the general syntax.

cp [-R] fromfile tofile

Removing or deleting files

The command rm comes into play when you want to remove single files, while the command rmdir plays its role in removing empty directories. It is important to note that some directories are undeletable (.dot and ..dot) because they are necessary for the proper ranking of a directory in the tree hierarchy.

Like UNIX, Linux does not have a garbage can (recycle bin) and once you remove a file, that is it, it is gone and you cannot get it back unless you have a backup. To protect against this, sometimes “mistake delete”, you can activate the interactive behavior of the cp, mv, and rm commands by using the -i option.

When the –i option is active, the system does not execute a command such as delete immediately; instead, it prompts for confirmation, which needs that stroke of a key or an additional click to execute the command fully. 

Related Article: Unix Interview Questions

Conclusion

The Linux Kernel plays an important role in resource allocation to different applications.  The kernel acts as a centralized place to connect the hardware and software and runs the applications in a system. Linux kernel has got more popularity due to its open-source nature.

Users can customize this OS according to their requirements. Therefore, it’s been used by a wide variety of devices.  

The modular characteristic of the Linux kernel allows a wide range of modifications without rebooting the system. The  Flexibility of the kernel enables its users to perform their level best. Moreover, the monolithic nature of this kernel has greater computational power than the microkernel.

If you are interested to learn Linux and become a certified developer in it. Then check out our certified Linux training courses near your cities.

Linux training Hyderabad  

Linux training Bangalore

These courses are incorporated with live instructor-led training, industry use cases, and hands-on live projects. This training program will make you an expert in Microsoft Azure and help help you to achieve your dream job 

List of Other Linux Blogs:

Linux File PermissionsLinux Advanced Functions And Commands
Introduction to Linux Operating SystemLinux Networking Commands with Examples
What is Linux?Top 10 Reasons Why You Should Learn Linux
What is Linux Operating System?Linux Commands for Beginners

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Linux TrainingDec 24 to Jan 08View Details
Linux TrainingDec 28 to Jan 12View Details
Linux TrainingDec 31 to Jan 15View Details
Linux TrainingJan 04 to Jan 19View Details
Last updated: 03 Apr 2023
About Author

Vinod M is a Big data expert writer at Mindmajix and contributes in-depth articles on various Big Data Technologies. He also has experience in writing for Docker, Hadoop, Microservices, Commvault, and few BI tools. You can be in touch with him via LinkedIn and Twitter.

read less