SnapOverflow

SnapOverflow Logo SnapOverflow Logo

SnapOverflow Navigation

  • Home
  • Server
  • Ubuntu

Mobile menu

Close
  • Home
  • System Administrators
    • Hot Questions
    • New Questions
    • Tags
  • Ubuntu
    • Hot Questions
    • New Questions
    • Tags
  • Help
Home / server / Questions / 309052
Accepted
James Anderson
James Anderson
Asked: 2011-09-08 09:24:24 +0800 CST2011-09-08 09:24:24 +0800 CST 2011-09-08 09:24:24 +0800 CST

Check if port is open or closed on a Linux server?

  • 772

How can I check if a port is listening on a Linux server?

linux port telnet
  • 7 7 Answers
  • 1235909 Views

7 Answers

  • Voted
  1. Best Answer
    joschi
    2011-09-08T09:31:43+08:002011-09-08T09:31:43+08:00

    You can check if a process listens on a TCP or UDP port with netstat -tuplen.

    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.

    • 236
  2. Adrian Macneil
    2011-09-08T09:33:22+08:002011-09-08T09:33:22+08:00

    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 :)

    • 129
  3. cjc
    2011-09-08T12:01:52+08:002011-09-08T12:01:52+08:00

    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.

    • 40
  4. warren
    2016-03-29T11:32:19+08:002016-03-29T11:32:19+08:00

    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.

    • 20
  5. user9517
    2011-09-08T09:57:04+08:002011-09-08T09:57:04+08:00

    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.

    • 10
  6. pgs
    2016-01-26T13:45:20+08:002016-01-26T13:45:20+08:00

    lsof -i :ssh will list all processes with the ssh port open, both listening and active connections.

    • 7
  7. Alexis Wilke
    2020-03-08T10:13:36+08:002020-03-08T10:13:36+08:00

    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.

    • 6

Sidebar

Stats

  • Questions 681965
  • Answers 980273
  • Best Answers 280204
  • Users 287326
  • Popular
  • Answers
  • Marko Smith

    Ping a Specific Port

    • 18 Answers
  • Marko Smith

    Check if port is open or closed on a Linux server?

    • 7 Answers
  • Marko Smith

    How to automate SSH login with password?

    • 10 Answers
  • Marko Smith

    How do I tell Git for Windows where to find my private RSA key?

    • 30 Answers
  • Marko Smith

    What's the default superuser username/password for postgres after a new install?

    • 5 Answers
  • Marko Smith

    What port does SFTP use?

    • 6 Answers
  • Marko Smith

    Resolve host name from IP address

    • 8 Answers
  • Marko Smith

    Command line to list users in a Windows Active Directory group?

    • 9 Answers
  • Marko Smith

    What is a Pem file and how does it differ from other OpenSSL Generated Key File Formats?

    • 3 Answers
  • Marko Smith

    How to determine if a bash variable is empty?

    • 15 Answers
  • Martin Hope
    Davie Ping a Specific Port 2009-10-09 01:57:50 +0800 CST
  • Martin Hope
    Smudge Our security auditor is an idiot. How do I give him the information he wants? 2011-07-23 14:44:34 +0800 CST
  • Martin Hope
    kernel Can scp copy directories recursively? 2011-04-29 20:24:45 +0800 CST
  • Martin Hope
    Robert ssh returns "Bad owner or permissions on ~/.ssh/config" 2011-03-30 10:15:48 +0800 CST
  • Martin Hope
    Eonil How to automate SSH login with password? 2011-03-02 03:07:12 +0800 CST
  • Martin Hope
    gunwin How do I deal with a compromised server? 2011-01-03 13:31:27 +0800 CST
  • Martin Hope
    Tom Feiner How can I sort du -h output by size 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich What is a Pem file and how does it differ from other OpenSSL Generated Key File Formats? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent How to determine if a bash variable is empty? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus How do you find what process is holding a file open in Windows? 2009-05-01 16:47:16 +0800 CST

Related Questions

Trending Tags

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • Home
  • Questions
    • Hot Questions
    • New Questions
  • Tags
  • Help

Footer

SnapOverflow

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Help

© 2022 SOF-TR. All Rights Reserve