Last active
January 31, 2025 12:13
-
-
Save Manouchehri/6dc5104495197caefa654317c8ed4e58 to your computer and use it in GitHub Desktop.
Get a random ephemeral port number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shuf -i 49152-60999 -n1 | |
# This obeys: | |
# RFC 6335 49152-65535 - https://www.rfc-editor.org/rfc/rfc6335.html | |
# Linux 32768-60999 - https://github.com/torvalds/linux/blob/1f5abbd77e2c1787e74b7c2caffac97def78ba52/net/ipv4/af_inet.c#L1833-L1834 | |
# Windows 49152-65535 - https://learn.microsoft.com/en-US/troubleshoot/windows-server/networking/default-dynamic-port-range-tcpip-chang | |
# Why does this matter? Naively, this gives us the best chance of not having our port changed when going through NAT. | |
# In reality, it probably doesn't help. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sysctl -w net.ipv4.ip_local_port_range="49152 60999" | |
# Hopefully avoid NAT port changing more |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment