Skip to content

Instantly share code, notes, and snippets.

@Manouchehri
Created January 12, 2023 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Manouchehri/6dc5104495197caefa654317c8ed4e58 to your computer and use it in GitHub Desktop.
Save Manouchehri/6dc5104495197caefa654317c8ed4e58 to your computer and use it in GitHub Desktop.
Get a random ephemeral port number
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment