IP_LOCAL_PORT_RANGE(2const)
β¨ IP_LOCAL_PORT_RANGE β¨
okay bestie, gather round, because today we're talking about IP_LOCAL_PORT_RANGE β she's a <netinet/in.h> / sys/socket.h girlie from the IP* constants friend group, she lives in IPPROTO_IP(2const) (that's the section 2 const page, iykyk), and she's literally just the default local port range π
. Linux 6.3 era, so she's young but she knows exactly what she wants.
π LIBRARY
Standard C library (libc, -lc). The classic. The blueprint. π
πΈ SYNOPSIS
#include <netinet/in.h> /* Definition of IP* constants */
#include <sys/socket.h>
int setsockopt(int sockfd, IPPROTO_IP, IP_LOCAL_PORT_RANGE,
const uint32_t *val, sizeof(uint32_t));
int getsockopt(int sockfd, IPPROTO_IP, IP_LOCAL_PORT_RANGE,
uint32_t *val, sizeof(uint32_t));
π DESCRIPTION
So what she does is set or get the per-socket default local port range. That's it, that's the job, and she does it flawlessly. π
Here's the tea: this option lets you clamp down the global local port range β the one defined by the /proc/sys/net/ipv4/ip_local_port_range interface β but just for one given socket. Not everyone, just her socket. Main character energy only, no sharing. π¦
The option takes a uint32_t value, and the bits are split like this:
- the high 16 bits = the upper range bound
- the low 16 bits = the lower range bound
Range bounds are inclusive, and those 16-bit values should be in host byte order (do NOT go handing her network byte order, she will not be doing the endianness dance for you π).
Also, important: the lower bound has to be less than the upper bound when both bounds are not zero. If you try it any other way, setting the option fails with EINVAL. No exceptions, no negotiation, periodt. π
And if either bound is outside of the global local port range, or is zero? Then that bound has no effect. Zero basically means "no opinion" here. Chill of her tbh.
Wanna reset the setting? Pass zero as both the upper and the lower bound. Clean slate, fresh start, we love a factory reset moment β¨
π ERRORS
Look, she's not gonna re-explain everyone else's lore:
- See
IPPROTO_IP(2const). - See
setsockopt(2). - See
ip(7).
π HISTORY
Linux 6.3. Babygirl is a 6.3 baby, iconic debut, no notes. π
π SEE ALSO
and that's the whole page bestie, you ate it up π«Ά this masterpiece was brought to you by Linux man-pages 6.20, dated 2025-11-25 β go forth and clamp those port ranges responsibly, mwah πβ¨
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms