How Can I View and Modify Kernel Parameters of a Linux ECS?

Modify the kernel parameters only if the parameter settings affect your services. Kernel parameters vary depending on OS versions. If the parameter settings must be modified,

  • Ensure that the target parameter settings meet service requirements.

  • Modify the correct kernel parameters. For details about common kernel parameters, see Table 1.

  • Back up key ECS data before modifying kernel parameter settings.

Background

Table 1 Common Linux kernel parameters

Parameter

Description

net.core.rmem_default

Specifies the default size (in bytes) of the window for receiving TCP data.

net.core.rmem_max

Specifies the maximum size (in bytes) of the window for receiving TCP data.

net.core.wmem_default

Specifies the default size (in bytes) of the window for transmitting TCP data.

net.core.wmem_max

Specifies the maximum size (in bytes) of the window for transmitting TCP data.

net.core.netdev_max_backlog

Specifies the maximum number of packets that can be sent to a queue when the rate at which each network port receives packets is faster than the rate at which the kernel processes these packets.

net.core.somaxconn

Defines the maximum length of the listening queue for each port in the system. This parameter applies globally.

net.core.optmem_max

Specifies the maximum size of the buffer allowed by each socket.

net.ipv4.tcp_mem

Uses the TCP stack to show memory usage in memory pages (4 KB generally).

The first value is the lower limit of memory usage.

The second value is the upper limit of the load added to the buffer when the memory is overloaded.

The third value is the upper limit of memory usage. When this value is reached, packets can be discarded to reduce memory usage. For a large BDP, increase the parameter value as needed. The unit of this parameter is memory page but not byte.

net.ipv4.tcp_rmem

Specifies the memory used by sockets for automatic optimization.

The first value is the minimum number of bytes allocated to the socket buffer for receiving data.

The second value is the default value, which is overwritten by rmem_default. The buffer size can increase to this value when the system load is not heavy.

The third value is the maximum number of bytes allocated to the socket buffer for receiving data. This value is overwritten by rmem_max.

net.ipv4.tcp_wmem

Specifies the memory used by sockets for automatic optimization.

The first value is the minimum number of bytes allocated to the socket buffer for transmitting data.

The second value is the default value, which is overwritten by wmem_default. The buffer size can increase to this value when the system load is not heavy.

The third value is the maximum number of bytes allocated to the socket buffer for transmitting data. This value is overwritten by wmem_max.

net.ipv4.tcp_keepalive_time

Specifies the interval at which keepalive detection messages are sent in seconds for checking TCP connections.

net.ipv4.tcp_keepalive_intvl

Specifies the interval at which keepalive detection messages are resent in seconds when no response is received.

net.ipv4.tcp_keepalive_probes

Specifies the maximum number of keepalive detection messages that are sent to determine a TCP connection failure.

net.ipv4.tcp_sack

Enables selective acknowledgment (value 1 indicates enabled). This configuration allows the transmitter to resend only lost packets, thereby improving system performance. However, this configuration will increase the CPU usage. You are suggested to enable selective acknowledgment for WAN communication.

net.ipv4.tcp_fack

Enables forwarding acknowledgment for selective acknowledgment (SACK), thereby reducing congestion. You are suggested to enable forwarding acknowledgment.

net.ipv4.tcp_timestamps

Specifies a TCP timestamp, which will add 12 bytes in the TCP packet header. This configuration calculates RTT using RFC1323, a more precise retransmission method upon timeout than retransmission. You are suggested to enable this parameter for higher system performance.

net.ipv4.tcp_window_scaling

Enables RFC1323-based window scaling by setting the parameter value to 1 if the TCP window is larger than 64 KB. The maximum TCP window is 1 GB. This parameter takes effect only when window scaling is enabled on both ends of the TCP connection.

net.ipv4.tcp_syncookies

Specifies whether to enable TCP synchronization (syncookie). This configuration prevents socket overloading when a large number of connections are attempted to set up. CONFIG_SYN_COOKIES must be enabled in the kernel for compilation. The default value is 0, indicating that TCP synchronization is disabled.

net.ipv4.tcp_tw_reuse

Specifies whether a TIME-WAIT socket (TIME-WAIT port) can be used for new TCP connections.

Note

This parameter is valid only for clients and takes effect only when net.ipv4.tcp_timestamps is enabled.

This parameter cannot be set to 1 if NAT is enabled. Otherwise, an error will occur in remote ECS logins.

net.ipv4.tcp_tw_recycle

Allows fast recycle of TIME-WAIT sockets.

Note

This parameter is valid only when net.ipv4.tcp_timestamps is enabled.

Do not set this parameter to 1 if NAT is enabled. Otherwise, an error will occur during remote ECS logins.

net.ipv4.tcp_fin_timeout

