Three ways to check whether a port is open in a remote Linux system

Three ways to check whether a port is open in a remote Linux system

This is a very important topic, not only for Linux administrators but also for all of us. I mean that for users working in the IT infrastructure industry, it is also very useful to understand this topic. They need to check whether a certain port is open on the Linux server before proceeding to the next step.

If the port is not open, they will go directly to the Linux administrator to open it. If this port is already open, we need to discuss with the application team what to do next.

In this article, we will show you 3 ways on how to check if a port is open.

This can be achieved using the following Linux commands:

  • nc
  • nmap
  • telnet

How to use nc (netcat) command to check whether a port is open in a remote Linux system?

nc stands for netcat. Netcat is a simple Unix tool that uses the TCP or UDP protocol to read and write data across network connections.

It is designed to be a reliable backend tool that can be used directly or simply called from other programs or scripts.

At the same time, it is also a feature-rich network debugging and exploration tool, as it can create almost any type of connection you need and has several interesting features built-in.

Netcat has three functional modes: connection mode, listening mode and tunnel mode.

The general syntax of the nc (netcat) command is:

$ nc [-options] [HostName or IP] [PortNumber]

In the following example, we will check whether port 22 is open in the remote Linux system.

If the port is open, you will get output similar to the following.

# nc -zvw3 192.168.1.8 22
Connection to 192.168.1.8 22 port [tcp/ssh] succeeded!

Command details:

  • nc
  • z
  • v
  • w3
  • 192.168.1.8
  • twenty two

When it detects that the port is not open, you will get the following output:

# nc -zvw3 192.168.1.95 22
nc: connect to 192.168.1.95 port 22 (tcp) failed: Connection refused

How to use nmap command to check whether a port is open in a remote Linux system?

nmap ("Network Mapper") is an open source tool for network exploration and security auditing, designed to quickly scan large networks, although it also works just as well against a single host.

Nmap uses raw IP packets in a novel way to determine whether hosts on the network are reachable, what services those hosts are providing (application name and version number), what operating systems they are running (system version), what packet filters or firewalls they are using, and other additional features.

Although nmap is commonly used for security auditing, many system and network administrators find it useful in everyday tasks such as inventorying network assets, managing service upgrade schedules, and monitoring whether a host or service is functioning properly.

The general syntax of nmap is:

$ nmap [-options] [HostName or IP] [-p] [PortNumber]

If the port is open, you will get output similar to the following:

# nmap 192.168.1.8 -p 22

Starting Nmap 7.70 ( https://nmap.org ) at 2019-03-16 03:37 IST Nmap scan report for 192.168.1.8 Host is up (0.00031s latency).

PORT STATE SERVICE

22/tcp open ssh

Nmap done: 1 IP address (1 host up) scanned in 13.06 seconds

If the port is not open, you will get a result similar to the following:

# nmap 192.168.1.8 -p 80
Starting Nmap 7.70 ( https://nmap.org ) at 2019-03-16 04:30 IST
Nmap scan report for 192.168.1.8
Host is up (0.00036s latency).

PORT STATE SERVICE
80/tcp closed http

Nmap done: 1 IP address (1 host up) scanned in 13.07 seconds

How to use telnet command to check whether a port is open in a remote Linux system?

The telnet command is used to interactively communicate with another host via the TELNET protocol.

The general syntax of the telnet command is:

$ telnet [HostName or IP] [PortNumber]

If the detection is successful, you will see output similar to the following:

$ telnet 192.168.1.9 22
Trying 192.168.1.9...
Connected to 192.168.1.9.
Escape character is '^]'.
SSH-2.0-OpenSSH_5.3
^]
Connection closed by foreign host.

If the probe fails, you will see output similar to the following:

$ telnet 192.168.1.9 80
Trying 192.168.1.9...
telnet: Unable to connect to remote host: Connection refused

Currently, we have only found the above 3 methods to check whether a port is open in a remote Linux system. If you have found other methods to achieve the same purpose, please let us know in the comment box below.

Summarize

The above are three methods that I introduced to you to check whether a port in a remote Linux system is open. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Summary of Linux methods for detecting whether a remote port is open
  • Interpret the network connection information displayed by the ip command under Linux
  • Method to detect whether ip and port are connectable

<<:  How to use JS to implement waterfall layout of web pages

>>:  MySQL reports an error: Can't find file: './mysql/plugin.frm' solution

Recommend

How to check if a table exists in MySQL and then delete it in batches

1. I searched for a long time on the Internet but...

HTML form_PowerNode Java Academy

1. Form 1. The role of the form HTML forms are us...

Practical record of MySQL 5.6 master-slave error reporting

1. Problem symptoms Version: MySQL 5.6, using the...

Docker-compose installation db2 database operation

It is troublesome to install the db2 database dir...

HTML tutorial, understanding the optgroup element

Select the category selection. After testing, IE ...

Summary of Common Problems with Mysql Indexes

Q1: What indexes does the database have? What are...

CSS navigation bar menu with small triangle implementation code

Many web pages have small triangles in their navi...

HTTP header information interpretation and analysis (detailed summary)

HTTP Header Explanation 1. Accept: Tells the web s...

How to quickly paginate MySQL data volumes of tens of millions

Preface In backend development, in order to preve...

Analysis of the difference between Mysql InnoDB and MyISAM

MySQL supports many types of tables (i.e. storage...

Modify the style of HTML body in JS

Table of contents 1. Original Definition 2. JS op...

JavaScript data structure bidirectional linked list

A singly linked list can only be traversed from t...

Implementation steps for docker deployment of springboot and vue projects

Table of contents A. Docker deployment of springb...

Let me teach you how to use font icons in CSS

First of all, what is a font icon? On the surface...