How can I check if a port is listening on a Linux server?
9 Answers
You can check if a process listens on a TCP or UDP port with ss -tuplen
(replacement of netstat).
To check whether some ports are accessible from the outside (this is probably what you want) you can use a port scanner like Nmap from another system. Running Nmap on the same host you want to check is quite useless for your purpose.
- 65GNU netstat knows the parameters
-t
,-u
,-p
,-l
,-e
, and-n
. Thanks to the options parser it can be expressed as-tuplen
. linux.die.net/man/8/netstatjoschi– joschi2013-04-26 09:53:51 +00:00Commented Apr 26, 2013 at 9:53 - 4Also, the
telnet
command usually does only supports TCP, so you're out of luck if the service you want to check runs on another protocol.joschi– joschi2013-04-26 09:55:06 +00:00Commented Apr 26, 2013 at 9:55 - 3nc is (better) alternative to telnet. It supports UDP too.Tsvetomir Dimitrov– Tsvetomir Dimitrov2013-07-10 08:42:04 +00:00Commented Jul 10, 2013 at 8:42
- 2When I go to the
netstat
man
page it saysnetstat
is obsolete.trysis– trysis2018-05-16 12:41:35 +00:00Commented May 16, 2018 at 12:41 - 6According to article: computingforgeeks.com/netstat-vs-ss-usage-guide-linux
netstat
is deprecated, andss
is it's replacement, so you can doss -an
,ss -tuplen
or for tcp listening socketsss -ntlp
.Alek_A– Alek_A2020-02-07 10:17:18 +00:00Commented Feb 7, 2020 at 10:17
Quickest way to test if a TCP port is open (including any hardware firewalls you may have), is to type, from a remote computer (e.g. your desktop):
telnet myserver.com 80
Which will try to open a connection to port 80 on that server. If you get a time out or deny, the port is not open :)
- 8"yum install telnet" to install the telnet client package.cjc– cjc2011-09-07 17:41:10 +00:00Commented Sep 7, 2011 at 17:41
- 2Says: telnet: connect to address 82.165.148.224: Connection refusedJames Anderson– James Anderson2011-09-07 17:42:35 +00:00Commented Sep 7, 2011 at 17:42
- 9Written above:
if you get a time out or deny, the port is not open
Industrial– Industrial2011-09-07 18:09:42 +00:00Commented Sep 7, 2011 at 18:09 - 2What if you don't have perms to install telnet? Is there another standard tool?KC Baltz– KC Baltz2014-01-14 23:40:52 +00:00Commented Jan 14, 2014 at 23:40
- 9I tried “telnet myhost 22” and get a timeout. But I can ssh into that machine. ?!Torsten Bronger– Torsten Bronger2018-09-06 10:59:52 +00:00Commented Sep 6, 2018 at 10:59
OK, in summary, you have a server that you can log into. You want to see if something is listening on some port. As root, run:
netstat -nlp
this will show a listing of processes listening on TCP and UDP ports. You can scan (or grep) it for the process you're interest in,and/or the port numbers you expect to see.
If the process you expect isn't there, you should start up that process and check netstat again. If the process is there, but it's listening on a interface and port that you did not expect, then there's a configuration issue (e.g., it could be listening, but only on the loopback interface, so you would see 127.0.0.1:3306 and no other lines for port 3306, in the case of the default configuration for MySQL).
If the process is up, and it's listening on the port you expect, you can try running a "telnet" to that port from your Macbook in your office/home, e.g.,
telnet xxxxxxxxxxxx.co.uk 443
That will test if (assuming standard ports) that there's a web server configured for SSL. Note that this test using telnet is only going to work if the process is listening on a TCP port. If it's a UDP port, you may as well try with whatever client you were going to use to connect to it. (I see that you used port 224. This is masqdialer, and I have no idea what that is).
If the service is there, but you can't get to it externally, then there's a firewall blocking you. In that case, run:
iptables -L -n
This will show all the firewall rules as defined on your system. You can post that, but, generally, if you're not allowing everything on the INPUT chain, you probably will need to explicitly allow traffic on the port in question:
iptables -I INPUT -p tcp --dport 224 -j ACCEPT
or something along those lines. Do not run your firewall commands blindly based on what some stranger has told you on the Internet. Consider what you're doing.
If your firewall on the box is allowing the traffic you want, then your hosting company may be running a firewall (e.g., they're only allowing SSH (22/tcp), HTTP (80/tcp) and HTTPS (443/tcp) and denying all other incoming traffic). In this case, you will need to open a helpdesk ticket with them to resolve this issue, though I suppose there might be something in your cPanel that may allow it.
- Could you pls add how to undo the iptables -I command? Thanks!!Evgeny– Evgeny2013-09-09 22:52:51 +00:00Commented Sep 9, 2013 at 22:52
- 1"iptables -D" followed by whatever else you had after the "-I" in the original command. Basically, look up the documentation.cjc– cjc2013-09-10 11:45:53 +00:00Commented Sep 10, 2013 at 11:45
- 1I'd highly recommend using
ufw
(justapt install ufw
then seeman ufw
) it's a more user-friendly frontend foriptables
.Nagev– Nagev2020-06-10 10:40:48 +00:00Commented Jun 10, 2020 at 10:40
I use the combo of netstat
and lsof
:
netstat -an | grep <portnumber> lsof -i:<portnumber>
To see if the port is being used, and what is using it.
- nothing prompts with or without sudoDheeraj Thedijje– Dheeraj Thedijje2017-11-22 06:25:20 +00:00Commented Nov 22, 2017 at 6:25
- 2@DheerajThedijje - then that port isn't openwarren– warren2018-05-25 14:47:06 +00:00Commented May 25, 2018 at 14:47
- 1Yes it was not, got it.Dheeraj Thedijje– Dheeraj Thedijje2018-06-06 07:29:00 +00:00Commented Jun 6, 2018 at 7:29
If you need to script such a test, the solution by Serhii Popov (see comment to question) is probably the best since nc
is capable of searching the TCP stack for an open port³ instead of attempting an actual connection.
The simplest form is:
nc -z <ip> <port>
The command returns true if it find the specified <ip>:<port>
combo as being opened (i.e. one of your services is listening).
So now you can write a script to wait until the port is open:
while ! nc -z <ip> <port> do sleep 1 done
Note 1: I tried the -w
command line option and that did not seem to do anything. Either way the command returns immediately. I think that the -w
is not useful with -z
.
Note 2: to help debug, try with the -v
command line option.
Note 3: nc -z ...
actually creates a socket()
and then attempts to bind()
it and connect()
. If that works, it deems the port open.
- 4
-w
is very useful if the port is not open and packages are dropped, otherwisenc
will wait forever.Carl Hörberg– Carl Hörberg2020-07-15 20:02:00 +00:00Commented Jul 15, 2020 at 20:02 - For me
-w5
works perfectly, like stated in this anwserglacier– glacier2025-09-30 06:06:24 +00:00Commented Sep 30 at 6:06
If you are connected to the system and can run a command as root then you can check the output of iptables
iptables -L -vn
this will list the firewall rules and which ports are open target ACCEPT
and any explicitly closed ports target REJECT
.
- 1And if you have firewalld, it's simpler
firewall-cmd --query-port=port/protocol
, e.g.firewall-cmd --query-port=80/tcp
.Agostino– Agostino2018-07-20 16:00:31 +00:00Commented Jul 20, 2018 at 16:00
lsof -i :ssh
will list all processes with the ssh port open, both listening and active connections.
- Prefix
sudo
if it doesn't return any output.Elijah Lynn– Elijah Lynn2017-06-13 23:08:01 +00:00Commented Jun 13, 2017 at 23:08 - 1@ElijahLynn Actually
sudo
is required for any connections opened by other users (and likelyLISTEN
ports which are opened by services such asssh
orhttp
).Alexis Wilke– Alexis Wilke2020-03-07 17:53:59 +00:00Commented Mar 7, 2020 at 17:53
Check Internally
netstat -tulpen | grep $PORT lsof -i:$PORT
Check Externally
You can use
nc -z $IP $PORT
You can use Telnet
telnet $IP $PORT
You can use this online tool (I like this one)
https://www.yougetsignal.com/tools/open-ports/
And port scanner like Nmap
CLI tool
If there are no utilities available, for example, in a Docker container:
(echo >/dev/tcp/127.0.0.1/80) &>/dev/null && echo "open" || echo "closed"
- 1
netstat -an | grep PORTNUMBER | grep -i listen
If the output is empty, the port is not in use.nc -w5 -z -v <ip_address> <port_number>
, you should get something likeConnection to 127.0.0.1 9000 port [tcp/*] succeeded!
, otherwise port is closed.