The purpose of this post is to explain how to configure kernel parameters on Red Hat (RHEL/CentOS) and Oracle Linux (OL) systems using the sysctl utility. The sysctl utility (/sbin/sysctl) allows (privileged) users to query and modify kernel parameters during runtime. The utility is common to most Linux distributions, however, subtle differences may exist between distributions e.g. RHEL/OL and SuSE. Parameters that can be viewed/modified are those exposed via procfs filesystem /proc/sys. The dot(“.”) notation is used when setting in a configuration file.

 

Querying a Specific Kernel Parameter

 

To query a named kernel parameter value, run the sysctl utility with either the ‘-n‘ or no arguments at all e.g.:

 

 

In the example above, parameter kernel.shmmax relates to /proc/sys/kernel/shmmax e.g.:

# cat /proc/sys/kernel/shmmax
68719476736

 

Querying all the kernel parameters

 

To query all kernel parameter values, run the systctl utility with the ‘-a‘ argument e.g.:

# sysctl -a | more
kernel.sched_child_runs_first = 0
kernel.sched_min_granularity_ns = 4000000
kernel.sched_latency_ns = 20000000
kernel.sched_wakeup_granularity_ns = 4000000
kernel.sched_tunable_scaling = 1

 

You can grep for a specific kernel parameter in the above output. Use Regular expressions for filtering out a group of kernel parameters. For Example,

# sysctl -a | grep ^kernel.s[h,e]m
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
kernel.shmmni = 4096
kernel.shm_rmid_forced = 0
kernel.sem = 250	32000	100	128

 

Setting Kernel Parameters immediately (not persistently)

 

To set a sysctl parameter value immediately (not persistent) use the sysctl -w command. For Example :

# sysctl -w kernel.sysrq=0
kernel.sysrq = 0

 

Parameter which take multiple values should have the values enclosed in quotes. For example, to set net.ipv4.ip_local_port_range to 1025-65535:

# sysctl -w net.ipv4.ip_local_port_range="1025 65535"

 

Alternatively, it is possible to echo values directly into the procfs file which represents a sysctl parameter. For example:

# echo 1 > /proc/sys/net/ipv4/ip_forward
# echo "1025 65535" > /proc/sys/net/ipv4/ip_local_port_range

 

Setting Kernel Parameters Persistently

 

Kernel parameter values changed using the ‘systctl -w’ method are volatile i.e. lost on server reboot. The sysctl utility’s configuration file, /etc/sysctl.conf, should be used to permanently store non-default kernel parameter values. The file is parsed on server boot and values within are used to configure the kernel. The syntax required to configure kernel parameters using the /etc/sysctl.conf file follows the component.parameter=value notation e.g. kernel.shmmax = 33554432.

Syntax

# vi /etc/sysctl.conf
[component].[parameter]=[value]

 

Following is a sample /etc/sysctl.conf file:

# grep -v ^# /etc/sysctl.conf
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
....

 

Along with using the ‘systctl -w’ method to modify kernel parameters, one may also modify parameters by adding them to the /etc/sysctl.conf file, then parsing the file using the sysctl utility with the ‘-p‘ argument e.g.:

# sysctl -n kernel.ctrl-alt-del
0

 

# echo "kernel.ctrl-alt-del=1" >> /etc/sysctl.conf

 

# sysctl -p
kernel.ctrl-alt-del = 1

 

Modifying kernel parameters by adding them to the /etc/sysctl.conf file not only sets them (sysctl -p), but also ensures the modified values persist after a server reboot.

 

Setting Kernel Parameters Persistently Under CentOS / RHEL 7

 

The approach to set kernel parameter under CentOS/RHEL 7 is a bit different than the older version. Create a new conf file under the /etc/sysctl.d/ directory. File names take the format /etc/sysctl.d/[name].conf. Files in the /etc/sysctl.d/ directory are parsed in order so it is recommended to prepend the file name with a number signifying the order you would like the files to be parsed in. For example, /etc/sysctl.d/01-custom.conf:

# cat /etc/sysctl.d/01-custom.conf
net.ipv4.ip_forward=1
net.ipv4.ip_local_port_range="1025 65535"

 

To have the system immediately apply the values in a new/updated /etc/sysctl.d file, run sysctl -p [filename]:

# sysctl -p /etc/sysctl.d/01-custom.conf

 

Hjalp dette svar dig? 0 Kunder som kunne bruge dette svar (0 Stem)