Operations 14 min read

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

This article explains the Linux kernel parameters that limit open files and TCP connections, shows how to increase those limits with sysctl and limits.conf, and estimates the maximum number of concurrent connections a server or client can support based on memory and port constraints.

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

1. Maximum Number of Open Files on a Server

1. Limiting Parameters

In Linux, everything is a file, and the maximum number of open files is limited by three parameters:

fs.file-max (system-wide limit): total number of files the system can open; root is exempt.

soft nofile (per‑process soft limit): maximum files a process may open; set once per system.

fs.nr_open (per‑process hard limit): also limits per‑process files and can be set per user.

These parameters are interrelated; when configuring them, consider:

If you increase soft nofile, you must also raise hard nofile, otherwise the effective limit is the lower of the two.

Raising hard nofile requires increasing fs.nr_open accordingly; setting hard nofile higher than fs.nr_open can prevent users from logging in.

Modifying fs.nr_open via echo "value" > /proc/sys/fs/nr_open is not persistent; it will revert after reboot, so avoid using echo for kernel parameters.

2. Example: Raising the Maximum Open Files

To allow a process to open 1,000,000 file descriptors, edit /etc/sysctl.conf:

fs.file-max=1100000  // system‑wide limit
fs.nr_open=1100000   // must be greater than hard nofile

Apply with sysctl -p, then edit /etc/security/limits.conf:

soft nofile 1000000
hard nofile 1000000

3. Maximum TCP Connections a Server Can Support

A TCP connection is represented by a pair of kernel socket objects (the TCP four‑tuple). The theoretical maximum is 2^32 × 2^16, but practical limits are set by CPU and memory.

For a 4 GB server, an ESTABLISHED idle connection consumes about 3.3 KB of memory, allowing roughly 1 million concurrent connections, assuming no data processing.

4. Maximum Connections a Client Machine Can Initiate

Each client connection consumes a source port (0‑65535). Depending on IP and server port configurations, the maximum connections are:

Single client IP, single server IP/port: up to 65,535 connections.

Client with n IPs, single server IP/port: n × 65,535.

Single client IP, server listening on m ports: 65,535 × m.

Port range can be expanded via net.ipv4.ip_local_port_range.

5. Other Relevant Settings

Connection queue length is controlled by net.core.somaxconn (default 128).

After terminating a process, ports may remain occupied briefly until the OS recycles them.

Clients should avoid calling bind() to let the kernel choose ports.

public static void main(String[] args) throws IOException {
    SocketChannel sc = SocketChannel.open();
    // client can also call bind()
    sc.bind(new InetSocketAddress("localhost", 9999));
    sc.connect(new InetSocketAddress("localhost", 8080));
    System.out.println("waiting..........");
}

Linux treats sockets as files; each opened socket consumes memory, and the kernel uses hash tables and red‑black trees to manage sockets and epoll objects efficiently.

For a push service handling 100 million idle connections, a 128 GB server can support about 5 million connections; thus roughly 20 servers are sufficient.

operationsLinuxSysctlfile-descriptorsserver limitsTCP connections
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.