In this tutorial, you will learn about Linux kernel optimization
The Linux kernel as I have discussed in one of the articles on this site is the core engine of the Linux system. The kernel can be tuned for optimization of your environment instead of recompiling or rebuilding the whole kernel to suit your environment.
Changing the Linux kernel module parameter has also been discussed in one of the articles on this site.
In this tutorial, we are going to look at how to tune the Linux kernel using the /proc directory and sysctl.
Tuning the kernel for optimization can be done in the /proc directory. The /proc directory is a directory that contains the real-time information of the kernel. Let’s see what we have in this directory.
[root@HQEBPRD ~ ]# cd /proc
[root@HQEBPRD proc]# ls 1 106 10739 11221 126 2203 412 493 535 816 asound misc 10 1060 10740 11261 127 2207 413 494 536 9 buddyinfo modules 100 10604 10743 11274 128 2230 42 495 537 928 bus mounts 1000 10607 10748 11278 129 2233 424 496 538 929 cgroups mpt 1001 10612 10749 113 13 2234 427 497 539 93 cmdline mtrr 1003 10616 10750 11309 130 2241 432 498 540 930 consoles net 1004 10617 10755 11311 1316 2242 443 499 541 931 cpuinfo pagetypeinfo 1005 10619 10757 11316 1370 2243 449 501 543 932 crypto partitions 1006 1062 10758 1138 1371 2244 450 502 5622 933 devices pressure 1007 10620 10784 114 1436 2245 451 505 5655 934 diskstats sched_debug 101 10624 10795 1149 1489 2249 453 506 5665 935 dma schedstat 1012 10637 108 115 15 2250 456 507 5674 94 driver scsi 102 10641 10806 1157 1538 2251 459 508 5675 95 execdomains self 103 10645 10810 1158 16 2257 465 511 5679 957 fb 10457 107 10913 118 20 2306 482 523 671 98 kcore timer_list 10463 10702 10916 119 2095 2348 483 524 672 99 keys tty 10469 10704 10950 12 21 24 484 525 673 991 key-users uptime
If you look at the files, the “cpuinfo” file, the “uptime” file and the “version” file, you will see the current information of the kernel.
For example, if you cat the file, cpuinfo, you will see the current system CPU information.
[root@HQEBPRD proc]# cat cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 69 model name : Intel(R) Core(TM) i5-4300U CPU @ 1.90GHz stepping : 1 microcode : 0x24 cpu MHz : 2501.000 cache size : 3072 KB physical id : 0 siblings : 1 core id : 0 cpu cores : 1 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc cpuid pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm cpuid_fault invpcid_single pti ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid xsaveopt arat arch_capabilities bugs : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs bogomips : 5002.00 clflush size : 64 cache_alignment : 64 address sizes : 43 bits physical, 48 bits virtual power management:
Let’s look at the uptime file as well, it will display the uptime information of the system.
[root@HQEBPRD proc]# cat uptime 48326.25 47696.63
If you look at the version file, you will also see the current kernel version.
[root@HQEBPRD proc]# cat version Linux version 4.18.0-147.el8.x86_64 (email@example.com) (gcc version 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)) #1 SMP Thu Sep 26 15:52:44 UTC 2019
The /proc directory also contains some subdirectories. The subdirectories we are concerned about in this subject matter is the “/proc/sys” directory, the directory that allows you to fine-tune the Linux kernel as you desire for optimization.
Changing the Linux kernel parameters in this directory requires that you know what you are doing because a wrong change can cause a problem you did not plan for on the system.
Let’s have a look at the “/proc/sys” directory.
[root@HQEBPRD sys]# ls abi crypto debug dev fs kernel net sunrpc user vm [root@HQEBPRD sys]#
you can see all the sub-directories in this directory. The kernel directory contains all the core functionalities of the kernel.
The net directory contains all the core functionalities related to network, the vm directory contains the virtual memory functionalities as well.
In the kernel directory, you will find some of the Linux kernel parameters being used by the system. examples are the sysrq, hostname, hung_task_panic, numa_balancing, panic, shmall, shmmax, shmmni, watchdog, osrelease, e.t.c.
Also, in the net directory, you will find some of the Linux network kernel parameters being used by the system. A very common parameter in the net directory is the ip_forward file. i.e “/proc/sys/net/ipv4/ip_forward.
[root@HQEBPRD ipv4]# cat /proc/sys/net/ipv4/ip_forward 1
you can see that the value of ip_forward is set to 1 which means that the system can forward an IP, i.e, act as a router. This value can be changed to 0 if you don’t want the system to be a router.
To change the value, use the echo command, (using a text editor may not work in this subdirectory)
let’s change the kernel parameter value for ip_forward
[root@HQEBPRD ipv4]# echo 0 > /proc/sys/net/ipv4/ip_forward
[root@HQEBPRD ipv4]# cat /proc/sys/net/ipv4/ip_forward 0 [root@HQEBPRD ipv4]#
you can see that the value has been changed. However, using the echo command to tune the kernel parameter will not make the changes persistent.
To make the changes persistent, you will have to use the “sysctl” tool.
The sysctl is a tool that is used to view and set the Linux kernel parameters on the fly in the /sys/proc directory. Just say that sysctl is sort of a directive of the /sys/proc directory.
you can use the sysctl to make the changes in the /sys/proc/ directory persistent.
so you can finetune your kernel without rebooting or recompiling/rebuilding your kernel with the sysctl tool.
To view the kernel variables or parameters that are currently set, use the command,
you can use the grep tool with sysctl as well to be to look for a specific parameter.
For example, to look for the ip_forward parameter we talked about earlier, you use the command,
[root@HQEBPRD ~]# sysctl -a |grep ip_forward net.ipv4.ip_forward = 0 net.ipv4.ip_forward_update_priority = 1 net.ipv4.ip_forward_use_pmtu = 0 [root@HQEBPRD ~]#
Also, did you notice something?
you will notice that the “/” sign has been replaced with “.”
That means if you use the sysctl -a command to view the kernel settings, “/” will be replaced with “.”
For example, In the in the /proc/sys/ directory, /net/ipv4/ip_forward corresponds with “.net.ipv4.ip.forward”.
Prior to RHEL 7 systems and above, the sysctl has just one configuration file, which is the /etc/sysctl.conf, so when the system boots, the sysctl process looks in the /etc/sysctl.conf file and begins to run with the parameters in the file.
However, the process of setting the parameters on the new generation Linux system has changed.
You will notice that in new RHEL systems, this file has become empty by default.
Let’s take a look at the file (/etc/sysctl.conf) to ascertain this.
[root@HQEBPRD ipv4]# cat /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). [root@HQEBPRD ipv4]#
you can see that no parameter settings are displayed here, you can also see the directory’s where the sysctl settings are now defined in the screenshot above.
The new parameters are now defined in the “/usr/lib/sysctl.d, “/run/sysctl.d”, and “/etc/sysctl.d”.
vendors settings are defined in “/usr/lib/sysctl.d”
If you check out the directory, you will see the files.
[root@HQEBPRD ~]# ls -l /usr/lib/sysctl.d/ total 20 -rw-r--r--. 1 root root 1810 Jul 5 2019 10-default-yama-scope.conf -rw-r--r--. 1 root root 524 Aug 30 2019 50-coredump.conf -rw-r--r--. 1 root root 1190 Aug 30 2019 50-default.conf -rw-r--r--. 1 root root 246 Aug 12 2018 50-libkcapi-optmem_max.conf -rw-r--r--. 1 root root 499 Sep 16 09:56 60-libvirtd.conf
you can also see the way the numbers are arranged at the beginning of the file. The orders should be respected. 10- will be read first, followed by 50- and 60-
So, if any parameter is set twice, the last one will have a higher priority, that is, it will override the former one with the lower number.
But again, note that the parameters, still, can be defined in this same directory that has been deprecated. that is, /etc/sysctl.conf, but to follow the standard file-system hierarchy, it is best to define the parameters in the “/etc/sysctl.d” directory.
Moving forward, from our previous example where we set the ip_forward value to be zero using the echo command, let’s now change the ip_forward parameter again from 0 to 1 by using sysctl.
As we said, you can put the parameter in “/etc/sysctl”, it will still work, but in our case, we will follow the standard file system hierarchy, so I will put the parameter in the /etc/sysctl.d directory.
So, let’s do this.
The first thing you will need to do is to create a file in the directory. For example, ipforward.conf and define the value in there.
[root@HQEBPRD ~]# vi /etc/sysctl.d/20-ipforward.conf
net.ipv4.ip_forward = 1
On rebooting the system, the system will be able to route packets again.
To view the new settings, as we mentioned, you can use the command,
# sysctl -a |grep ip_forward
There are some other sysctl options that allow you to write to the sysctl configuration file.
One of them is,
it is used as an alternative to the echo command. For example, to change the ip_forward parameter, you use the command,
[root@HQEBPRD ~]# sysctl -w net.ipv4.ip_forward=1 net.ipv4.ip_forward = 1
To change the kernel.sysrq parameter, you use the command,
[root@HQEBPRD ~]# sysctl -w kernel.sysrq=1 kernel.sysrq = 1
To change the swapiness value of the system, you use the command,
[root@HQEBPRD ~]# sysctl -w vm.swappiness=10 vm.swappiness = 10
To load or apply the settings, you use the command,
[root@HQEBPRD ~]# sysctl -p [root@HQEBPRD ~]#
However, please note that using the “sysctl -w” option is not recommended because you need to be sure that the parameters that will be changed will not affect the system after you make it permanent and the only way to be sure of this is to use the echo command, after which you can now enter the parameters in the file permanently.
Your feedback is welcomed. If you love others, you will share with others