Specifies the time (in seconds) during which a socket TCP connection that is disconnected from the local end remains in the FIN-WAIT-2 state. Process suspension may be caused by the disconnection from the peer end, continuous connection from the peer end, or other reasons.

net.ipv4.ip_local_port_range

Specifies local port numbers allowed by TCP/UDP.

net.ipv4.tcp_max_syn_backlog

Specifies the maximum number of connection requests that are not acknowledged by the peer end and that can be stored in the queue. The default value is 1024. If the server is frequently overloaded, try to increase the value.

net.ipv4.tcp_low_latency

This option should be disabled if the TCP/IP stack is used for high throughput, low latency.

net.ipv4.tcp_westwood

Enables the congestion control algorithm on the transmitter end to evaluate throughput and improve the overall bandwidth utilization. You are suggested to enable the congestion control algorithm for WAN communication.

net.ipv4.tcp_bic

Enables binary increase congestion for fast long-distance networks so that the connections with operations being performed at a rate of Gbit/s can be functional. You are suggested to enable binary increase congestion for WAN communication.

net.ipv4.tcp_max_tw_buckets

Specifies the number of TIME_WAIT buckets, which defaults to 180000. If the number of buckets exceeds the default value, extra ones will be cleared.

net.ipv4.tcp_synack_retries

Specifies the number of times that SYN+ACK packets are retransmitted in SYN_RECV state.

net.ipv4.tcp_abort_on_overflow

When this parameter is set to 1, if the system receives a large number of requests within a short period of time but fails to process them, the system will send reset packets to terminate the connections. It is recommended that you improve system processing capabilities by optimizing the application efficiency instead of performing reset operations.

Default value: 0

net.ipv4.route.max_size

Specifies the maximum number of routes allowed by the kernel.

net.ipv4.ip_forward

Forward packets between interfaces.

net.ipv4.ip_default_ttl

Specifies the maximum number of hops that a packet can pass through.

net.netfilter.nf_conntrack_tcp_timeout_established

Clears iptables connections that are inactive for a specific period of time.

net.netfilter.nf_conntrack_max

Specifies the maximum value of hash entries.

Viewing Kernel Parameters

  • Method 1: Run the cat command in /proc/sys to view file content.

    /proc/sys/ is a pseudo directory generated after the Linux kernel is started. The net folder in this directory stores all kernel parameters that have taken effect in the system. The directory tree structure is determined based on complete parameter names. For example, net.ipv4.tcp_tw_recycle corresponds to the /proc/sys/net/ipv4/tcp_tw_recycle file, and the content of the file is the parameter value.

    Example:

    To view the net.ipv4.tcp_tw_recycle value, run the following command:

    cat /proc/sys/net/ipv4/tcp_tw_recycle

  • Method 2: Use the /etc/sysctl.conf file.

    Run the following command to view all parameters that have taken effect in the system:

    /usr/sbin/sysctl -a

    net.ipv4.tcp_syncookies = 1
    net.ipv4.tcp_max_tw_buckets = 4096
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.tcp_keepalive_time = 1800
    net.ipv4.tcp_fin_timeout = 30
    ......
    net.ipv4.tcp_keepalive_time = 1200
    net.ipv4.ip_local_port_range = 1024 65000
    net.ipv4.tcp_max_syn_backlog = 8192
    net.ipv4.tcp_rmem = 16384 174760 349520
    net.ipv4.tcp_wmem = 16384 131072 262144
    net.ipv4.tcp_mem = 262144 524288 1048576
    ......
    

Modifying Kernel Parameter Settings

  • Method 1: Run the echo command in /proc/sys to modify the file for the target kernel parameters.

    The parameter values changed using this method take effect only during the current running and will be reset after the system is restarted. To make the modification take effect permanently, see method 2.

    /proc/sys/ is a pseudo directory generated after the Linux kernel is started. The net folder in this directory stores all kernel parameters that have taken effect in the system. The directory tree structure is determined based on complete parameter names. For example, net.ipv4.tcp_tw_recycle corresponds to the /proc/sys/net/ipv4/tcp_tw_recycle file, and the content of the file is the parameter value.

    Example:

    To change the net.ipv4.tcp_tw_recycle value to 0, run the following command:

    echo "0" > /proc/sys/net/ipv4/tcp_tw_recycle

  • Method 2: Use the /etc/sysctl.conf file.

    The parameter values changed using this method take effect permanently.

    1. Run the following command to change the value of a specified parameter:

      /sbin/sysctl -w kernel.domainname="example.com"

      Example:

      sysctl -w net.ipv4.tcp_tw_recycle="0"

    2. Run the following command to change the parameter value in the /etc/sysctl.conf file:

      vi /etc/sysctl.conf

    3. Run the following command for the configuration to take effect:

      /sbin/sysctl -p