How do you decide what port to use?
Asked Answered
E

8

99

This is a little subjective, as there are no rules so to speak. Every time I create a server, I think to myself, "What is the best port to use?" I guess an answer is "Any, as long as the user can change it." So, how does everyone else decide how to choose the default port? Personally, I like to use something like 8000-something if it's HTTP related, and I've noticed this is a pretty common trend. But what if 8000 is already in use? Use 8001? It seems a little ad-hoc, and I suppose it is.

Clearly I'm not the first to have asked this question; IANA maintain a port numbers list... Which leads me on to the unassigned range (48620-49150). I guess we should really be using these, but why don't more programmers do so? How do you decide which to use; if everyone started at #1, then we'd all be using 48620.

Egret answered 4/2, 2010 at 14:9 Comment(2)
+1 for answering your own questionTrehala
Thanks - I wanted to tell everyone my thought process, so that others can call me on it if I was wrong.Egret
M
53

I think you've pretty much answered your question as much as is possible; there isn't really a strict rule you can follow here beyond what you've said. But generally:

  • Look at the IANA list and pick a port that's not in use.
  • Pick a port number that is easy to remember.
  • Don't fix the port number in your code. Some other product may have picked the same port as you and you never know when you'll have to co-exist on a server, so put the port number in a configuration file somewhere so it can be changed without a recompile if necessary. The ability to change port number can also be helpful for getting through firewalls without having to get them reconfigured. (You can always default to your chosen value if the configuration file doesn't exist.)
  • There is an argument that you don't want to pick something too high as you may conflict with the range used for ephemeral ports. It's not that likely that you'll get hit by this, but it's a hard problem to debug when it happens.

(And if you want a tip for picking memorable port numbers, I once worked with someone who remembered port numbers based around the telephone extensions of his co-workers.)

Moran answered 4/2, 2010 at 14:23 Comment(2)
"Look at the IANA list and pick a port that's not in use" or just pick a large numberKragh
A large number under 49152 obviously, I knew that, really I didKragh
N
29

Some easy to remember and appropriately nerdy unassigned (per IANA) ports:

27182 (e)

31415 (pi)

60221 (avagadro's)

Necromancy answered 4/2, 2010 at 14:38 Comment(1)
The top answer here says for ssh you should use a root-reserved port: unix.stackexchange.com/questions/424755/…Ables
E
19

During testing... always port #666 ;)

Emilie answered 4/2, 2010 at 14:16 Comment(2)
I take it you always run as root?Mexican
it's reserved for the Doom game! you should respect itRambow
D
4

How about:

defaultPort = (new Random()).Next(48620, 49150);
Darwin answered 5/2, 2010 at 15:38 Comment(0)
T
1

You answered your own question? Pick any unassigned port and allow the user to change it.

Trehala answered 4/2, 2010 at 14:13 Comment(0)
R
1

I prefer this way: (python code following)

#!/usr/bin/env python3
import random as R
r = R.SystemRandom()
print([r.randrange(1024, 65535) for x in range(4)])

And then I pick the number which I like the most. Or course, change the range if you have some stricter limits of what are acceptable numbers.

Rambow answered 26/8, 2015 at 17:59 Comment(2)
int.from_bytes(hashlib.sha256(b"my application name").digest()[:4], "little") & 0xFFFFRunlet
I would not leave it up to random. Could easily pick one that is being used by ephemeral root process launchers from the system at higher ranges.Goodwife
G
0

After a quick Google search to make sure it's clear, I generally just choose a number of personal significance.

Gaskins answered 4/2, 2010 at 14:13 Comment(0)
C
0

I'd suggest never use a port that is a big number like 5 digits, as it might hit some other operation system processes and assigns the Ephemeral ports. You would start to get 'Already in use errors' due to its limitations.

Circumscription answered 15/7, 2016 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.