Get the App
SLTechnology News&Howtos  ›  Servers  › 

A simple method for testing TCP/UDP Port Connectivity in Linux operating system

Shulou Source: shulou.com Published: 2022-06-02 05:21:12 10月10日 Update

When using Linux operating system, it is sometimes necessary to test the connectivity of ports to confirm whether the TCP and UDP protocol stacks of the system can run normally. This article explains this briefly.

TCP port test

Test existing listening port connectivity using telnet

You can use Linux's telnet tool to test connectivity of existing ports. The test command is:

telnet

# host is the target server IP, port is the port number to be tested

Examples:

The connectivity of port 22 can be tested with the following command:

telnet 127.0.0.1 22

If the connection is successful, a message similar to the following will be displayed. The display may vary depending on the Linux environment, but it usually contains "Connected to... "message, the connection is successful.

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

SSH-2.0-OpenSSH_5.3

In addition, in Windows, after a successful connection, a new window appears (with or without any echo). If the connection fails, a message similar to the following appears:

C:\>telnet 127.0.0.1 1111

Connecting to 127.0.0.1... Unable to open connection to host. On port 1111: connection failed

Create a new listening port test

Python's built-in Web server can be used to temporarily create new listening ports for testing. The usage is as follows:

python -m SimpleHTTPServer

Example output:

[root@centos]# python -m SimpleHTTPServer 123

Serving HTTP on 0.0.0.0 port 123 ...

Description: The Web service started is single-threaded, with the current directory as the root directory, and can only accept one request at a time, which is generally only used for testing. Press Ctr +C to terminate the process.

UDP Port Test

telnet can only be used for TCP port testing, to test UDP ports, you can use nc program.

Verify that nc is installed in your system using the following command:

which nc

Example output:

[root@centos]# which nc

/usr/bin/nc

If nc is not installed, use yum or apt-get to install it, depending on the operating system, which is not described in detail in this article.

Test the connectivity of the UDP port on the target server using the following command:

Usage:

nc -vuz

Example output:

[root@centos]# nc -vuz 192.168.0.1 25

Connection to 192.168.0.1 25 port [udp/smtp] succeeded!

#If the returned result contains the word "succeeded", it means that the corresponding port access is normal. If there is no return message, it means that the corresponding port access failed.

Parameter Description:

-v verbose output (two-v's for more verbose content)

-u Use UDP transport protocol

-z lets nc scan ports only and not send any data

Note: The above content is for reference only. The article reference source http://raksmart.idcspy.com, please indicate if it is reproduced.

Tags: Port test system information example service output different success instruction server monitor operating system content tools environment target reference two host Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Huawei Shulou Information vpn OPPO Reno