[系统资源]port range

ip_local_port_range 端口范围 sysctl

Linux中有限定端口的使用范围,如果我要为我的程序预留某些端口,那么我需要控制这个端口范围,
本文主要描述如何去修改端口范围。

/proc/sys/net/ipv4/ip_local_port_range的原文解释:

The /proc/sys/net/ipv4/ip_local_port_range defines the local port range that is used by TCP and UDP traffic to choose the local port. You will see in the parameters of this file two numbers: The first number is the first local port allowed for TCP and UDP traffic on the server, the second is the last local port number. For high-usage systems you may change its default parameters to 32768-61000 -first-last.

/proc/sys/net/ipv4/ip_local_port_range定义了本地tcp/udp的端口范围。可以理解为系统中的程序会选择这个范围内的端口来连接到目的端口(目的端口当然是用户指定的)。

如何修改port range

查看

[root@jiangyi02.sqa.zmf /home/ahao.mah]
#sysctl net.ipv4.ip_local_port_range
net.ipv4.ip_local_port_range = 1024	65535

修改

# echo 1024 65535 > /proc/sys/net/ipv4/ip_local_port_range

或者, 如果想修改这个范围,可以使用sysctl工具,sysctl的配置文件位于/etc/sysctl.conf 。

$ sudo sysctl -w net.ipv4.ip_local_port_range="1024 64000"

最后编辑文件: /etc/sysctl.conf

# increase system IP port limits
net.ipv4.ip_local_port_range = 1024 65535

使生效

sysctl -p /etc/sysctl.conf 

看一下man中的描述

[root@jiangyi01.sqa.zmf /home/ahao.mah/ulimit]
#man sysctl 
原文地址:https://www.cnblogs.com/muahao/p/6511447.html