Operations 15 min read

How Many Files and TCP Connections Can a Linux Server Actually Handle?

Linux treats everything as a file, so the maximum number of open files and TCP connections a server can support depends on kernel parameters like fs.file-max, soft/hard nofile, and fs.nr_open, as well as memory size, port limits, and proper configuration to avoid 'too many open files' errors.

ITPUB
ITPUB
ITPUB
How Many Files and TCP Connections Can a Linux Server Actually Handle?

1. Maximum Open Files on a Server

In Linux, everything is a file, and the total number of files a server can open is limited by three kernel parameters:

fs.file-max – system‑wide maximum number of file descriptors. The root user is not restricted by this value.

soft nofile – per‑process soft limit for open files; can be increased only once per system.

fs.nr_open – per‑process hard limit; can be set differently for each user.

These parameters are coupled, so changing one often requires adjusting the others.

2. Adjusting Server Limits Example

To allow a process to open one million file descriptors, modify the configuration files as follows:

fs.file-max=1100000  // system‑wide limit, leave some buffer</code><code>fs.nr_open=1100000   // must be larger than hard nofile

Apply the changes with sysctl -p. Then edit /etc/security/limits.conf:

soft nofile 1000000</code><code>hard nofile 1000000

Do not use echo to write kernel parameters, because the changes will be lost after a reboot.

3. Maximum TCP Connections on a Server

Theoretically a TCP connection is identified by a 4‑tuple (source IP, source port, destination IP, destination port), giving a maximum of 2³² × 2¹⁶ ≈ 2 × 10¹⁴ possible connections. In practice the limit is set by CPU and memory.

An ESTABLISH‑state idle connection consumes roughly 3.3 KB of memory. On a 4 GB server this translates to about 1 million concurrent connections, assuming no data traffic or processing overhead.

4. Client Connection Limits

Each client connection consumes a source port. With a single client IP and a single server port, the maximum is 65 535 connections (actual usable ports are slightly fewer due to reserved ranges). The limit can be increased by:

Assigning multiple IP addresses to the client (n × 65 535 connections).

Running multiple server programs listening on different ports (m × 65 535 connections).

The kernel parameter net.ipv4.ip_local_port_range controls the usable port range and may need adjustment.

5. Other Relevant Settings

net.core.somaxconn – length of the listen backlog queue (default 128).

When a process is terminated, the OS may temporarily hold the port, causing “port already in use” errors until it is reclaimed.

Clients should avoid calling bind() with a fixed port; letting the kernel choose a random port yields better reuse behavior.

Linux manages sockets with hash tables; epoll uses a red‑black tree for efficient insert/delete operations.

6. Common Issues and Solutions

The “too many open files” error occurs when a process exceeds the kernel’s file‑descriptor limits. Resolve it by increasing fs.file-max, soft nofile, and fs.nr_open as described above, and ensure the hard and soft limits are consistent.

For long‑living push services (e.g., 100 million users), a 128 GB server can handle roughly 5 million idle connections (≈3 KB each). About 20 such servers are sufficient to support the full load.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxSystem Tuningfile-descriptorsKernel ParametersTCP connections
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.