Show tcp ports that are listening, show numbers only (don't resolve names - makes it was faster) and show the process that is doing the listening (the p only works if you are root)
netstat -ulnp
Show udp ports that are listening, show numbers only (don't resolve names - makes it was faster) and show the process that is doing the listening (the p only works if you are root)
netstat -unp
Show udp ports that are open but not listening, show numbers only (don't resolve names- makes it was faster) and show the process that is doing the listening (the p only works if you are root)
netstat -an
Show all ports in use, show numbers only - don't resolve names
lsof -i <proto>@<host>:<port>
e.g
lsof -i tcp@localhost:25
to see if anything is listening on port localhost 25/TCP, or
You don't mention what protocol you want to use, i.e. TCP or UDP - and it's also important to realise that "port" isn't quite as granular the system supports to disambiguate sockets. E.g. if your system has multiple IP addresses then port 80 might be in use on all IP addresses (either the application has bound to "0.0.0.0" or "::" or to each IP address in succession), or it might be in use only on a subset of those IP addresses.
The best, and surest, way to determine if a port/address is free and available is to attempt to bind to it. Netcat is handy for this.
nc -l [-s a.b.c.d] -p NN
will attempt to bind to TCP port NN on (optional, the default will be all addresses) a.b.c.d. Add the -u option to do the same in UDP.
Next, to tell if the port is truly "open" as you ask - you need to start looking at potential firewall rules. Again the easiest thing is to try to connect to the port. Use netcat as above, on the server, and from a client use netcat to attempt to the connect to the port you opened.
nc [-u] a.b.c.d NN
will connect to port NN on a.b.c.d, using UDP if the -u flag is specified. You can then type input into the client end, and it should show up on the server. If it doesn't, you need to look into system and network specific tools.
A lot ways of doing it gave me issues because they didn't work on linux and osx, and/or because they didn't show the ports used by docker, or processes that were owned by root. Now I just use this javascript program:
(make sure you have node installed and that it works with node not just nodejs or change the program accordingly)
save the following to a file called check-port in your path or in your project
#!/usr/bin/env node
var http = require('http');
var app = new http.Server();
app.listen(process.argv[2], () => {
process.exit(0)
});
Use "netstat" to check the presently using ports.
This (netstat) is the fastest solution...
...but this gives you more control (at the cost of speed (sometimes a lot of speed))...
The above example for example shows you all the TCP ports open and in the
LISTEN
state, AND (-a
) belonging to the Apache (www-data
) user.Try
If you get any results, something is listening and bound, eg
All good answers.
However you don't mention if you are logged onto the computer in question. ;P
If not, then nmap is your friend.
for starters try:
nmap -O
target
amap is also a good choice which will also attempt to guess server software by grabbing banner pages.
for starters try:
amap
target
1-6000
Show
t
cp ports that arel
istening, shown
umbers only (don't resolve names - makes it was faster) and show thep
rocess that is doing the listening (thep
only works if you are root)Show
u
dp ports that arel
istening, shown
umbers only (don't resolve names - makes it was faster) and show thep
rocess that is doing the listening (thep
only works if you are root)Show
u
dp ports that are open but not listening, shown
umbers only (don't resolve names- makes it was faster) and show thep
rocess that is doing the listening (thep
only works if you are root)Show
a
ll ports in use, shown
umbers only - don't resolve namese.g
to see if anything is listening on port localhost 25/TCP, or
to see if there are any sockets either local or remote either listening (local) or connected to (local or remote) for any host/interface
lsof (list open files) is a good tool to see if a process is listening on a port
netstat is a good tool for seeing if there are any active connections.
You don't mention what protocol you want to use, i.e. TCP or UDP - and it's also important to realise that "port" isn't quite as granular the system supports to disambiguate sockets. E.g. if your system has multiple IP addresses then port 80 might be in use on all IP addresses (either the application has bound to "0.0.0.0" or "::" or to each IP address in succession), or it might be in use only on a subset of those IP addresses.
The best, and surest, way to determine if a port/address is free and available is to attempt to bind to it. Netcat is handy for this.
will attempt to bind to TCP port NN on (optional, the default will be all addresses) a.b.c.d. Add the -u option to do the same in UDP.
Next, to tell if the port is truly "open" as you ask - you need to start looking at potential firewall rules. Again the easiest thing is to try to connect to the port. Use netcat as above, on the server, and from a client use netcat to attempt to the connect to the port you opened.
nc [-u] a.b.c.d NN
will connect to port NN on a.b.c.d, using UDP if the -u flag is specified. You can then type input into the client end, and it should show up on the server. If it doesn't, you need to look into system and network specific tools.
I use fuser (in package psmisc):
Brings back the pid of process bound to this port.
If this is to know if a port is listening, the good old telnet does the trick :)
This one-liner will get you a list of all TCP ports in use. It works in bash on Ubuntu and OS X.
netstat -ant | sed -e '/^tcp/ !d' -e 's/^[^ ]* *[^ ]* *[^ ]* *.*[\.:]\([0-9]*\) .*$/\1/' | sort -g | uniq
The list will have one port per line without any extra information.
A lot ways of doing it gave me issues because they didn't work on linux and osx, and/or because they didn't show the ports used by docker, or processes that were owned by root. Now I just use this javascript program:
(make sure you have node installed and that it works with
node
not justnodejs
or change the program accordingly)save the following to a file called
check-port
in your path or in your projectset permissions
run from your path
or run from the same directory
so far it is working pretty well.