Help Center/ Elastic Cloud Server/ Best Practices/ Maintaining and Monitoring ECSs/ Testing TCP/UDP Port Connectivity in Linux
Updated on 2026-07-29 GMT+08:00

Testing TCP/UDP Port Connectivity in Linux

Scenarios

This section describes how to test the connectivity of TCP and UDP ports on Linux instances, helping diagnose network status in abnormal scenarios.

Testing TCP Port Connectivity

Select an appropriate solution based on the port status:

  • If the port to be tested is being listened on, use Telnet to test the port. For details, see solution 1.
  • If the port to be tested is not being listened on, create a new listening port for testing. For details, see solution 2.

The solutions are described as follows:

Solution 1: Use Telnet to test a listening port.

  1. Log in to the Linux ECS. For details, see Login Overview (Linux).
  2. Run the following command to test the port connectivity using Telnet. Replace host_ip with the IP address of the target server and port with the port to be tested.

    sudo telnet <host_ip> <port>

    If the connection is successful, information similar to the following is displayed. The displayed information may vary depending on the OS, but it will contain "Connected to IP address".

Solution 2: Create a new listening port for testing.

Prepare two Linux ECSs, one as the server and the other as the client. During the test, the client assists in testing the connectivity of the port created on the server.

  1. Log in to the Linux ECS. For details, see Login Overview (Linux).
  2. Run the following command on the server to check the Python version:

    python -V

    If an error message is displayed, Python is not installed. In this case, you need to deploy Python. For details, see Setting Up a Python Environment.

  3. Run the following commands on the server to create a new listening port using the built-in web server of Python. Replace port with the one to be tested.

    # For Python 2.x
    sudo python -m SimpleHTTPServer <port>
    # For Python 3.x
    sudo python3 -m http.server <port>

  4. Run the following command on the client to use Telnet to test the connectivity of the port created on the server.

    Replace host_ip with the IP address of the server and port with the listening port created on the server.

    sudo telnet <host_ip> <port>

    If the connection is successful, information similar to the following is displayed. The displayed information may vary depending on the OS, but it will contain "Connected to IP address".

    Enter any test content, for example, test. An error message is displayed, but the input content test is displayed on the server.

    Message on the client

    Message on the server

Testing UDP Port Connectivity

Prepare two Linux ECSs, one as the server and the other as the client. During the test, the client assists in testing the connectivity of the port created on the server.

  1. Log in to the Linux ECS. For details, see Login Overview (Linux).
  2. Run the following command on the client and server respectively to check whether the nc program has been installed:

    which nc

    If the following information is displayed, the program has been installed.

    If the program is not installed, use Yum or apt-get to install it depending on the OS. The following uses Yum as an example to describe how to install the program.

    sudo yum install -y nc

  3. Run the following command on the server to create a listening port. Replace port with the port to be tested.

    sudo nc -uvlp <port>

    In this command, u indicates the UDP protocol, v indicates that detailed connection information is printed, l indicates the listening mode, waiting for incoming connections or packets, and p indicates the port number.

    If the command is executed successfully, the following information is displayed:

  4. Run the following command on the client to connect to the port to be tested on the server. Replace host_ip with the IP address of the server and port with the port to be tested.

    sudo nc -u <host_ip> <port>

  5. After the connection is successful, enter the test string test on the client and check whether the server receives the data synchronously. If the server receives the data, the port connectivity is normal.

    Message on the client

    Message on the server