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

Complete steps to solve 403 forbidden in Nginx

The webpage displays 403 Forbidden Nginx (yum ins...

How to add sudo permissions to a user in Linux environment

sudo configuration file The default configuration...

Basic usage examples of Vue named slots

Preface Named slots are bound to elements using t...

Sample code for implementing two-way authentication with Nginx+SSL

First create a directory cd /etc/nginx mkdir ssl ...

IE8 uses multi-compatibility mode to display web pages normally

IE8 will have multiple compatibility modes . IE pl...

CSS realizes the layout method of fixed left and adaptive right

1. Floating layout 1. Let the fixed width div flo...

Detailed process of deploying Docker to WSL2 in IDEA

The local environment is Windows 10 + WSL2 (Ubunt...

jQuery plugin to achieve seamless carousel

Seamless carousel is a very common effect, and it...

Tutorial on installing MySQL 5.6 on CentOS 6.5

1. Download the RPM package corresponding to Linu...

Use of Linux read command

1. Command Introduction The read command is a bui...

How to uninstall MySQL 8.0 version under Linux

1. Shut down MySQL [root@localhost /]# service my...