Cyberithub

Best Examples to Test Port Connectivity in Linux(RedHat 7/CentOS 7/Ubuntu 18.04)

Advertisements

In this tutorial, I will take you through the different tools that can be used to check/test port connectivity in Linux.

Test Port Connectivity

You might be aware of curl, netstat, telnet and nc command in Linux: -

curl - It is usually used for downloading web pages or files from a Linux/Unix command line. But there's another great usage curl command has: testing TCP ports connectivity. Taking an example, let's assume you're helping with some network changes and need to confirm that connection from your server to some remote host and specific TCP port still works.

telnet - telnet command is used for interactive communication with another host using the TELNET protocol. It begins in command mode, where it prints a telnet command prompt ("telnet>"). If telnet is invoked with a host argument, it performs an open command implicitly.

nc - nc command is for performing maintenance/diagnosis tasks related to network . It can perform operations like read,write or data redirections over the network, similar to how you can use cat command to manipulate files on Linux system

 

What is Port

Ports are basically Communication endpoint in Network Topology through which outbound and inbound traffic flows.

Prerequisites

You need to have netstat, curl, nc and telnet command in linux.

How to Install and Use netstat command in Linux
Top 10 Ping Command Examples in Linux

Best Examples to Test Port Connectivity in Linux(RedHat 7/CentOS 7/Ubuntu 18.04) 1

Test TCP Ports

What is TCP 

TCP stands for Transmission Control Protocol. Using this method, the system sending the data connects directly to the computer it is sending the data it to, and stays connected for the duration of the transfer.

With this method, the two systems can guarantee that the data has received safely and correctly without compromising the integrity of packets, and then they disconnect the connection.

This method of transferring data tends to be quicker and more reliable, but puts a higher load on the system as it has to monitor the connection and the data flowing across it.

Here's how you can do it using curl command and its telnet functionality.

Test SSH port connection with curl

You can test local port 22 through curl command as specified below.

[root@localhost ~]# curl -v telnet://127.0.0.1:22
* About to connect() to 127.0.0.1 port 22 (#0)
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 22 (#0)
SSH-2.0-OpenSSH_7.4
^C

Test SSH port connection with telnet

You can test port 22 by using telnet command as mentioned below.

[root@localhost ~]# telnet 127.0.0.1 22
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4
^C

Test UDP Ports

What is UDP 

UDP is known as User Datagram Protocol. Using this method, the system sending the data packages the information into a nice little packets and releases it into the network with the hopes that it will get to the right destination.

What this means is that UDP does not connect directly to the receiving computer like TCP does, but rather sends the data out and relies on the network devices in between the transmitting system and the receiving system to get the data where it is supposed to go properly.

This method of transmission does not provide any guarantee that the data you transmit will ever reach its destination. On the other hand, this method of transmission has a very low overhead and is therefore very popular to use for services that are not that important to work on the first go.

Test SSH port connection with nc

You can test udp port 123 by using below netcat command:-

[root@localhost ~]# nc -v -z -u 127.0.0.1 123
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 127.0.0.1:123.
Ncat: UDP packet sent successfully
Ncat: 1 bytes sent, 0 bytes received in 2.02 seconds.

Also Read: How to create Network Bonding in Linux

Reference: curl commands

Leave a Comment