Not many of today's Linux users remember the days when you could (and sometimes had to) compile your own kernel. If you had a special device or special needs for the kernel there was no other way to get it to work. Today most kernels work out of the box with most hardware. But there are times when you could squeeze out a bit more performance or even lighten your kernel on the fly. You can do that with the sysctl command.
This command allows you to turn on or off numerous kernel parameters on the fly or you can edit a specific file to tune those same parameters. Either way you can modify all of the parameters listed in /proc/sys, so if you would like to see just how much you can modify, take a look at each of the subdirectories under the /proc/sys directory. For example, you can change the number of the maximum file descriptors on your machine by tuning the kern.maxfiles parameter.
NOTE: You will definitely want to exercise caution when toying with this command as it could, upon modifying any given parameter in the wrong way, cause data loss or worse.
The basic usage of the command is sysctl OPTIONS PARAMETER VARIABLE. The variable for a parameter will be in the form of either boolean, string, or numbers. You must run this command as either the root user or with the help of sudo. One of the first things you might want to do is to issue the command sudo sysctl -a. This command will display all of the tunable variables on your machine. The list will look something like this:
kernel.sched_min_granularity_ns = 2000000
kernel.sched_latency_ns = 10000000
kernel.sched_wakeup_granularity_ns = 2000000
kernel.sched_shares_ratelimit = 500000
kernel.sched_shares_thresh = 4
kernel.sched_features = 15834235
kernel.sched_migration_cost = 500000
kernel.sched_nr_migrate = 32
kernel.sched_time_avg = 1000
kernel.timer_migration = 1
kernel.sched_rt_period_us = 1000000
kernel.sched_rt_runtime_us = 950000
kernel.sched_compat_yield = 0
kernel.panic = 0
kernel.core_uses_pid = 0
kernel.core_pattern = core
kernel.core_pipe_limit = 0
kernel.tainted = 0
kernel.latencytop = 0
kernel.real-root-dev = 0
kernel.print-fatal-signals = 0
In total, there are over 500 tunable parameters on an average Linux system. That's a lot to tune. If you want to examine that list more closely issue the command sudo sysctl -a > tunable and then open that file up with your favorite editor. Let's do this with something instantly useful. Say you want to re-enable <Ctrl><Alt><Del> that some distributions have disabled. First let's make sure your distribution has disabled this. Do this with the command:
sudo sysctrl kernel.ctrl-alt-del
What you should see output is:
kernel.ctrl-alt-del = 0
The above output does in fact inform you that this function has been disabled. Let's enable it. To do this you would issue the command:
sudo sysctl -w kernel.ctrl-alt-del=1
You should see the output:
kernel.ctrl-al-del = 1
Which tells you the command was successful. You can, of course, reset this to the original value with the command:
sudo sysctl -w kernel.ctrl-alt-del=0
Of course this is only a simple example of what you can tune with the sysctl command. Seeing as how there are over 500 tunable parameters, you could tune your kernel to meet some very specific needs.
As with any tool that modifies a running kernel, make sure you exercise caution when using sysctl. You might want to practice this command on a non-production machine before you attempt to make any changes on a production machine. And start off simple. Modify some parameters that won't render your machine unbootable, your hard disc not writable, your network not reachable, etc.Advertisement
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.