How to troubleshoot network applications with curl

There are many online tools for testing the services offered by a server but those tests only indicate the view from the online tool. Sometimes you need to test connectivity from your local computer. This article describes how to use curl to troubleshoot network applications.

About curl

Curl is a command line tool to transfer data from or to a server

Curl is available at the Linux command line, in the Macintosh Terminal and in the Windows Subsystem for Linux. Later versions of Windows 10 include curl in the command prompt. Curl can also be installed from https://curl.haxx.se.

Don't want to install software? Try our articles on network troubleshooting using telnet or using PowerShell and tnc. Just about every computer will have one of these three programs installed.

Using curl to troubleshoot

To use curl to test basic network connectivity, you need to know several things:

  • The remote server name or IP address.
  • The protocol for the service to be tested (HTTP, FTP, SMTP, etc.)
  • The port number for the network application you want to test.

To open a connection to a remote server, open a terminal window on your computer, and then type curl protocol://IP/host:port, where protocol is the communication protocol to be used IP/host represents the IP address or hostname of the server, and port represents the TCP port number. Port is optional when the standard port for a given protocol is used. For example, to connect to http://example.com on the standard port for http, type the following command:

curl http://example.com
For a complete list of assigned TCP port numbers and their associated protocols, please visit http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers.

When you try to establish a connection to a remote server, one of two things happens:

  • The server accepts the connection. If this happens, curl may display some text from the server.
  • The server rejects the connection. If this happens, you receive a message such as Connection refused or Connect failed.

The following sections demonstrate how to do basic troubleshooting with curl.

Troubleshooting web servers

Web server testing is probably the most common scenario for network troubleshooting. With curl, you can open a connection to a remote server on port 80 and get a response. Text in red represents commands typed by the user:

$ curl http://example.com -I
HTTP/1.1 200 OK
Date: Thu, 14 Feb 2019 20:11:29 GMT
Server: Apache
X-Powered-By: PHP/5.5.38
Content-Type: text/html

curl returns the entire content of the web page by default so in this example the -I option is added to return only the header. The HTTP response confirms that the server is accepting connections and responding to requests.

Troubleshooting mail (SMTP) servers

Use curl to try and connect via SMTP protocol

The following text shows a sample exchange between curl and a remote mail server. Text in red represents commands typed by the user:

$ curl smtp://example.com
214-Commands supported:
214 AUTH STARTTLS HELO EHLO MAIL RCPT DATA BDAT NOOP QUIT RSET HELP

The SMTP responses show the server is running and accepting requests.

Some ISPs block port the standard SMTP port (25) to help reduce spam on their networks.If you are testing connectivity to an A2 Hosting mail server, you can also use port 2525 or 587.

For example, to try and connect to SMTP on port 2525, add the optional port number at the end of the command. The following text shows a sample exchange between curl and a remote mail server using port 2525. Text in red represents commands typed by the user:

$ curl smtp://example.com:2525
214-Commands supported:
214 AUTH STARTTLS HELO EHLO MAIL RCPT DATA BDAT NOOP QUIT RSET HELP

The SMTP responses show the server is running and accepting requests.

Troubleshooting FTP

To test an FTP server, use curl to connect via ftp protocol or to port 21.

The following text shows a sample exchange between curl and a remote FTP server. Text in red represents commands typed by the user:

$ curl ftp://example.com
curl: (67) Access denied: 530

Because a valid user name was not supplied, curl returns Access denied.

More detail is available by using the FTP port (21) without the FTP protocol.

$ curl example.com:21
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 18 of 50 allowed.
220-Local time is now 12:46. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.

Either response indicat the FTP server is active and running.

Troubleshooting SSH

SSH uses encrypted connections. However, you can still use curl to verify that the service is running on a server.

The following text shows a sample exchange between curl and a remote SSH server. In this case we use the port number without the protocol. The standard port for ssh is 22 but port 7822 is used by A2 Hosting managed servers. Text in red represents commands typed by the user:

$ curl example.com:7822
SSH-2.0-OpenSSH_5.3

The server returns a response showing the current OpenSSH version indicating the SSH server is active and running.

More Information

curl is a very versatile tool. In addition to the basic connectivity checks shown in this document, it can:

  • send emails
  • upload and download files
  • post and retrieve information from web servers

Download and read about curl at the curl website.

Read the curl book online.

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.