Can you explain sysctl and where do you use it?
The Linux kernel is flexible, and you can even modify the way it works on the fly by dynamically changing some of its parameters, thanks to the sysctl command. Sysctl provides an interface that allows you to examine and change several hundred kernel parameters in Linux or BSD. Changes take effect immediately, and there’s even a way to make them persist after a reboot. By using sysctl judiciously, you can optimize your box without having to recompile your kernel, and get the results immediately.
To start getting a taste of what sysctl can modify, run
sysctl -a and you will see all the possible parameters. The list can be quite long: in my current box there are 712 possible settings.
$ sysctl -a
kernel.panic = 0
kernel.core_uses_pid = 0
kernel.core_pattern = core
kernel.tainted = 129
…many lines snipped…
If you want to get the value of just a single variable, use something like
sysctl vm.swappiness, or just
sysctl vm to list all variables that start with “vm.” Add the
-n option to output just the variable values, without the names;
-N has the opposite effect, and produces the names but not the values.
You can change any variable by using the
-w option with the syntax
sysctl -w variable=value. For example,
sysctl -w net.ipv6.conf.all.forwarding=1 sets the corresponding variable to true (0 equals “no” or “false”; 1 means “yes” or “true”) thus allowing IP6 forwarding. You may not even need the
-w option — it seems to be deprecated. Do some experimenting on your own to confirm that.
For more information, run
man sysctl to display the standard documentation.
sysctl values are loaded at boot time from the /etc/sysctl.conf file. This file can have blank lines, comments (lines starting either with a “#” character or a semicolon), and lines in the “variable=value” format. For example, my own sysctl.conf file is listed below. If you want to apply it at any time, you can do so with the command
# Disable response to broadcasts.
net.ipv4.icmp_echo_ignore_broadcasts = 1
# enable route verification on all interfaces
net.ipv4.conf.all.rp_filter = 1
# enable ipV6 forwarding
net.ipv6.conf.all.forwarding = 1
# increase the number of possible inotify(7) watches
fs.inotify.max_user_watches = 65536
With so many tunable parameters, how do you decide what to do? Alas, this is a sore point with sysctl: most of the relevant documentation is hidden in the many source files of the Linux kernel, and isn’t easily available, and it doesn’t help that the explanations given are sometime arcane and difficult to understand. You may find something in the /usr/src/linux/Documentation/sysctl directory, but most (if not all) files there refer to kernel 2.2, and seemingly haven’t been updated in the last several years.
Looking around for books on the subject probably won’t help much. I found hack #71 in O’Reilly’s Linux Server Hacks, Volume 2, from 2005, but that was about it. Several other books include references to sysctl, but as to specific parameters or hints, you are on your own.
As an experiment, I tried looking for information on the swappiness parameter, which can optimize virtual memory management. The /usr/src/Linux/Documentation/sysctl/vm.txt file didn’t even refer to it, probably because this parameter appeared around version 2.6 of the kernel. Doing a general search in the complete /usr/src/linux directory turned up five files that mention “swappiness”: three “include” (.h) files in include/linux, plus kernel/sysctl.c and mm/vmscan.c. The latter file included the information:
* From 0 .. 100. Higher means more swappy.
int vm_swappiness = 60;
That was it! You can see the default value (60) and a minimal reference to the field meaning. How helpful is that?
My suggestion would be to use
sysctl -a to learn the available parameters, then Google around for extra help. You may find, say, an example of changing the shared memory allocation to solve a video program problem, or an explanation on vm.swappiness, or even more suggestions for optimizing IP4 network traffic.
sysctl shows yet another aspect of the great flexibility of Linux systems. While documentation for it is not widely available, learning its features and capabilities on your own can help you get even more performance out of your box. That’s system administration at its highest (or lowest?) level.