Tune your Linux kernel with sysctl - gHacks Tech News

Tune your Linux kernel with sysctl

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.

Usage

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.

Final thoughts

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

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.

If you like our content, and would like to help, please consider making a contribution:


Previous Post: «
Next Post: »

Comments

  1. Dotan Cohen said on May 13, 2010 at 8:22 pm
    Reply

    Nice, thanks! Anything like this for Ubuntu?

    1. Miguel Arvelo said on June 1, 2010 at 4:32 pm
      Reply

      Last time I checked Ubuntu was a Linux distro…

  2. Evan Carroll said on May 13, 2010 at 11:16 pm
    Reply

    /sbin/sysctl is in the “procps” package which is required for ubuntu-minimal (it will be in any distro based on Ubuntu). It is also common in most distros using newer kernels.

  3. Dotan Cohen said on May 14, 2010 at 2:45 am
    Reply

    Thanks, Evan. It seems that /sbin/ is not in the regular user’s path, and searching for the sysctl package in aptitude found nothing. Thanks for mentioning the path!

  4. Dan Dart said on May 14, 2010 at 4:58 pm
    Reply

    You forgot to mention: /etc/sysctl.conf is the default location for the configuration file so you don’t have to use sysctl -a to make yourself one, just edit that one.

  5. SamD said on May 15, 2010 at 10:25 pm
    Reply

    You have a typo in the example that shows how to check the value of kernel.ctl-alt-del. You’ve got ‘sysctrl’ vice ‘sysctl’.

    Good info to have.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.