• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to query and modify kernel parameters using sysctl (Immediately and persistently) in CentOS / RHEL

by admin

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.:

# sysctl kernel.shmmax
kernel.shmmax = 68719476736
# sysctl -n kernel.shmmax
68719476736

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

Filed Under: CentOS/RHEL 6, CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. “yum update” fails with “[package version 1] is a duplicate with [package version 2]”
  2. renice: command not found
  3. How to use auditd to monitor a file deletion in Linux
  4. userdel Command Examples in Linux
  5. nping: command not found
  6. gnome-terminal: command not found
  7. nft Command Examples in Linux
  8. host Command Examples in Linux
  9. CentOS / RHEL 7 : How to rename the volume group for root and swap
  10. User Unable To Edit crontab, Error: “/tmp/crontab.Lm34gsJV: Permission denied”

You May Also Like

Primary Sidebar

Recent Posts

  • qm Command Examples in Linux
  • qm wait Command Examples in Linux
  • qm start Command Examples in Linux
  • qm snapshot Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright