linux設置預留端口號,防止監聽端口被占用 ip_local_reserved_ports


1. 背景

linux服務器啟動時,會對指定的端口進行監聽bind,如果同一個機器上這個端口已經被使用,則監聽失敗,程序無法啟動。

linux客戶端連接服務器accept時,系統會分配本地臨時端口用於網絡連接。

目前是”大混部”的時代,一台物理機可能會部署多個服務,如果端口號設置不好,很可能會出現端口被其他程序占用的情況。

2. 解決方法

針對我的linux機器看看

本地臨時端口配置(2個數: start, end)

cat /proc/sys/net/ipv4/ip_local_port_range
32768	61000

 預留端口配置(可支持逗號分隔的多個數字,比如10000, 10005-10010)

cat /proc/sys/net/ipv4/ip_local_reserved_ports

 如下面參考鏈接說的,有2種方法:

(1) 修改臨時端口范圍 ip_local_port_range,因為一個程序的端口問題修改一個機器的臨時端口范圍,明顯減少了臨時端口的使用量。代價大。

(2) 修改預留端口ip_local_reserved_ports,即使沒有發生沖突也可以預先設置,防止后續端口被占用。

貼一下參考鏈接里面的英文

ip_local_reserved_ports解釋如下:
ip_local_reserved_ports - list of comma separated ranges
Specify the ports which are reserved for known third-party
applications. These ports will not be used by automatic port
assignments (e.g. when calling connect() or bind() with port
number 0). Explicit port allocation behavior is unchanged.

The format used for both input and output is a comma separated
list of ranges (e.g. "1,2-4,10-10" for ports 1, 2, 3, 4 and
10). Writing to the file will clear all previously reserved
ports and update the current list with the one given in the
input.

參考:

(1) http://www.ttlsa.com/linux/reserved-port-to-avoid-occupying-ip_local_reserved_ports/

(2) https://blog.csdn.net/bdss58/article/details/78546551?locationNum=10&fps=1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM