Ephemeral port

On this page 7

An ephemeral port is a temporary port number that the operating system assigns automatically to the client side of a connection, typically when an application opens an outbound TCP or UDP socket without choosing a port itself. Among transport protocols, it completes the four-part identifier of a connection: source address, source port, destination address and destination port. The port exists only for the lifetime of that connection or socket and returns to the pool afterward.

How the operating system assigns an ephemeral port

An ephemeral port is allocated when a client socket connects or sends without an explicit bind. The kernel picks a free number from a configured ephemeral port range, records it in the socket, and uses it as the source port in every outgoing segment or datagram. Replies from the server arrive addressed to that port, which lets the kernel deliver them to the correct application.

Modern stacks choose the number using randomized port selection, as recommended in RFC 6056, rather than simply taking the next sequential value. Randomization makes it harder for an off-path attacker to guess the port and inject forged packets or poison a DNS cache. For TCP, the kernel only needs the full four-tuple to be unique, so the same ephemeral port can be reused toward different destinations at the same time.

The table below summarizes the life cycle of an ephemeral port for a typical TCP client.

StageWhat happensPort state
Socket creationApplication calls connect without bindNo port assigned yet
AllocationKernel selects a free number from the rangePort reserved for this socket
Data transferPort appears as source in outgoing segmentsIn use, connection established
Active closeClient sends the first FINEnters TIME_WAIT
ReleaseTIME_WAIT expires, usually 60 to 240 secondsReturned to the pool
Life cycle of a TCP ephemeral port

Ephemeral port ranges by operating system

The size of the ephemeral port range determines how many simultaneous outbound connections a host can open to a single destination address and port. The Internet Assigned Numbers Authority designates 49152 to 65535 as the dynamic or private range, but operating systems are free to use different defaults.

PlatformDefault rangeNumber of portsWhere it is configured
IANA recommendation49152 to 6553516,384Not applicable
Windows (Vista and later)49152 to 6553516,384netsh int ipv4 dynamicport
Linux32768 to 6099928,232net.ipv4.ip_local_port_range
FreeBSD and macOS49152 to 6553516,384net.inet.ip.portrange sysctls
Default ephemeral port ranges

Administrators often widen the range to 1024 through 65535 on busy proxies and load balancers, but they must then reserve ports used by local services so that the kernel does not hand a listening port to an outbound connection. On Linux, the ip_local_reserved_ports setting serves this purpose.

Ephemeral ports compared with well-known and registered ports

Port numbers form three ranges with different purposes: well-known ports identify standard services, registered ports identify specific applications, and ephemeral ports identify the client end of a conversation. The difference is about who chooses the number and for how long it is held.

RangeNumbersChosen byTypical lifetime
Well-known ports0 to 1023IANA, used by serversAs long as the service runs
Registered ports1024 to 49151IANA registration, used by serversAs long as the service runs
Ephemeral portsDynamic range of the hostOperating system kernelOne connection or socket
Port number ranges compared

The ranges overlap in practice: on Linux the default ephemeral range starts at 32768, inside the registered range. A server that listens on a registered port such as 33060 can therefore fail to start if an outbound client connection already holds that number.

Ephemeral ports in firewalls, NAT and load balancers

Ephemeral ports matter most for stateful firewalls and network address translation, because return traffic arrives on an unpredictable port. A stateful firewall tracks each outbound connection and permits replies to the ephemeral port automatically, so administrators do not need to open the whole range. Stateless filters, such as many cloud network access control lists, cannot track state and must explicitly allow inbound traffic to the ephemeral range.

With port address translation, a NAT device rewrites the source port of each internal connection to an ephemeral port on its own public address. A single public IPv4 address therefore supports at most about 64,000 concurrent flows per destination address and port, which explains why carrier-grade NAT and busy proxies run out of ports before they run out of bandwidth.

The decision table lists common situations involving ephemeral ports and the usual response.

SituationRecommended actionReason
Stateless ACL blocks repliesAllow inbound to the ephemeral rangeReplies target unpredictable client ports
Proxy opens many backend connectionsWiden the range and reuse connectionsAvoids exhausting the available ports
Service fails to bind its portReserve the port from the dynamic rangePrevents the kernel assigning it to clients
NAT gateway drops new flowsAdd public addresses to the poolEach address adds a full set of ports
Practical handling of ephemeral ports

Port exhaustion and other common problems

The best-known failure is ephemeral port exhaustion, which occurs when a host opens connections to one destination faster than closed ports leave the TIME_WAIT state. With 28,000 ports and a 60-second TIME_WAIT, a client can sustain roughly 470 new connections per second to the same destination before connect calls begin failing with errors such as EADDRNOTAVAIL.

  • Short-lived connections without pooling: opening a new TCP connection for every request is the most common root cause of exhaustion.
  • Lowering TIME_WAIT carelessly: TIME_WAIT protects against delayed segments from an old connection being accepted by a new one, so removing it risks data corruption.
  • Assuming ports are fixed: logging or firewall rules that expect a constant client port break, because the port changes on each connection.
  • Ignoring UDP: UDP sockets such as DNS resolvers also consume ephemeral ports, and predictable ports there weaken spoofing protection.

The durable fix is almost always architectural: connection pooling, HTTP keep-alive and spreading traffic across several destination addresses or source addresses, rather than tuning kernel timers.

A non-technical view

Think of a large office building with a public front desk number that everyone calls. When an employee phones out, the switchboard gives that call a temporary extension so the answer comes back to the right desk. When the call ends, the extension is freed for someone else.

An ephemeral port works the same way for computers. The server keeps its well-known number, while your device borrows a short-lived number for each conversation. Problems only appear when a device makes so many calls so quickly that it runs out of temporary extensions.

A concrete example

An online retailer runs a reverse proxy on Linux that forwards customer requests to a single database-backed API service. During a seasonal sale, traffic rises to about 900 requests per second, and the proxy opens a new TCP connection for every request to the API at one address and port.

Within minutes the proxy logs connection errors, and monitoring shows close to 28,000 sockets in TIME_WAIT, which is the entire default Linux range of 32768 to 60999. The engineers confirm ephemeral port exhaustion rather than an API outage, because the API itself reports normal load.

As an immediate measure they widen the ephemeral range to 15000 through 64999 and reserve the ports of local services. The lasting fix is enabling keep-alive connection pooling to the backend, which reduces new connections to a few dozen per second and ends the errors entirely.