Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

connect: Cannot assign requested address on Linux: ephemeral ports and local addresses

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

An outgoing connection that fails with connect: Cannot assign requested address has hit EADDRNOTAVAIL. The connect(2) manual gives one cause for internet sockets: the socket had no address yet and, "upon attempting to bind it to an ephemeral port, it was determined that all port numbers in the ephemeral port range are currently in use". The bind(2) manual gives another: the requested source address "was not local". Count the sockets, read the range, and list the addresses on the host to tell them apart.

What the error means

"Cannot assign requested address." is the GNU C Library's text for EADDRNOTAVAIL. The same name appears in the Linux error list as "Address not available", so logs from different programs can word it differently.

Two common situations produce it:

  1. No free ephemeral port. A client that does not choose its own local port gets one from a range the kernel manages. The kernel documentation says ip_local_port_range "Defines the local port range that is used by TCP and UDP to choose the local port." The default is 32768 to 60999. That is 60999 - 32768 + 1 = 28,232 ports. When every port in the range is in use, connect() fails with this error.
  2. A source address that is not on the host. If a program binds an outgoing socket to an address, the bind(2) manual says EADDRNOTAVAIL means "A nonexistent interface was requested or the requested address was not local." This does not depend on how many sockets are open.

Counting sockets shows whether this is the cause. ss can list TCP sockets by state, including TIME-WAIT, and the count set against the size of the range tells you how close the host is to the ceiling. The kernel documentation also describes a setting for TIME-WAIT sockets, tcp_tw_reuse, discussed under "Risks and limits".

Likely causes

Cause What you see How to confirm
Many short outgoing connections use up the range Errors come in bursts at high request rates; the count of TCP sockets approaches 28,232 on a default range ss counts compared with sysctl net.ipv4.ip_local_port_range
Closed connections to one server pile up in TIME-WAIT Most TIME-WAIT sockets point at one remote address ss with state time-wait and dst
The range was narrowed earlier A range far smaller than the default The sysctl output shows a small range
Ports reserved for applications shrink the usable range A long list in ip_local_reserved_ports The sysctl output; the documentation says reserved ports "will not be used by automatic port assignments"
The program binds to an address the host does not have Every connection fails, whatever the load The address is missing from ip address show

Prove the cause before changing anything

All of these only read data. Use root for ss -p.

1. Read the range and the reserved ports. The sysctl manual says a variable name alone reads the key:

sysctl net.ipv4.ip_local_port_range net.ipv4.ip_local_reserved_ports

2. Count the TCP sockets that hold ports. The ss manual says -H suppresses the header line, -a shows both listening and non-listening sockets, -t selects TCP, -n stops name lookups, and state time-wait filters by state:

ss -Htan state time-wait | wc -l
ss -Htan state established | wc -l

ss -s prints summary statistics, which the manual describes as "Print summary statistics".

3. Count by remote address. The ss manual lists dst HOST as a predicate that tests the destination:

ss -Htan state time-wait dst 198.51.100.20 | wc -l

4. Name the program. Add -p to show processes:

ss -tanp dst 198.51.100.20

5. List the addresses on the host. The ip-address manual gives ip address show:

ip address show

If the source address in the application's configuration is not in this list, the cause is the address, not the ports.

6. Check the listeners before widening a range. The ss manual says -l shows only listening sockets:

ss -Hltn

Fix

Reduce short-lived connections

Fix the cause in the application first: reuse connections to the same server (connection pooling or keep-alive) instead of opening a new connection for each request. A wider range only moves the ceiling.

Widen the range

Choose a first port that is at least ip_unprivileged_port_start (default 1024, per the kernel documentation, which says the range must be "greater than or equal to" it) and a last port that still fits below 65536. The documentation adds "If possible, it is better these numbers have different parity (one even and one odd value)." The range 20000 to 60999 qualifies: 20000 is even, 60999 is odd, and it holds 60999 - 20000 + 1 = 41,000 ports.

If ss -Hltn shows a service listening on a port inside the new range, choose a range that excludes it. The kernel documentation also describes ip_local_reserved_ports, for "known third-party applications", whose ports are not used by automatic assignment. It says "Writing to the file will clear all previously reserved ports", so changing it is a separate change that starts from the saved list.

First save the current values:

sysctl net.ipv4.ip_local_port_range net.ipv4.ip_local_reserved_ports > /root/sysctl-ports.2026-11-22

Risk: the next command changes the local ports the kernel hands to every outgoing connection on this host at once. A service that starts later on a port inside the new range can find the port in use by an outgoing connection unless that port is reserved.

Set the range for the running kernel. The sysctl manual says -w forces write arguments, and that a value with characters the shell parses may need double quotes:

sysctl -w net.ipv4.ip_local_port_range="20000 60999"

To undo it: set the saved value back. If /root/sysctl-ports.2026-11-22 shows 32768 60999, which the kernel documentation lists as the default, run:

