Operating System - HP-UX
1822168 Members
3331 Online
109640 Solutions
New Discussion юеВ

how to find a free port to use

 
Anders Gullberg
Frequent Advisor

how to find a free port to use

Hi all,

Im trying to help a customer to use an application that want to use port 10000.
I looked in /etc/services and find that the port is busy. How should i find a port that are free to use for an application?
11 REPLIES 11
Bharat Katkar
Honored Contributor

Re: how to find a free port to use

Hi,
Use
# netstat -a
To see which port are in use.
Regards,
You need to know a lot to actually know how little you know
Anders Gullberg
Frequent Advisor

Re: how to find a free port to use

Hi Bharat,

Maybe a dumb question but is it free to use any port that are not in netstat -a?
How do i lock that port to the application?
Fred Ruffet
Honored Contributor

Re: how to find a free port to use

A port that is listed in /etc/services is not necessarly used. It is just a Database referencing which process is commonly using this port.

netstat -a will give you used ports.

Once your program is launched, it opens the port. No other program will then be able to open the port. It is a kind of lock.

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Anders Gullberg
Frequent Advisor

Re: how to find a free port to use

Hi Fred,
and thanks for your explanation.
The application wanted port 10000 and its not in use when i look in netstat -a.
But still its give a error message.
I tried to telnet to port 10000 and 10001 and it gives connection refused. Shouldnt that work?

//Anders
A. Clay Stephenson
Acclaimed Contributor

Re: how to find a free port to use

If you attempt to telnet using ports 10000 and 10001 and get a connection refused, that means that there is no currently operating listener on that port. You should note that both ports 10000 and 10001 are registered ports and therefore should not be used. Your vendor should have made a wiser choice because he is asking for collision with other applications. Having said this, as long as the port is not being used, it can be used for your servive but the question is "should it be used?".

http://www.iana.org/assignments/port-numbers
If it ain't broke, I can fix that.
Fred Ruffet
Honored Contributor

Re: how to find a free port to use

From Clay's post, we can see that 1521 port does not belong to Oracle... :) they should use 66 or 1525 port.

Don't your application offers you a way to configure which port it is listening to ?

Isn't there a log to see why it does not listen on 10000 once launched ?

Regards,

Fred


--

"Reality is just a point of view." (P. K. D.)
Stuart Abramson
Trusted Contributor

Re: how to find a free port to use

As stated above, /etc/services is only a "map" to who uses what ports.

"lsof" will also show ports used, if you have it. You can get it from one of the "porting centers" (I could look it up for you...)

You should use "netstat -a" to identify the "generally used" ports on your system, then pick a port that is:
a. Not used in netstat -a
b. Not listed in /etc/services.

Then, after testing, you should edit /etc/services to put your new port in it, so the next SA will know what is going on.

Ports below 1024 are reserved for hp-ux.

Many programs, such as ftp and nfs, pick random unused ports to use for short periods, and then release them. They may have ranges within which they pick.

Mark Greene_1
Honored Contributor

Re: how to find a free port to use

The port numbers are divided into three ranges:
The Well Known Ports are those from 0 through 1023.
The Registered Ports are those from 1024 through 49151
The Dynamic and/or Private Ports are those from 49152 through 65535


Your vendor should make use of an open port on your system above 49151.


mark
the future will be a lot like now, only later
Anders Gullberg
Frequent Advisor

Re: how to find a free port to use

Thank you all!

For your explanation, its more clear to me now!
Bill Thorsteinson
Honored Contributor

Re: how to find a free port to use

You should avoid ports listed on
http://www.iana.org/assignments/port-numbers
for uses other than listed.

That being said webmin uses 10000 by default
but it is now registered for other uses.
Oracle traditionally uses port 1521, which
is registered otherwise.

If you are sure the registered service won't
be installed on the server. Then there is
no strong reason not to use that port.

However, it is good practice to avoid using
ports assigned to well-known services such
as WWW, HTTPS, SMTP, and other services
covered by the RFCs for services other than
intended. Likewise, these services should
be run on their assigned ports.

On unix ports 0-1024 are restricted to use by
programs started by root. This are generally
trusted ports. Windows and other OSs don't
follow this restriction, so you can no longer
count on connections on these ports being
blessed by a sys admin.

As noted add the port usage to your
/etc/services file. You may also want to
have an extra .local or .patch file to retain
the changes if the file gets upgraded.
Comment out the original assignment if reusing the port.
rick jones
Honored Contributor

Re: how to find a free port to use

Once your application claims to have started, make sure it is running with ps -ef | grep app binary and make sure it has created its listen endpoints on ports 10000 and 10001 with netstat -a | grep tcp | grep LISTEN or netstat -a | grep 1000

If you get a connection refused when you try to telnet to those port numbers on the server, it suggests one or more of:

*) application is not actually running
*) application's attempt to create the listen endpoint failed
*) inetd.sec isn't allowing the connection
*) someother nasty firewall hasn't had those ports opened
*) whatever else I've forgotten

The official port ranges have been given - some of the historical was that the anonymous/ephemperal ports started at 5000 (or was it 5001). Either way, your app vendor should have a mechanism for you to choose the port, and they should obtain a registered port number.
there is no rest for the wicked yet the virtuous have no pillows