How Many Files and Connections Can a Linux Server Actually Handle?
This article explains the Linux kernel limits that determine how many files a server can open and how many TCP connections it can sustain, covering the key parameters (fs.file-max, soft/hard nofile, fs.nr_open), practical configuration examples, memory constraints, client-side limits, and related performance considerations.
1. Maximum number of files a server can open
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): describes the total number of files the system can open; the root user is not limited by this parameter.
soft nofile (process‑level): limits the maximum number of files a single process can open; it is configured globally.
fs.nr_open (process‑level): also limits the maximum number of files per process and can be set per user.
These parameters are inter‑related. When configuring them, keep in mind:
If you increase soft nofile, you must also raise hard nofile; the effective limit is the lower of the two.
If you raise hard nofile, fs.nr_open must be larger; otherwise login may fail.
Modifying fs.nr_open via echo "xxx" > /proc/sys/fs/nr_open is not persistent and will be lost after a reboot, so avoid using echo for kernel parameters.
2. Example of increasing the maximum open files
To allow a process to open one million file descriptors, edit /etc/sysctl.conf:
fs.file-max=1100000 // system‑wide setting
fs.nr_open=1100000 // must be larger than hard nofileApply the changes 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 pair of socket kernel objects (the TCP four‑tuple). The theoretical maximum is 2^32 × 2^16 ≈ 2 × 10^14 connections, but real limits are imposed by CPU and memory.
For a 4 GB server, an ESTABLISHED idle connection consumes about 3.3 KB of memory, so roughly one million connections can be kept idle. In practice, data transfer and processing dramatically reduce this number.
The main overhead is not the connection itself but the data handling and business‑logic processing per connection.
4. Maximum connections a client machine can initiate
A client consumes one local port per connection. With a single IP, the theoretical limit is 65,535 ports (actually around 64,000 usable). If the client has n IP addresses, the limit becomes n × 65,535. If the server listens on m ports, the limit is 65,535 × m. The usable port range is also limited by net.ipv4.ip_local_port_range.
Thus a client can also reach over one million connections given multiple IPs or multiple server ports.
5. Other considerations
The listen queue length is controlled by net.core.somaxconn (default 128); increase it to avoid half‑open queue overflow.
After terminating a process, the port may remain occupied briefly until the OS recycles it.
Clients should not bind a specific port; let the kernel choose to avoid interfering with its port‑selection strategy.
6. Sample Java NIO client code
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..........");
}7. Related practical questions
"Too many open files" occurs when a process exceeds the file‑descriptor limits; adjust fs.file-max, soft nofile, and fs.nr_open accordingly.
Server capacity is primarily limited by memory; a 4 GB server can hold about one million idle connections.
Clients can break the 65 k limit by using multiple IPs or connecting to different server ports.
For a push service serving 100 million idle connections, a 128 GB server can handle roughly 5 million; about 20 such servers would be sufficient.
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.
