{"id":143,"date":"2016-01-09T16:04:04","date_gmt":"2016-01-09T16:04:04","guid":{"rendered":"http:\/\/info.ffteixeira.net\/2016\/01\/09\/compile-and-install-linux-kernel-v4-2-source-on-a-debian-ubuntu-linux\/"},"modified":"2016-01-09T16:04:04","modified_gmt":"2016-01-09T16:04:04","slug":"compile-and-install-linux-kernel-v4-2-source-on-a-debian-ubuntu-linux","status":"publish","type":"post","link":"https:\/\/blog.ffteixeira.net\/?p=143","title":{"rendered":"Compile and Install Linux Kernel v4.2 Source On a Debian \/ Ubuntu Linux"},"content":{"rendered":"<p>In order to create a custom kernel configuration file and build a custom kernel, the full Linux kernel source tree must first be downloaded and installed. The latest Linux kernel stable version is 4.2. In this tutorial, you will learn how to compile the Linux kernel version 4.2 on a Debian and Ubuntu Linux operating system and build .deb file.<\/p>\n<p>&nbsp;<\/p>\n<h2>Why build a custom kernel?<\/h2>\n<p>Compiling a custom Linux kernel has its own advantages and disadvantages. In order to change the kernel&#039;s behavior, one had to compile and then reboot into a new Linux. Most of the the functionality in the Linux kernel is contained in modules which can be dynamically loaded and unloaded from the kernel as necessary. Some benefits of a custom Linux kernel:<\/p>\n<ol>\n<li>Support a wide range of hardware including the latest hardware.<\/li>\n<li>Remove unwanted drivers from the kernel.<\/li>\n<li>Faster boot time due to small kernel size.<\/li>\n<li>Increased security due to disabled unneeded modules.<\/li>\n<li>Learning about the kernel and advanced usage.<\/li>\n<li>Always run the cutting edge latest kernel.<\/li>\n<li>Lower memory usage.<\/li>\n<\/ol>\n<h2>Prerequisites<\/h2>\n<p>You need to install the following packages on a Debian or Ubuntu Linux to compiler the Linux kernel:<\/p>\n<ul>\n<li><strong>git<\/strong> : Fast, scalable, distributed revision control system. You can grab the latest source code using the git command.<\/li>\n<li><strong>fakeroot<\/strong> : Tool for simulating superuser privileges. Useful to build .deb files.<\/li>\n<li><strong>build-essential<\/strong> : <a href=\"http:\/\/www.cyberciti.biz\/faq\/debian-linux-install-gnu-gcc-compiler\/\">Tools for building the Linux kernel such as GCC compiler and related tools<\/a> on a Debian or Ubuntu Linux based system.<\/li>\n<li><strong>ncurses-dev<\/strong> : <a href=\"http:\/\/www.cyberciti.biz\/faq\/linux-install-ncurses-library-headers-on-debian-ubuntu-centos-fedora\/\">Developer&#039;s libraries for ncurses. This is used by menuconfig<\/a> while configuring the kernel options.<\/li>\n<li><strong>kernel-package<\/strong> : Utility for building Linux kernel related Debian packages.<\/li>\n<li><strong>xz-utils<\/strong> : XZ-format compression utilities to decompress the Linux kernel tar ball.<\/li>\n<li><strong>Disk space<\/strong> : 10 GB or more free disk space.<\/li>\n<li><strong>Time<\/strong> : Kernel compilation may take quite a while, depending on the power of your machine.<\/li>\n<\/ul>\n<h2>Install required packages<\/h2>\n<p>Open the terminal application. Type the following <a href=\"http:\/\/www.cyberciti.biz\/tips\/linux-debian-package-management-cheat-sheet.html\" title=\"See Linux\/Unix apt-get command examples for more info\">apt-get command<\/a> to install the required packages for building the Linux kernel:<br \/><code>$ sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils<\/code><br \/>Sample outputs:<\/p>\n<div class=\"wp-caption aligncenter\" id=\"attachment_145218\" style=\"width: 608px\"><img loading=\"lazy\" decoding=\"async\" alt=\"Fig.01: Install gcc and friends\" class=\"size-full wp-image-145218\" height=\"577\" src=\"http:\/\/s0.cyberciti.org\/uploads\/faq\/2015\/09\/git-fackroot-build-essential-ncurese-dev-xz-utils.jpg\" width=\"598\" \/><\/p>\n<p class=\"wp-caption-text\">Fig.01: Install gcc and friends<\/p>\n<\/div>\n<p><\/p>\n<p>Finally, install the kernel-package package too:<br \/><code>$ sudo apt-get install kernel-package<\/code><br \/>OR<br \/><code>$ sudo apt-get --no-install-recommends install kernel-package<\/code><br \/>Sample outputs:<\/p>\n<div class=\"wp-caption aligncenter\" id=\"attachment_145220\" style=\"width: 608px\"><img loading=\"lazy\" decoding=\"async\" alt=\"Fig.02: Install utility for building Linux kernel \" class=\"size-full wp-image-145220\" height=\"303\" src=\"http:\/\/s0.cyberciti.org\/uploads\/faq\/2015\/09\/kernel-package.jpg\" width=\"598\" \/><\/p>\n<p class=\"wp-caption-text\">Fig.02: Install utility for building Linux kernel<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<h2>Download the Linux kernel source code<\/h2>\n<p>Type the following wget command:<br \/><code>$ https:\/\/cdn.kernel.org\/pub\/linux\/kernel\/v4.x\/linux-4.2.5.tar.xz<\/code><br \/>Sample outputs:<\/p>\n<div class=\"wp-caption aligncenter\" id=\"attachment_145214\" style=\"width: 608px\"><img loading=\"lazy\" decoding=\"async\" alt=\"Fig.03: Use the wget to grab the latest source code from kernel.org\" class=\"size-full wp-image-145214\" height=\"171\" src=\"http:\/\/s0.cyberciti.org\/uploads\/faq\/2015\/09\/wget-linux-kernel-4.2-source-code.jpg\" width=\"598\" \/><\/p>\n<p class=\"wp-caption-text\">Fig.03: Use the wget to grab the latest source code from kernel.org<\/p>\n<\/div>\n<p><\/p>\n<p>Untar the Linux kernel tar ball using the tar command enter:<br \/><code>$ tar xvf linux-4.2.5.tar.xz<br \/>$ ls<br \/>$ cd linux-4.2.5<br \/>$ ls<\/code><br \/>Sample outputs<\/p>\n<p>&nbsp;<\/p>\n<pre>\nlinux-4.2\/\nlinux-4.2.5\/.get_maintainer.ignore\nlinux-4.2.5\/.gitignore\nlinux-4.2.5\/.mailmap\nlinux-4.2.5\/COPYING\nlinux-4.2.5\/CREDITS\nlinux-4.2.5\/Documentation\/\nlinux-4.2.5\/Documentation\/00-INDEX\nlinux-4.2.5\/Documentation\/ABI\/\nlinux-4.2.5\/Documentation\/ABI\/README\nlinux-4.2.5\/Documentation\/ABI\/obsolete\/\nlinux-4.2.5\/Documentation\/ABI\/obsolete\/proc-sys-vm-nr_pdflush_threads\n.....\n..\n....\nlinux-4.2.5\/virt\/kvm\/async_pf.c\nlinux-4.2.5\/virt\/kvm\/async_pf.h\nlinux-4.2.5\/virt\/kvm\/coalesced_mmio.c\nlinux-4.2.5\/virt\/kvm\/coalesced_mmio.h\nlinux-4.2.5\/virt\/kvm\/eventfd.c\nlinux-4.2.5\/virt\/kvm\/irqchip.c\nlinux-4.2.5\/virt\/kvm\/kvm_main.c\nlinux-4.2.5\/virt\/kvm\/vfio.c\nlinux-4.2.5\/virt\/kvm\/vfio.h\n\n<span>linux-4.2.5  linux-4.2.5.tar.xz<\/span>\n\n<span>arch     crypto         fs       Kbuild   MAINTAINERS  README          security  virt\nblock    Documentation  include  Kconfig  Makefile     REPORTING-BUGS  sound\nCOPYING  drivers        init     kernel   mm           samples         tools\nCREDITS  firmware       ipc      lib      net          scripts         usr<\/span><\/pre>\n<h2>Configure the Linux kernel<\/h2>\n<p>First, copy your existing Linux kernel config file<br \/><code>$ cd linux-4.2.5<br \/>$ cp \/boot\/config-$(uname -r) .config<\/code><br \/>To configure the kernel, run:<br \/><code>$ make menuconfig<\/code><br \/>Sample outputs:<\/p>\n<p>#gallery-1 {<br \/>\n\t\t\t\tmargin: auto;<br \/>\n\t\t\t}<br \/>\n\t\t\t#gallery-1 .gallery-item {<br \/>\n\t\t\t\tfloat: left;<br \/>\n\t\t\t\tmargin-top: 10px;<br \/>\n\t\t\t\ttext-align: center;<br \/>\n\t\t\t\twidth: 50%;<br \/>\n\t\t\t}<br \/>\n\t\t\t#gallery-1 img {<br \/>\n\t\t\t\tborder: 2px solid #cfcfcf;<br \/>\n\t\t\t}<br \/>\n\t\t\t#gallery-1 .gallery-caption {<br \/>\n\t\t\t\tmargin-left: 0;<br \/>\n\t\t\t}<br \/>\n\t\t\t\/* see gallery_shortcode() in wp-includes\/media.php *\/\n<\/p>\n<div class=\"gallery galleryid-145213 gallery-columns-2 gallery-size-medium\" id=\"gallery-1\">\n<dl class=\"gallery-item\">\n<dt class=\"gallery-icon landscape\"><a href=\"http:\/\/www.cyberciti.biz\/faq\/debian-ubuntu-building-installing-a-custom-linux-kernel\/make-menuconfig\/\"><img loading=\"lazy\" decoding=\"async\" alt=\"Fig.04: Starting menuconfig\" aria-describedby=\"gallery-1-145221\" class=\"attachment-medium size-medium\" height=\"175\" src=\"http:\/\/s0.cyberciti.org\/uploads\/faq\/2015\/09\/make-menuconfig-300x175.jpg\" width=\"300\" \/><\/a><\/dt>\n<dd class=\"wp-caption-text gallery-caption\" id=\"gallery-1-145221\">Fig.04: Starting menuconfig<\/dd>\n<\/dl>\n<dl class=\"gallery-item\">\n<dt class=\"gallery-icon portrait\"><a href=\"http:\/\/www.cyberciti.biz\/faq\/debian-ubuntu-building-installing-a-custom-linux-kernel\/kernel-config\/\"><img loading=\"lazy\" decoding=\"async\" alt=\"Fig.05: Select Linux kernel config options and drivers to build\" aria-describedby=\"gallery-1-145219\" class=\"attachment-medium size-medium\" height=\"300\" src=\"http:\/\/s0.cyberciti.org\/uploads\/faq\/2015\/09\/kernel-config-294x300.jpg\" width=\"294\" \/><\/a><\/dt>\n<dd class=\"wp-caption-text gallery-caption\" id=\"gallery-1-145219\">Fig.05: Select Linux kernel config options and drivers to build<\/dd>\n<\/dl>\n<\/div>\n<p><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p class=\"alert\"><strong><span>WARNING<\/span><\/strong>: 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.<\/p>\n<p>Make sure you save the changes before exit from menuconfig.<\/p>\n<h2>Compile the Linux kernel<\/h2>\n<p>You need to clean the source tree and reset the kernel-package parameters, type:<br \/><code>$ make-kpkg clean<\/code><br \/>Sample outputs:<\/p>\n<div class=\"wp-caption aligncenter\" id=\"attachment_145217\" style=\"width: 608px\"><img loading=\"lazy\" decoding=\"async\" alt=\"Fig.06: Run make-kpkg command\" class=\"size-full wp-image-145217\" height=\"224\" src=\"http:\/\/s0.cyberciti.org\/uploads\/faq\/2015\/09\/make-kpkg-clean.jpg\" width=\"598\" \/><\/p>\n<p class=\"wp-caption-text\">Fig.06: Run make-kpkg command<\/p>\n<\/div>\n<p><\/p>\n<p>Now, you can compile the kernel, run:<br \/><code>$ fakeroot make-kpkg --initrd --revision=1.0.NAS kernel_image kernel_headers<\/code><br \/>Sample outputs:<\/p>\n<div class=\"wp-caption aligncenter\" id=\"attachment_145222\" style=\"width: 608px\"><img loading=\"lazy\" decoding=\"async\" alt=\"Fig.07: Start compiling the kernel\" class=\"size-full wp-image-145222\" height=\"259\" src=\"http:\/\/s0.cyberciti.org\/uploads\/faq\/2015\/09\/kernel-compile.jpg\" width=\"598\" \/><\/p>\n<p class=\"wp-caption-text\">Fig.07: Start compiling the kernel<\/p>\n<\/div>\n<p><\/p>\n<p>The fakeroot runs a command called make-kpkg in an environment wherein it appears to have root privileges for file manipulation. This is useful for allowing users to create archives (tar, ar, .deb etc.) with files in them with root permissions\/ownership. The make-kpkg command build Debian\/Ubuntu kernel packages from Linux kernel sources and options are:<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li><kbd>--initrd<\/kbd> : Create an initrd image.<\/li>\n<li><kbd>--revision=1.0.NAS<\/kbd> : Set custom revision for your kernel such as 1.0.NAS or -1.0-custom-kernel etc.<\/li>\n<li><kbd>kernel_image<\/kbd> : This target produces a Debian package of the Linux kernel source image, and any modules configured in the kernel configuration file .config.<\/li>\n<li><kbd>kernel_headers<\/kbd> : This target produces a Debian package of the Linux kernel header image.<\/li>\n<\/ul>\n<p>Please note that kernel compilation may take quite a while, depending on the power of your machine. On my shared 4 CORE CPU and 4GB ram it took 60 mins to build the Linux kernel. In the end you should see something as follows on screen:<\/p>\n<pre>\n$ ls  ..\/*.deb\n..\/linux-headers-4.2.5_1.0.NAS_amd64.deb  ..\/linux-image-4.2.5_1.0.NAS_amd64.deb<\/pre>\n<h2>Installing a custom kernel<\/h2>\n<p>Type the following dpkg command to install a custom kernel on your system:<br \/><code>$ sudo dpkg -i linux-headers-4.2.5_1.0.NAS_amd64.deb<br \/>$ sudo dpkg -i linux-image-4.2.5_1.0.NAS_amd64.deb <\/code><br \/>Sample outputs:<\/p>\n<pre class=\"ini\">\nSelecting previously unselected package linux-image<span>-4.2<\/span><span>.5<\/span>.\n<span>(<\/span>Reading database ... <span>154262<\/span> files and directories currently installed.<span>)<\/span>\nPreparing to unpack linux-image<span>-4.2<\/span>.5_1<span>.0<\/span>.NAS_amd64.deb ...\nExamining \/etc\/kernel\/preinst.d\/\nDone.\nUnpacking linux-image<span>-4.2<\/span><span>.5<\/span> <span>(<\/span><span>1.0<\/span>.NAS<span>)<\/span> ...\nSetting up linux-image<span>-4.2<\/span><span>.5<\/span> <span>(<\/span><span>1.0<\/span>.NAS<span>)<\/span> ...\n&nbsp;\n Hmm. There is a symbolic link \/lib\/modules\/<span>4.2<\/span><span>.5<\/span>\/build\n However, I can not read it: No such file or directory\n Therefore, I am deleting \/lib\/modules\/<span>4.2<\/span><span>.5<\/span>\/build\n&nbsp;\n&nbsp;\n Hmm. The package shipped with a symbolic link \/lib\/modules\/<span>4.2<\/span><span>.0<\/span>\/source\n However, I can not read the target: No such file or directory\n Therefore, I am deleting \/lib\/modules\/<span>4.2<\/span><span>.5<\/span>\/source\n&nbsp;\nRunning depmod.\nExamining \/etc\/kernel\/postinst.d.\nrun-parts: executing \/etc\/kernel\/postinst.d\/apt-auto-removal <span>4.2<\/span><span>.5<\/span> \/boot\/vmlinuz<span>-4.2<\/span><span>.5<\/span>\nrun-parts: executing \/etc\/kernel\/postinst.d\/dkms <span>4.2<\/span><span>.5<\/span> \/boot\/vmlinuz<span>-4.2<\/span><span>.5<\/span>\nrun-parts: executing \/etc\/kernel\/postinst.d\/initramfs-tools <span>4.2<\/span><span>.5<\/span> \/boot\/vmlinuz<span>-4.2<\/span><span>.5<\/span>\nupdate-initramfs: Generating \/boot\/initrd.img<span>-4.2<\/span><span>.5<\/span>\nrun-parts: executing \/etc\/kernel\/postinst.d\/zz-update-grub <span>4.2<\/span><span>.5<\/span> \/boot\/vmlinuz<span>-4.2<\/span><span>.5<\/span>\nGenerating grub configuration file ...\nFound linux image: \/boot\/vmlinuz<span>-4.2<\/span><span>.5<\/span>-nixcraft-nas<span>.1<\/span><span>.0<\/span>\nFound initrd image: \/boot\/initrd.img<span>-4.2<\/span><span>.5<\/span>-nixcraft-nas<span>.1<\/span><span>.0<\/span>\nFound linux image: \/boot\/vmlinuz<span>-4.2<\/span><span>.5<\/span>\nFound initrd image: \/boot\/initrd.img<span>-4.2<\/span><span>.5<\/span>\nFound linux image: \/boot\/vmlinuz<span>-4.1<\/span><span>.6<\/span>\nFound initrd image: \/boot\/initrd.img<span>-4.1<\/span><span>.6<\/span>\nFound linux image: \/boot\/vmlinuz<span>-3.16<\/span><span>.0<\/span><span>-4<\/span>-amd64\nFound initrd image: \/boot\/initrd.img<span>-3.16<\/span><span>.0<\/span><span>-4<\/span>-amd64\nFound linux image: \/boot\/vmlinuz<span>-2.6<\/span><span>.32<\/span><span>-5<\/span>-amd64\nFound initrd image: \/boot\/initrd.img<span>-2.6<\/span><span>.32<\/span><span>-5<\/span>-amd64\ndone<\/pre>\n<h2>Reboot the box\/server\/laptop<\/h2>\n<p>Type the following command:<br \/><code>$ reboot<\/code><br \/>OR<br \/><code>$ shutdown -r now<\/code><\/p>\n<h2>Verify that everything is working<\/h2>\n<p>Type the following command to verify your new kernel and everything is working fine:<br \/><code>$ uname -a<br \/>$ uname -r<br \/>$ uname -mrs<br \/>$ dmesg | more<br \/>$ dmesg | egrep -i --color &#039;error|critical|failed&#039; <\/code><br \/>Sample outputs:<\/p>\n<pre>\nLinux nas02.nixcraft.net.in 4.2.5 #1 SMP Fri Sep 11 18:31:20 UTC 2015 x86_64 GNU\/Linux<\/pre>\n<p>And, there you have it, the Linux kernel version 4.2.5 installed and working correctly.<\/p>\n<p><strong>Tips<\/strong>:<\/p>\n<p><em>apt-cache search&nbsp; linux-image<\/em><\/p>\n<p><em>dpkg &#8211;list | grep linux-image<\/em><\/p>\n<p><em>aptitude purge linux-headers-4.1.15 linux-headers-4.3.3<\/em><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/www.cyberciti.biz\/faq\/debian-ubuntu-building-installing-a-custom-linux-kernel\/\" target=\"_blank\" rel=\"noopener noreferrer\">Credits<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to create a custom kernel configuration file and build a custom kernel, the full Linux kernel source tree must first be downloaded and installed. The latest Linux kernel stable version is 4.2. In this tutorial, you will learn &hellip; <a href=\"https:\/\/blog.ffteixeira.net\/?p=143\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-143","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts\/143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=143"}],"version-history":[{"count":0,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts\/143\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}