Table of Contents
Why should we compile the Linux kernel?
Compile a Linux kernel‍

Why does linux compile the kernel?

Mar 30, 2023 pm 02:03 PM
linux

Reason: The new kernel has revised the bugs of the old kernel and added many new features; if users want to use these new features, or want to customize a more efficient and stable system according to their own system kernel, you need to recompile the Linux kernel. Usually, newer kernels will support more hardware, have better process management capabilities, run faster and more stably, and will generally fix many vulnerabilities found in older versions. Frequently choose to upgrade to newer system kernels. It is a necessary operation content for Linux users.

Why does linux compile the kernel?

The operating environment of this tutorial: Ubuntu 18.04 system, Dell G3 computer.

Why should we compile the Linux kernel?

The new kernel has revised the bugs of the old kernel and added many new features. If users want to use these new features, or want to tailor a more efficient and stable kernel to their own system, they need to recompile the Linux kernel.

Usually, updated kernels will support more hardware, have better process management capabilities, run faster and more stably, and generally fix many vulnerabilities found in older versions, etc., frequently. Choosing to upgrade the updated system kernel is a necessary operation for Linux users.

In order to correctly and reasonably set the kernel compilation configuration options, so as to compile only the code for the functions required by the system, there are generally the following four considerations:

  • (1) Your own custom-compiled kernel runs faster (has less code)

  • (2) The system will have more memory (kernel parts will not be swapped into virtual memory)

  • (3) Compiling unnecessary functions into the kernel may increase the vulnerabilities that can be exploited by system attackers

  • (4) Putting some kind of Compiling functions into modules will be slower than compiling them into the kernel.

The purpose of this type of compilation is mainly to understand the Linux kernel compilation process through compilation and become familiar with the work of the kernel. principles, and you can even try to make some modifications.
Compilation only compiles the source code into a program. It will not replace the current system or affect the operation of the current system.

Compiling the kernel may be due to certain needs, such as kernel size requirements and removing some unused parts of the kernel. This scenario is often embedded systems.
Or if you have modified some part of the kernel code yourself, you need to verify the function after compilation.

When compiling the module, some functional modules are compiled into .ko. You can insmod xxx.ko to use the written code functions in the system without recompiling the kernel.
After compiling the kernel, the current kernel will not be replaced. The compiled new kernel is often in a directory similar to the following, and its name is mostly bzImage

/usr/src/kernels/3.xx.x-.x86_64/arch/x86/boot/
Copy after login

Then you can edit the system's grub list to add the latest kernel to use it.

The new kernel integrates new drivers, such as Intel core display: /lib/modules/`uname -r`/kernel/drivers/gpu/drm/i915/i915.ko

A system Multiple kernels can be installed, such as startup files, and the new kernel will not overwrite the old kernel:

/boot/vmlinuz-VERSION
/boot/initrd.img-VERSION
Copy after login

During the installation of a new kernel, some kernel modules need to be recompiled, such as VirtualBox:

/lib/modules/`uname -r`/updates/dkms/vboxdrv.ko
Copy after login

If new The kernel is not running properly. You can select the old kernel to start in the GRUB boot process.

You can also change back to the original kernel like this:

ln -sf /boot/vmlinuz-VERSION /vmlinuz
ln -sf /boot/initrd.img-VERSION /initrd.img
Copy after login

where VERSION is the version of the original kernel.

Compile a Linux kernel‍

The entire kernel compilation process is very simple, but kernel compilation takes a long time. This is mainly because the kernel has a lot of code. Of course, if your computer is powerful, the time will be much shorter. Another thing to note is that it is recommended to compile in a virtual machine environment to avoid errors that may cause system problems. If testing in a virtual machine, It is recommended that the size of the system partition and kernel source code partition be greater than 20GB.

Step 1: Download the source code

1. Go to the official website of the Linux kernel and download the latest version, or other versions of the kernel code. Here is a compressed archive of the source code.

Why does linux compile the kernel?

#2. Assuming that we are now in a Linux operating system, enter the following command on the command line to download the kernel.

wget 链接(由于链接会被认为是广告,本文省略链接,请自行复制)
Copy after login

You can see the download progress during the download process. After the download is completed, the information is roughly as follows.

Why does linux compile the kernel?

Step 2: Unzip the source code

After the compressed package is downloaded, it can be decompressed through the tar command.

tar xvf linux-5.9.6.tar.xz
Copy after login

在解压的时候可以看到文件列表,这个会很多,可能需要等一会儿。

Why does linux compile the kernel?

Step 3: 安装需要的软件包

安装编译工具以及其它一下依赖的软件包,在Ubuntu 18.04环境下执行如下命令。

sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison
Copy after login

如果一切顺利,大概安装输出信息如下所示。

Why does linux compile the kernel?

Step 4: 配置内核代码

在编译内核之前,我们可以对内核源代码进行配置。配置的目的主要是确定哪些模块会编译到内核当中。

1. 进入源代码目录

cd linux-5.9.6
Copy after login

2. 从当前机器的启动目录拷贝配置信息到源代码目录。这步操作的意思是我们编译内核的配置采用用当前环境一致的配置。

cp -v /boot/config-$(uname -r) .config
Copy after login
Why does linux compile the kernel?

3. 可以通过如下命令启动配置界面

make menuconfig
Copy after login

该命令会运行一些脚本,然后打开一个配置界面

Why does linux compile the kernel?

4. 下面是打开的配置界面。可以看出里面包含所有的内核组件,包括文件系统,网络,IO栈,虚拟化和设备驱动等等。如果你不熟悉,可以不做任何修改。

Why does linux compile the kernel?

Step 5: 编译内核

1. 通过如下命令就可以编译内核了

make -j 10
Copy after login

上面参数是并发数量,通常可以是CPU的2倍。

Why does linux compile the kernel?

2. 安装模块

sudo make modules_install
Copy after login
Why does linux compile the kernel?

3. 安装内核

sudo make install
Copy after login

安装完成后会有如下提示信息。

Why does linux compile the kernel?

Step 6: 重启,验证版本

当上述步骤都没有出错的情况下,我们重启一下计算机,然后运行如下命令。

uname -mrs
Copy after login

此时就可以看到内核版本已经是我们编译的版本了。

1Why does linux compile the kernel?

结论‍

通过上面几步,我们可以很简单的编译一个内核。如果后面开发内核模块,也是要基于内核代码树的,因此这个是内核开发的基础。

可能遇到的问题‍

编译内核的时候可能会遇到这个问题:

没有规则可制作目标
debian/certs/debian-uefi-certs.pem,由certs/x509_certificate_list需求停止

在要编译的内核目录下编辑一下配置文件即可。简单的方式是执行如下命令

vim .config
Copy after login

然后找到
CONFIG_SYSTEM_TRUSTED_KEYS,将其设置为空,也就是下面这个样子。

CONFIG_SYSTEM_TRUSTED_KEYS=”
Copy after login

相关推荐:《Linux视频教程

The above is the detailed content of Why does linux compile the kernel?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

See all articles