Set up sysctl variables on CentOS/RHEL

A sysctl variable represents file locations under the /proc/sys directory. The dot(“.”) notation is used when setting in a configuration file.

RHEL/CentOS 7
– Set a sysctl parameter value immediately (not persistent/not applied on boot) on any version of RHEL:
Use the sysctl -w command. Parameter which take multiple values should have the values enclosed in quotes. For example, to set net.ipv4.ip_forward to 1 and net.ipv4.ip_local_port_range to 1025-65535:

# sysctl -w net.ipv4.ip_forward=1
# 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:

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

– Set a persistent value for a sysctl parameter in RHEL 7 which will be applied at boot:
Create a new conf file under the /etc/sysctl.d/ directory. File names take the format /etc/sysctl.d/.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 :

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

RHEL/CentOS 6
To set a persistent value for a sysctl parameter in RHEL 6 which will be applied at boot:

Insert or update the parameter values as defined in the/etc/sysctl.conf file:

# cat /etc/sysctl.conf 
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.
......
net.ipv4.ip_forward=1
net.ipv4.ip_local_port_range="1025 65535"

To have the system immediately apply the values in the updated /etc/sysctl.conf file, run sysctl -p:

# sysctl -p

Example setup persistent value on RHEL/CentOS 6:

[root@centos6 ~]# cat /proc/sys/kernel/pid_max
65536
[root@centos6 ~]# echo kernel.pid_max = 131068 > /etc/sysctl.conf
[root@centos6 ~]# sysctl -p /etc/sysctl.conf
kernel.pid_max = 131068
[root@centos6 ~]# cat /proc/sys/kernel/pid_max
131068
[root@centos6 ~]#

Example setup persistent value on RHEL/CentOS 6:

[root@horse ~]# echo kernel.pid_max = 131068 > /etc/sysctl.conf
[root@horse ~]# sysctl -p /etc/sysctl.conf
kernel.pid_max = 131068
[root@horse ~]# cat /proc/sys/kernel/pid_max
131068
[root@horse ~]#

Leave a Reply

Your email address will not be published. Required fields are marked *