** Abstract: **Linux operating system, whether writing client program or server program, in high concurrent TCP connection processing, the maximum number of concurrent is limited by the system to the user’s single process can open file number at the same time.

[Appeal scene]

In Linux, the maximum number of concurrent TCP connections is limited by the number of files that can be opened by a single process (because the system creates a socket handle for each TCP connection). Each socket handle is also a file handle.

This document mainly changes the concurrent limit of the number of files.

[Configuration method]

1. Modify the limit of the number of files that can be opened by the user process, as shown in the figure:

Description:

Ulimit -n: Displays the maximum number of files that can be opened by the current user process.

The default value is 1024: each process of the current user can open a maximum of 1024 files at the same time.

The 1024 files have to be removed from each process’s open STANDARD input, standard output, standard error, server listening sockets, and so on, leaving only the number of files available for client socket connections

1024-10=1014 or so, that is, linux-based programs can allow up to 1014 CONCURRENT TCP connections at the same time.

(1) Modify the Linux system to open soft limit and hard limit for users, as shown in the figure:

vim /etc/security/limits.conf

Add root soft nofile 1921

root hard nofile 1921

Note: root indicates that the limit on the number of files that can be opened by the root user is changed. You can use asterisks (*) to represent all users. 1921 Specifies the new limit value to change, that is, the maximum number of open files (soft limit must be less than or equal to hard limit)

(2) Modify the /etc/pam.d/login file, as shown in the following figure:

Added: session required /lib/security/pam_limits.so

Linux should call pam_limits.so after a user logs in to the system to set the maximum number of resources that the user can use (including the maximum number of files that the user can open).

Pam_limits. So the module from the/etc/security/limits. These limit the conf file to read this

Run the following command to check the maximum number of open files in Linux: cat /proc/sys/fs/file-max

Note: This indicates that the Linux operating system can open a maximum of 96,854 files at a time (including the total number of files opened by all users). This is the hard limit for the Linux operating system. The number of files that can be opened by all users cannot exceed this value.

Echo /proc/sys/fs/file-max, restart the system, run ulimit -n, as shown in the following figure:

(5) If the value displayed after the ulimit -n command is executed is still smaller than the maximum value after the restart, it may be that the ulimit -n command used in the /etc/profile login script limits the number of files that can be opened simultaneously. Ulimit -n specifies the maximum number of files that can be opened at the same time. You can delete or change the value and restart the system.

(6) The above operation can solve the system limit of the number of open files processed by high concurrent TCP connections.

Sometimes, even though the limit on the number of files opened by the system is lifted, new TCP connections cannot be created when the number of concurrent TCP connections increases to a certain number.

2. The Linux network kernel has restrictions on the range of local port numbers. For example, the current system kernel limits the range of local port numbers to 1024-32768. Can’t assign requested address. This is because the default range of local port numbers set during kernel compilation is too small.

Modification: / etc/sysctl. Conf

Add: net.ipv4.ip_local_port_range = 1024 65535 (the maximum value must be smaller than or equal to 65535)

Run the sysctl -p command. If no error message is displayed, the setting is successful. The displayed content contains the content in the figure:

Above is a single process can open a maximum of 63000 TCP client connections at the same time.

Click to follow, the first time to learn about Huawei cloud fresh technology ~