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

HTML iframe usage summary collection

Detailed Analysis of Iframe Usage <iframe frame...

JavaScript manual implementation of instanceof method

1. Usage of instanceof instanceof operator is use...

Centos6.5 glibc upgrade process introduction

Table of contents Scenario Requirements glibc ver...

Vue Element front-end application development: Use of API Store View in Vuex

Table of contents Overview 1. Separation of front...

Detailed explanation of Java calling ffmpeg to convert video format to flv

Detailed explanation of Java calling ffmpeg to co...

JavaScript implements the nine-grid mobile puzzle game

This article shares the specific code for JavaScr...

Example of ellipsis when CSS multi-line text overflows

Ellipses appear when multi-line text overflows Th...

Detailed explanation of MySQL InnoDB secondary index sorting example

Sorting Problem I recently read "45 Lectures...

CentOS8 network card configuration file

1. Introduction CentOS8 system update, the new ve...

MySQL group query optimization method

MySQL handles GROUP BY and DISTINCT queries simil...

Introduction to the usage of common XHTML tags

There are many tags in XHTML, but only a few are ...

A simple method to merge and remove duplicate MySQL tables

Scenario: The crawled data generates a data table...

js to realize the function of uploading pictures

The principle of uploading pictures on the front ...

Summary of practical experience of HTML knowledge points

1. The table tag is table, tr is row, td is cell, ...

Briefly describe the difference between Redis and MySQL

We know that MySQL is a persistent storage, store...