Step by step compile and install Linux Kernel from source code 源码编译定制安装Linux内核

Compiling a custom kernel has its advantages and disadvantages. However, new Linux user/admin find it difficult to compile Linux kernel. Compiling kernel needs to understand few things and then type a couple of commands. This step by step howto covers compiling Linux kernel version 5.16.9 under an Ubuntu or Debian Linux. The following instructions successfully tested on an RHELCentOS 7/8 (and clones), Debian Linux, Ubuntu Linux and Fedora Linux 31/32. However, instructions remain the same for any other Linux distribution.

The procedure to build (compile) and install the latest Linux kernel from source is as follows:

  1. Grab the latest kernel from kernel.org
  2. Install the required compilers and other tools
  3. Configure the Linux kernel features and modules
  4. Compile and install Linux Kernel
  5. Update Grub configuration
  6. Reboot the system

Let us see all steps in details.

Step 1. Get the latest Linux kernel source code

Visit the official project site and download the latest source code. Click on the big yellow button that read as “Latest Stable Kernel“:

Download Linux Kernel Source Code

The filename would be linux-x.y.z.tar.xz, where x.y.z is actual Linux kernel version number. For example file linux-5.16.9.tar.xz represents Linux kernel version 5.16.9. Use the wget command to download Linux kernel source code:

$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.16.9.tar.xz

Extract tar.xz file

You really don’t have to extract the source code in /usr/src. You can extract the source code in your $HOME directory or any other directory using the following unzx command or xz command:
$ unxz -v linux-5.16.9.tar.xz
OR
$ xz -d -v linux-5.16.9.tar.xz

untar/extract the Linux kernel tarball using the tar command, enter:
$ tar xvf linux-5.16.9.tar

Step 2. Install the required compilers and other tools

You must have development tools such as GCC compilers and related tools installed to compile the Linux kernel.

How to install GCC and development tools on a Debian/Ubuntu Linux

Type the following apt command or apt-get command to install the same:

$ sudo apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev

See “Ubuntu Linux Install GNU GCC Compiler and Development Environment” for more info.

How to install GCC and development tools on a CentOS/RHEL/Oracle/Scientific Linux

Try yum command:
$ sudo yum group install "Development Tools"
OR
$ sudo yum groupinstall "Development Tools"
Additional packages too:

$ sudo yum install ncurses-devel bison flex elfutils-libelf-devel openssl-devel

Update gcc 7.3

yum -y install centos-release-scl
yum -y install devtoolset-7-gcc devtoolset-7-gcc-c++ devtoolset-7-binutils
scl enable devtoolset-7 bash

You can confirm gcc version with the following command:

gcc -v

How to install GCC and development tools on a Fedora Linux

Run the following dnf command:

$ sudo dnf group install "Development Tools"
$ sudo dnf install ncurses-devel bison flex elfutils-libelf-devel openssl-devel

Step 3. Configure the Linux kernel features and modules

Before start building the kernel, one must configure Linux kernel features. You must also specify which kernel modules (drivers) needed for your system. The task can be overwhelming for a new user. I suggest that you copy existing config file using the cp command:
$ cd linux-5.16.9
$ cp -v /boot/config-$(uname -r) .config

Sample outputs:

'/boot/config-4.15.0-30-generic' -> '.config'

Configuring the kernel

Now you can start the kernel configuration by typing any one of the following command in source code directory:

  • make menuconfig – Text based color menus, radiolists & dialogs. This option also useful on remote server if you wanna compile kernel remotely.
  • make xconfig – X windows (Qt) based configuration tool, works best under KDE desktop
  • make gconfig – X windows (Gtk) based configuration tool, works best under Gnome Dekstop.

For example, run make menuconfig command launches following screen:
$ make menuconfig

How to compile and install Linux Kernel 5.16.9

You have to select different options as per your need. Each configuration option has HELP button associated with it so select help button to get help. Please note that ‘make menuconfig’ is optional. I used it here to demonstration purpose only. You can enable or disable certain features or kernel driver with this option. It is easy to remove support for a device driver or option and end up with a broken kernel. For example, if the ext4 driver is removed from the kernel configuration file, a system may not boot. When in doubt, just leave support in the kernel.

Strongly suggest you to copy the running Kernel configuration (.config) from the /boot directory to new kernel directory, and skip configure the Kernel, when you just update the host Kernel.

Step 4. How to compile a Linux Kernel

Start compiling and tocreate a compressed kernel image, enter:
$ make
To speed up compile time, pass the -j as follows:
## use 4 core/thread ##
$ make -j 4
## get thread or cpu core count using nproc command ##
$ make -j $(nproc)

Compiling and building the Linux kernel going take a significant amount of time. The build time depends upon your system’s resources such as available CPU core and the current system load. So have some patience.

Install the Linux kernel modules

$ sudo make modules_install

Install the Linux kernel

So far we have compiled the Linux kernel and installed kernel modules. It is time to install the kernel itself:
$ sudo make install

It will install three files into /boot directory as well as modification to your kernel grub configuration file:

  • initramfs-5.16.9.img
  • System.map-5.16.9
  • vmlinuz-5.16.9

Step 5. Update grub config

You need to modify Grub 2 boot loader configurations. Type the following command at a shell prompt as per your Linux distro:

CentOS/RHEL/Oracle/Scientific and Fedora Linux

$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg
$ sudo grubby --set-default /boot/vmlinuz-5.16.9

You can confirm the details with the following commands:
grubby --info=ALL | more
grubby --default-index
grubby --default-kernel

Debian/Ubuntu Linux

The following commands are optional as make install does everything for your but included here for historical reasons only:
$ sudo update-initramfs -c -k 5.16.9
$ sudo update-grub

Step 6. Reboot the system and Verify new Linux kernel version

You have compiled a Linux kernel. The process takes some time, however now you have a custom Linux kernel for your system. Let us reboot the system.

Reboot Linux computer and boot into your new kernel

Just issue the reboot command or shutdown command:
# reboot
Verify new Linux kernel version after reboot:
$ uname -mrs
Sample outputs:

Linux 5.16.9 x86_64

Conclusion – Linux Compile Kernel version 5.16.9

Configurations! You completed various steps to build the Linux kernel from source code and compiled kernel should be running on your system. I strongly suggest that you always keep backup of essential data and visit the kernel.org page here for more info.

未经允许随便转载:看过够 » Step by step compile and install Linux Kernel from source code 源码编译定制安装Linux内核

赞 (0) 打赏

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