Modify Linux kernel parameters

Set Linux kernel parameters /etc/sysctl.conf

How does Linux modify kernel parameters (/proc/sys and /etc/sysctl.conf) when the system is running  

 

RedHat provides a very nice way to change kernel parameters while the system is running without rebooting the system. This is achieved through the /proc virtual filesystem.

 

Most of the kernel parameters are stored in the / proc/sys directory, and are designed to be changed while the system is running , but they will fail after restarting the machine. You can change the file /etc/ corresponding to the kernel parameters in /proc/sys sysctl.conf kernel parameters to permanently change.

 

Below we take the example of enabling the ip forwarding function of the kernel to illustrate two methods for modifying kernel parameters when the system is running. IP forwarding refers to allowing the system to pass the network . RedHat blocks this function by default . This function needs to be turned on when the local machine needs to be used as a router, NAT, etc.

Method 1: Modify the content of the kernel parameter file under /proc

Directly modify the kernel parameter ip_forward to correspond to the file /proc/sys/net/ipv4/ip_forward under /proc. Use the following command to view the contents of the ip_forward file:
# cat /proc/sys/net/ipv4/ip_forward
The default value of this file is 0 to prohibit ip forwarding, and modifying it to 1 enables ip forwarding. The modification command is as follows:
# echo 1 >/proc/sys/net/ipv4/ip_forward
The modification will take effect immediately, that is, the kernel has enabled the ip forwarding function. However, if the system is restarted, it will return to the default value of 0. If you want to open it permanently, you need to modify the content of the /etc/sysctl.conf file to achieve it.

Method 2: Modify the /etc/sysctl.conf file

There is a variable in the default sysctl.conf file that is
net.ipv4.ip_forward = 0
Change the latter value to 1 and save the file. Because the initialization script /etc/rc.d/rc.sysinit will read the contents of the /etc/sysctl.conf file every time the system starts, the ip forwarding function will be enabled every time the system starts after modification. But just modifying the sysctl file will not take effect immediately. If you want the modification to take effect immediately, you can execute the following command:
# sysctl –p
When modifying other kernel parameters, you can add corresponding variables to the /etc/sysctl.conf file.

 

The following describes the correspondence between the kernel files under /proc/sys and the variables in the configuration file sysctl.conf

 

Since the modifiable kernel parameters are in the /proc/sys directory, the variable names in sysctl.conf omit the front part of the directory (/proc/sys).

Converting files in /proc/sys to variables in sysctl follows two simple rules:
  1. Remove the previous part of /proc/sys
  2. Change slashes in filenames to dots
These two rules can convert any file name in /proc/sys into a variable name in sysctl.
E.g:
/proc/sys/net/ipv4/ip_forward => net.ipv4.ip_forward
/proc/sys/kernel/hostname => kernel.hostname

can use the following command to query all modifiable variable names
# sysctl –a
Here are some simple kernel parameters:
  1. /proc/sys/kernel/shmmax: This file specifies the maximum shared memory segment size allowed by the kernel.
  2. /proc/sys/kernel/threads-max: This file specifies the maximum number of threads that the kernel can use.
  3. /proc/sys/kernel/hostname: This file allows you to configure the network hostname.
  4. /proc/sys/kernel/domainname: This file allows you to configure the network domain name

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326873325&siteId=291194637