大部分文章都说这个值决定了客户端的一个 ip 可用的端口数量,即一个 ip 最多只能创建 60K 多一点的连接(1025-65535),如果要突破这个限制需要客户端机器绑定多个 ip。
还有部分文章说的是这个值决定的是 socket 四元组中的本地端口数量,即一个 ip 对同一个目标 ip+port 最多可以创建 60K 多一点连接,只要目标 ip 或端口不一样就可以使用相同的本地端口,不一定需要多个客户端 ip 就可以突破端口数量限制。
文档中的介绍也很模糊:
1 2 3 4 5 6 7
ip_local_port_range - 2INTEGERS Defines the local port range that is used by TCP and UDP to choose the local port. The first number is the first, the second the last local port number. If possible, it is better these numbers have different parity. (one even and one odd values) Thedefault values are 32768 and 60999 respectively.
下面就来做一些实验来确认这个选项的实际效果。
实验环境:
1 2
$ uname -a Linux vagrant 4.15.0-29-generic #31-UbuntuSMPTueJul1715:39:52UTC2018 x86_64 x86_64 x86_64 GNU/Linux
相同目标 ip 和相同目标端口下的端口数量限制
先设置 ip_local_port_range 的值为非常小的范围:
1 2 3 4 5
$ echo "61000 61001" | sudo tee /proc/sys/net/ipv4/ip_local_port_range 6100061001
[What is the theoretical maximum number of open TCP connections that a modern Linux box can have - Stack Overflow](What is the theoretical maximum number of open TCP connections that a modern Linux box can have - Stack Overflow)
[**Coping with the TCP TIME-WAIT state on busy Linux servers | Vincent Bernat**](Coping with the TCP TIME-WAIT state on busy Linux servers | Vincent Bernat)
[‘Re: Fix for rare EADDRNOTAVAIL error’ - MARC](‘Re: Fix for rare EADDRNOTAVAIL error’ - MARC)