How to Check Remote Ports are Reachable Using ‘nc’ Command

A port is a logical entity which acts as a endpoint of communication associated with an application or process on an Linux operating system. It is useful to know which ports are open and running services on a target machine before using them.

We can easily list open ports in Linux on a local machine using the netstat or several other Linux commands such NMAP.

In this guide, we will show you how to determine if ports on a remote host are reachable/open using simple netcat (in short nc) command.

netcat (or nc in short) is a powerful and easy-to-use utility that can be employed for just about anything in Linux in relation to TCP, UDP, or UNIX-domain sockets.

# yum install nc                  [On CentOS/RHEL]
# dnf install nc                  [On Fedora 22+]
$ sudo apt-get install netcat     [On Debian/Ubuntu]

We can use it to: open TCP connections, listen on arbitrary TCP and UDP ports, send UDP packets, do port scanning under both IPv4 and IPv6 and beyond.

Using netcat, you can check if a single or multiple or a range of open ports as follows. The command below will help us see if the port 22 is open on the host 192.168.56.10:

$ nc -zv 192.168.1.15 22

In the command above, the flag:

  1. -z – sets nc to simply scan for listening daemons, without actually sending any data to them.
  2. -v – enables verbose mode.

The next command will check if ports 80, 22 and 21 are open on the remote host 192.168.5.10 (we can use the hostname as well):
nc -zv 192.168.56.10 80 22 21

It is also possible to specify a range of ports to be scanned:’

$ nc -zv 192.168.56.10 20-80

For more examples and usage of netcat command, read through our articles as follows.

  1. Transfer Files Between Linux Servers Using netcat Command
  2. Linux Network Configuration and Troubleshooting Commands

That’s all. In this article, we explained how to check if ports on a remote host are reachable/open using simple netcat commands. Make use of the comment section below to write back to us concerning about this tip.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

11 Responses

  1. Bernard says:

    Hi,

    Don’t use this for connectivity checks on Centos 7. This only works on Centos 6. It does not work on Centos 7. You need something like this:

    # echo | nc -w1 $server $port >/dev/null 2>&1 ;echo $?
    

    Its because redhat/centos distro is changed at Centos 7.

  2. Timir Mazumder says:

    Hi,

    I have Red Hat Enterprise Linux Server release 7.3 (Maipo) installed on my PC.

    When I try either of the below commands, I get the error message as shown:

    # ncat -z 127.0.0.1 5556
    # nc -z 127.0.0.1 5556
    

    Error Message:
    nc: invalid option — ‘z’
    Ncat: Try `–help’ or man(1) ncat for more information, usage options and help. QUITTING.

    ‘–help’ or man() did not provide any details on how to simply scan for listening daemons.

    Any suggestions?

  3. Jonathan says:

    Very useful article for security. I’ll test that.Thank you !
    Long life to super penguin ^^

  4. Chris Pesoa says:

    Hello.

    In CentOS 7, there is no package named ‘nc‘.
    Searching for ‘netcat‘:

    $ yum search netcat
    

    the resulting package is ‘nmap-ncat.x86_64 : Nmap’s Netcat replacement

    If you install this package, the above command will looks like this:

    $ nc --recv-only -z 192.168.1.15 22
    

    There is another package ‘netcat-gnu‘. You can read more about this package in: https://fedora.pkgs.org/25/rpm-sphere/netcat-gnu-0.7.1-15.1.x86_64.rpm.html

    Are this observations correct?

Leave a Reply to Aaron Kili Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.