DEV Community

Teddy Zugana
Teddy Zugana

Posted on

Linux System & Kernel Tuning on C Socket Linux

Linux System & Kernel Tuning

When running high-concurrency stress tests, default Linux kernel limits will quickly block connections with errors like Too many open files or Cannot assign requested address.
Increase File Descriptor Limits (ulimit)

Each socket requires a file descriptor.
Bash

Check current limit

ulimit -n

Increase limit for the current session (e.g., to 1,048,576)

ulimit -n 1048576

To make this permanent, edit /etc/security/limits.conf:
Plaintext

`* soft nofile 1048576

  • hard nofile 1048576 ` Optimize Kernel Network Parameters (sysctl.conf)

Add or update the following parameters in /etc/sysctl.conf and apply them with sysctl -p:
Ini, TOML


# Increase max system open file handles
fs.file-max = 2097152

# Expand ephemeral port range to allow more outgoing connections
net.ipv4.ip_local_port_range = 1024 65535

# Allow reuse of TIME_WAIT sockets for new connections
net.ipv4.tcp_tw_reuse = 1

# Max backlog of incoming connections
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535

# Reduce FIN timeout to free closed connections faster
net.ipv4.tcp_fin_timeout = 15

# Increase max OS receive/send buffer sizes
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
Enter fullscreen mode Exit fullscreen mode

Top comments (0)