sysctl -w net.ipv4.ip_local_port_range="32768 60999"

Use the two numbers in the saved file if they differ.

Make the range survive a reboot

The sysctl.d manual says files with the .conf extension in /etc/sysctl.d are read at boot, and recommends names that start with a two-digit number, 60 to 90 for files in /etc/.

This step is only for a file name that does not exist yet. Check first, and stop if a file is shown:

ls -l /etc/sysctl.d/90-local-ports.conf

First save the current directory listing and the running range:

ls -l /etc/sysctl.d > /root/sysctl.d-list.2026-11-22
sysctl net.ipv4.ip_local_port_range > /root/port-range-persist-before.2026-11-22

Risk: the next step makes the new range apply at every boot, and any later full reload of the sysctl configuration applies it again.

Create /etc/sysctl.d/90-local-ports.conf with one line, in the key = value form of the sysctl.d examples:

net.ipv4.ip_local_port_range = 20000 60999

Load only that file. The sysctl manual says -p loads "sysctl settings from the file specified":

sysctl -p /etc/sysctl.d/90-local-ports.conf

To undo it: if the saved listing does not contain 90-local-ports.conf, the file is new. Check the exact file first, then remove it with its own command:

ls -l /etc/sysctl.d/90-local-ports.conf
rm /etc/sysctl.d/90-local-ports.conf

Then set the running value back to the two numbers in /root/port-range-persist-before.2026-11-22, with the sysctl -w form shown under "Widen the range".

The program binds to an address the host does not have

The fix is in the program's configuration or on the host: use an address that appears in ip address show, or restore the missing address. The kernel documentation describes ip_nonlocal_bind as allowing processes "to bind() to non-local IP addresses, which can be quite useful - but may break some applications". No command for it is given here, because it hides a wrong address rather than fixing it.

Example (illustrative): web01 calls an API at 198.51.100.20:443. Its log shows connect: Cannot assign requested address in bursts. sysctl net.ipv4.ip_local_port_range prints 32768 60999, so the range holds 60999 - 32768 + 1 = 28,232 ports. ss -Htan state time-wait | wc -l prints 28150, and the same command with dst 198.51.100.20 also prints 28150, so all of them point at the API. That leaves 28,232 - 28,150 = 82 ports. The ss -Hltn output shows listeners on 8443 and 9100 only, both outside 20000 to 60999. The engineer saves the values and sets the range to 20000 60999, which holds 41,000 ports. The application owner then changes the client library to reuse connections.

Confirm the fix

  1. sysctl net.ipv4.ip_local_port_range prints the new range.
  2. The count from ss -Htan state time-wait | wc -l stays well below the size of the range during a busy period.
  3. The error no longer appears in the application log under the same load.
  4. After a reboot, the range is still the one in the file.

What to watch afterwards

  • Record the TIME-WAIT count at peak load, and compare it with the size of the range.
  • Watch for the count to climb after a release of the application; it can point to connection reuse that stopped working.
  • Keep listening ports out of the ephemeral range, or reserve them.

Risks and limits

  • These commands have not been run or lab-tested.
  • The kernel documentation says of tcp_tw_reuse: "It should not be changed without advice/request of technical experts." Its default on the documented kernel is 2 (loopback traffic only). No command for it is given here.
  • The kernel documentation says of tcp_max_tw_buckets: "you must not lower the limit artificially". Do not lower it to hide the problem.
  • The documentation ties tcp_fin_timeout to an orphaned connection in FIN_WAIT_2 and does not mention TIME-WAIT.
  • A very wide range can overlap ports used by services on the same host. Check ss -Hltn and ip_local_reserved_ports each time.
  • The kernel documentation says of ip_unprivileged_port_start: "This is a per-namespace sysctl." The page does not say this for ip_local_port_range, so check the range inside the namespace of the program that fails.
  • Defaults on kernels older than the documented one can differ.

Sources

  • IP Sysctl — The Linux Kernel documentation, latest (page header 7.4.0-rc3), no date shown, accessed 22 November 2026
  • connect(2) — Linux man-pages 6.19, 2026-02-08, accessed 22 November 2026
  • bind(2) — Linux man-pages 6.19, 2026-02-08, accessed 22 November 2026
  • Error Codes — GNU C Library manual, no date shown, accessed 22 November 2026
  • errno(3) — Linux man-pages 6.19, 2026-05-28, accessed 22 November 2026
  • ss(8) — iproute2 manual page, no manual date shown, accessed 22 November 2026
  • ip-address(8) — iproute2 manual page, dated 20 Dec 2011, accessed 22 November 2026
  • sysctl(8) — procps-ng 2024-07-19, accessed 22 November 2026
  • sysctl.d(5) — systemd 262 development manual page, no manual date shown, accessed 22 November 2026