How to Use /proc/sys and sysctl to modify and set kernel runtime parameters in RHEL7

When you want to improve the performance or the characteristics of your server, you need to set the kernel runtime parameters.

9

When you want to improve the performance or the characteristics of your server, you need to set the kernel runtime parameters.

In order to do this, you’ve got three ways:

  • through the /proc filesystem,
  • with the sysctl command,
  • through the /etc/sysctl.conf file.

The /proc Filesystem

To get the value of a kernel runtime parameter (here /proc/sys/net/ipv4/ip_forward used for allowing a host to act as an router), type:

# cat /proc/sys/net/ipv4/ip_forward

To set the value of the same parameter, type:

# echo 1 > /proc/sys/net/ipv4/ip_forward

Note: 1 is used for On and 0 for off.

This change is instantaneously active but doesn’t persist a reboot. You have to write it into the /etc/rc.d/rc.local file to get it re-applied at each boot. See below for a better solution.

The sysctl Command

With the sysctl command, you can get all the available kernel runtime parameters with their current value.

# sysctl -a | grep vm.swappiness
vm.swappiness = 30

But you can also set a kernel runtime parameter with the -w option.

# sysctl -w vm.swappiness=20
vm.swappiness = 20

Still like the previous method, this change is instantaneously active but doesn’t persist a reboot. You have to write it into the /etc/rc.d/rc.local file to get it re-applied at each boot. See below for a better solution.

The /etc/sysctl.conf File

To permanently store kernel runtime parameters, you need to write them into the /etc/sysctl.conf file.

For example, edit the /etc/sysctl.conf file and paste the following line:

# allow IPv4 forwarding
net.ipv4.ip_forward = 1

Caution: Comments are only allowed on a separate line and not at the end of a line!
Note: It is not a coincidence if the net.ipv4.ip_forward kernel runtime parameter name matches the /proc/sys/net/ipv4/ip_forward path name.

Note: There is also a directory called /etc/sysctl.d. You can create files with .conf extension inside that will be read at boot.

Then, you need to apply the change:

# sysctl -p

Caution: Only changes in the /etc/sysctl.conf file will be applied. If you created some files in the /etc/sysctl.d directory, you will need either to type sysctl -p /etc/sysctl.d/file.conf (if file.conf is the file where kernel runtime parameters are stored) or sysctl –system to get the associated changes applied.

Many kernel runtime parameters can be set this way. Here are only a few examples:

# don't respond to a ping
net.ipv4.icmp_echo_ignore_all = 1
# don't respond to a ping to the broadcast address
net.ipv4.icmp_echo_ignore_broadcasts = 1
# disable IPv6 for all network interfaces
net.ipv6.conf.all.disable_ipv6 = 1

Note: As seen before, the sysctl -a command gets all the kernel runtime parameters with their current value. By redirecting the output to a file, this is also a good way to back up your configuration before any change.

Default kernel runtime configuration is located in the /usr/lib/sysctl.d directory.

To know the order the files are read and apply the various settings, type: # sysctl –system.

Caution: Kernel runtime parameters set in the /etc/sysctl.conf file can be overrided by the application of a tuned profile.

Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More