How to compile and install Linux Kernel 5.1.2 from source code

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.1.2 under an Ubuntu or Debian Linux. The following instructions successfully tested on an RHEL 7/CentOS 7 (and clones), Debian Linux, Ubuntu Linux and Fedora Linux 28/29. However, instructions remain the same for any other Linux distribution.

How to compile and install Linux Kernel 5.1.2

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. Verify kernel
  3. Untar the kernel tarball
  4. Copy existing Linux kernel config file
  5. Compile and build Linux kernel 5.1.2
  6. Install Linux kernel and modules (drivers)
  7. Update Grub configuration
  8. 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“:

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.1.2.tar.xz represents Linux kernel version 5.1.2. Use the wget command to download Linux kernel source code:
$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.1.2.tar.xz

Step 2. 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.1.2.tar.xz
OR
$ xz -d -v linux-5.1.2.tar.xz

Verify Linux kernel tartball with pgp

First grab the PGP signature for linux-5.1.2.tar:
$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.1.2.tar.sign
Try to verify it:
$ gpg --verify linux-5.1.2.tar.sign
Sample outputs:

gpg: assuming signed data in 'linux-5.1.2.tar'
gpg: Signature made Sun 12 Aug 2018 04:00:28 PM CDT
gpg: using RSA key 79BE3E4300411886
gpg: Can't check signature: No public key

Grab the public key from the PGP keyserver in order to verify the signature i.e. RSA key ID 79BE3E4300411886 (from the above outputs):
$ gpg --recv-keys 79BE3E4300411886
Sample outputs:

gpg: key 79BE3E4300411886: 7 duplicate signatures removed
gpg: key 79BE3E4300411886: 172 signatures not checked due to missing keys
gpg: /home/vivek/.gnupg/trustdb.gpg: trustdb created
gpg: key 79BE3E4300411886: public key "Linus Torvalds <torvalds@kernel.org>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg: imported: 1

Now verify gpg key again with the gpg command:
$ gpg --verify linux-5.1.2.tar.sign
Sample outputs:

gpg: assuming signed data in 'linux-5.1.2.tar'
gpg: Signature made Sun 12 Aug 2018 04:00:28 PM CDT
gpg: using RSA key 79BE3E4300411886
gpg: Good signature from "Linus Torvalds <torvalds@kernel.org>" [unknown]
gpg: aka "Linus Torvalds <torvalds@linux-foundation.org>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg: There is no indication that the signature belongs to the owner.
Primary key fingerprint: ABAF 11C6 5A29 70B1 30AB E3C4 79BE 3E43 0041 1886

If you do not get “BAD signature” output from the “gpg –verify” command, untar/extract the Linux kernel tarball using the tar command, enter:
$ tar xvf linux-5.1.2.tar

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.1.2
$ cp -v /boot/config-$(uname -r) .config

Sample outputs:

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

Step 4. 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

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 5. 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       # 最好在general setup中设置下你定义的名称,方便识别

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.

Step 5. 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.1.2.img
  • System.map-5.1.2
  • vmlinuz-5.1.2

Step 6. 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.1.2   #安装自己定义的名称来设置

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.1.2
$ sudo update-grub

How to build and install the latest Linux kernel from source code

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.1.2 x86_64

Conclusion – Linux Compile Kernel version 5.1.2

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.

提示:

make config : Ask me all options from the new kernel source.
make defconfig : It will auto answer the defaults for each options in new kernel source.
make oldconfig : Ask me the new options that is not present in current kernel.
make olddefconfig : It will auto answer the defaults for new options.
make menuconfig : Text baseed GUI to make some tweaks in .config file.

最新文章

  1. jQuery中怎样阻止后绑定事件
  2. 【JAVA正则表达式综合练习】
  3. 《统计推断(Statistical Inference)》读书笔记——第2章 变换与期望
  4. VS问题汇总——竹子整理
  5. 图片轮播(左右切换)--JS原生和jQuery实现
  6. Android2.2快速入门 zz
  7. maven在windows环境下加载settings.xml文件
  8. HDU5092——Seam Carving(动态规划+回溯)(2014上海邀请赛重现)
  9. 《C++ Primer 4th》读书笔记 第8章-标准IO库
  10. Asteroids(二分图最大匹配模板题)
  11. 监控工具cacti
  12. windows程序设计读书笔记4——字符显示3
  13. require.js学习笔记
  14. Socket编程实践(7) --Socket-Class封装(改进版v2)
  15. 16进制字符串转QByteArray,char转16进制字符串
  16. dbus-glib 和 GDBus 的区别
  17. python内建的命名空间研究
  18. Luogu P2575 高手过招
  19. OKHttpUtil工具类
  20. 堆(heap)、栈(stack)、方法区(method)

热门文章

  1. encodeURIComponent()加密、decodeURIComponent()解码及v-html将字符串转换为html
  2. AT2371 Placing Squares
  3. WEB Fuzz中需要关注的7种响应
  4. 使用maven如何生成源代码的jar包
  5. 洛谷P4127同类分布
  6. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第4节 ArrayList集合_17-ArrayList练习二_存储自定义
  7. axios 拦截以及 API简单配置(element)
  8. Python 使用openpyxl导出Excel表格的时候,使用save()保存到指定路径
  9. gradle implementation runtimeOnly 和api 区别
  10. 为什么说 Babel 将推动 JavaScript 的发展【转】