If you’re a network or system administrator, developer, or just someone who’s working on network-related tasks, you might encounter situations where you need to test the TCP connectivity between two machines.
Usually we use telnet, however because it’s insecure, it is not installed on the most distributions.
Alternatively, we can use Curl, a popular command-line tool used for transferring data over various protocols, including HTTP, FTP, SMTP, and others. it can also be used to test TCP connectivity. In this article, we’ll explore how to use Curl to test TCP connectivity on Linux.
Syntax
The basic syntax for testing TCP connectivity with curl is as follows :
curl telnet://<host>:<port>
host is the hostname or IP address of the server you want to connect to, and port is the port number.
Test port connection with curl
Here I’m trying to access a Linux server with ssh but it doesn’t work :
[root@Linux ~]# ssh 192.168.139.145 ssh: connect to host 192.168.139.145 port 22: Connection refused
So I need to check TCP connectivity to the server on port 22 with curl by running the following command :
[root@Linux ~]# curl telnet://192.168.139.145:22 curl: (7) Failed connect to 192.168.139.145:22; Connection refused
In my case there is no firewall between the two machine, so either the local firewall blocks incoming ssh connection or the ssh service is not started.
After checking, I found that the ssh service is not started :
[root@server ~]# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: inactive (dead) since Sun 2023-03-12 12:45:16 +01; 16min ago Docs: man:sshd(8) man:sshd_config(5) Process: 1076 ExecStart=/usr/sbin/sshd -D $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 1076 (code=exited, status=0/SUCCESS)
Now, After starting the service, here is the message I get when I test with curl :
[root@Linux ~]# curl telnet://192.168.139.164:22 SSH-2.0-OpenSSH_7.4
you can use -v option to get more informations about the connection :
[root@Linux ~]# curl -v telnet://192.168.139.164:22 * About to connect() to 192.168.139.164 port 22 (#0) * Trying 192.168.139.164... * Connected to 192.168.139.164 (192.168.139.164) port 22 (#0) SSH-2.0-OpenSSH_7.4
Want to discover other alternative tools to test connectivity ? click here.
nmap : les 12 commandes que vous devez connaître
Protocole du routage
Netstat : Les 14 commandes les plus utilisées
Samba : Installation et Configuration sur Ubuntu 22.04
Fail2Ban : How to protect Linux services