What is a random port generator?
A TCP or UDP port is a number from 0 to 65535 that tells the operating system which program should receive a connection. A random port generator draws numbers from that range for you. You need one whenever you start a second copy of a service on the same machine and the default port is taken: a local database next to another one, a dev server per branch, one container per test run, or a mapping such as -p 49321:80 in a compose file.
Not every number is a good choice, and that is why the ranges matter. The IANA registry splits the space in three: system ports (0 to 1023), which usually need administrator rights to listen on, registered or user ports (1024 to 49151), which software vendors may claim, and dynamic or private ports (49152 to 65535), which are meant for temporary use and not assigned to anyone. This generator starts on 1024 to 65535 so that the result never needs root. For a service you start by hand, the registered range is the usual pick; for something short-lived such as a test, the dynamic range avoids clashing with installed software.
The draw happens in your browser with Math.random. That is fine for choosing a development port and not meant for secrets: a port number is not a password, and a predictable draw is acceptable here. Nothing is sent anywhere. What the page cannot do is check whether the number is already in use on your machine; for that, see the commands in the code section.
How to generate a random port
- Open the page. A single port is already drawn, in the unprivileged range, so you can copy it right away.
- Choose the range: unprivileged (1024 to 65535), registered (1024 to 49151), dynamic or private (49152 to 65535), or a custom range with a first and a last port. In a custom range, both ends can be drawn.
- Set the quantity, from 1 to 100. With more than one port the result becomes a list, one port per line when copied.
- Leave the option for no repeated ports on if you need distinct numbers, for example to give each service in a stack its own port.
- Turn on the option that skips popular ports to avoid numbers that well-known software uses by default, such as 3000, 5432, 6379, 8080 or 27017.
- Press the refresh button to draw again, and the copy button to take the result. Changing any option also draws again. Your choices are remembered for the next visit.
The numbers are ready to paste in a ports: line of the Docker run to Compose converter. If you also need a password or key for the service, the token generator is the right tool, not this one.
Examples
One port for a local dev server
Default options: range 1024 to 65535, quantity 1. A possible result:
1027Start your server with that port, for example PORT=1027 npm start.
Five distinct dynamic ports
Range dynamic (49152 to 65535), quantity 5, no repeated ports. A possible result, one per line:
64237
53890
52601
61102
57879Every value is between 49152 and 65535 and none appears twice.
A custom range that skips popular ports
Range custom, first 3000, last 3010, quantity 3, skip popular ports on. The draw can return 3001 to 3010, but never 3000, because 3000 is in the list of popular ports (Node and React dev servers use it). With the option off, 3000 is allowed.
Common errors and how to fix them
- Ports go from 0 to 65535. Change the range to fit inside it: a custom first or last port is outside the valid range. Port numbers are 16 bits wide.
- The first port is greater than the last: the two custom limits are swapped. Swap them or lower the first one.
- The range has fewer ports than requested: with no repeats on, you cannot ask for 20 distinct ports in a range of 10. Widen the range, lower the quantity or allow repeated ports. Skipping popular ports also shrinks the range.
Frequently asked questions
How do I pick a random port that is not in use?
Draw one here, then check it on your machine. On Linux, ss -ltn lists the ports that are listening; on macOS, lsof -iTCP -sTCP:LISTEN -n -P. A port that does not appear is free at that moment. For software, the most reliable way is to ask the operating system for port 0 and read back the number it assigned.
Which port range is safe for my application?
For a development server, any port from 1024 up works without administrator rights. The registered range (1024 to 49151) is the usual home of applications. The dynamic range (49152 to 65535) is the one IANA sets aside for temporary use, and operating systems draw the ports of outgoing connections from a range of their own (Linux uses 32768 to 60999 by default), so a fixed service in these ranges can occasionally collide with one of them.
Are the numbers truly random?
They come from Math.random, a general-purpose generator that is not cryptographically secure. For picking a development port that is fine. Never use a port number as a secret.
Can I generate several ports at once?
Yes, up to 100 per draw. With the option for no repeats on, they are all different, and the whole list is copied with one click.
How to get a random port in code
JavaScript
In Node, listen on port 0 to let the system pick a free one, or use crypto.randomInt for a number in a range (the upper limit is exclusive).
import net from 'node:net';
import { randomInt } from 'node:crypto';
console.log(randomInt(1024, 65536)); // 1024 to 65535
const server = net.createServer().listen(0, '127.0.0.1', () => {
console.log(server.address().port); // a port that was free
server.close();
});Python
secrets.randbelow draws a number in a range; binding to port 0 asks the system for a free one.
import secrets
import socket
print(secrets.randbelow(65535 - 1024 + 1) + 1024)
s = socket.socket()
s.bind(("127.0.0.1", 0))
print(s.getsockname()[1]) # a port that was free
s.close()Go
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
defer l.Close()
fmt.Println(l.Addr().(*net.TCPAddr).Port)Bash
Check whether a port is in use before you pick it.
lsof -iTCP:8080 -sTCP:LISTEN -n -P # macOS and Linux with lsof
ss -ltn 'sport = :8080' # Linux
shuf -i 1024-65535 -n 1 # Linux (GNU coreutils), 1024 to 65535