How Many Files and TCP Connections Can a Linux Server Actually Handle?
This article explains the Linux kernel limits that determine how many files and TCP connections a server can open, describes the key parameters (fs.file-max, soft nofile, fs.nr_open), shows configuration examples, and discusses how memory, ports, and other settings affect real‑world capacity for both servers and clients.
1. Maximum number of files a server can open
1. Limiting parameters
In Linux everything is a file; the maximum number of open files is affected by three parameters:
fs.file-max (system‑wide limit; root is not restricted by this parameter)
soft nofile (per‑process soft limit)
fs.nr_open (per‑process hard limit, can be set per user)
These parameters are interrelated; when configuring you must consider:
If you increase soft nofile, you must also raise hard nofile, otherwise the effective limit is the lower of the two.
If you raise hard nofile, fs.nr_open must be larger; setting hard nofile higher than fs.nr_open can prevent login.
Modifying fs.nr_open via an echo command is not persistent across reboots and is strongly discouraged.
2. Example of increasing the maximum file descriptors
To allow a process to open 1,000,000 descriptors, edit /etc/sysctl.conf:
fs.file-max=1100000
fs.nr_open=1100000Apply with sysctl -p, then edit /etc/security/limits.conf:
soft nofile 1000000
hard nofile 10000003. Maximum number of connections a server can support
A TCP connection is represented by a socket object; each ESTABLISHED idle connection consumes about 3.3 KB of memory. On a 4 GB server this yields roughly 1 million concurrent connections, assuming no data processing.
In practice, CPU and memory usage for data transfer reduce this number dramatically.
4. Maximum connections a client machine can initiate
With a single IP and a single server port, a client can open up to 65,535 connections (limited by source ports). With multiple client IPs the limit scales by the number of IPs; with multiple server ports it scales by the number of ports, etc.
The port range can be adjusted via net.ipv4.ip_local_port_range.
5. Other considerations
The listen backlog is controlled by net.core.somaxconn (default 128).
After terminating a process, the port may remain occupied briefly until the OS releases it.
Clients should avoid calling bind() so the kernel can choose ports automatically.
6. Common “too many open files” error
The error occurs when a process exceeds the file‑descriptor limits; fixing it requires adjusting fs.file-max, soft nofile, and fs.nr_open while respecting their coupling.
7. Capacity planning examples
A 4 GB server can handle about 1 million idle TCP connections.
A 128 GB server could support roughly 5 million idle connections, so about 20 servers would suffice for 100 million long‑lived connections.
8. Java NIO socket example
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..........");
}Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
