Home Linux Commandline Tips How to Set Connection Timeout in Curl Command

How to Set Connection Timeout in Curl Command

One main activity in the Linux command-line environment is data transfer. To comfortably and flexibly move data to/from different servers, we need the help of a reputable data transfer terminal-based tool.

Curl command makes it easy to transfer data between servers through the aid of supported protocols like HTTPS, HTTP, IMAPS, IMAP, LDAPS, LDAP, POP3S, POP3, SFTP, SCP, SMTPS, SMTP, TFTP, and TELNET.

Curl command’s functional design does not accommodate user interaction but offers more than enough reputable features like Metalink, cookies, HTTP post, user authentication, file transfer resume, SSL connections, FTP upload, and proxy support.

This article will address using the Curl command with a set timeout.

Curl Installation in Linux

To use the curl tool to transfer a URL from one server to another, first make sure that it is installed on your Linux system. Select your preferred installation command from the following list depending on the Linux operating system distribution you are using.

$ sudo apt install curl         [On Debian, Ubuntu and Mint]
$ sudo yum install curl         [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a net-misc/curl  [On Gentoo Linux]
$ sudo pacman -S curl           [On Arch Linux]
$ sudo zypper install curl      [On OpenSUSE]    

Curl Command Timeout Parameters

In reference to the Curl command usage syntax, the timeout parameters we will be addressing fall under [options]. When it comes to dealing with Curl’s timeout parameters, we have to look at the following two options:

Option 1: Curl’s Timeout Parameter

The --connect-timeout parameter points to the maximum connection duration (in seconds) between servers and/or machines. The effectiveness of the Curl command parameter is only effective during the connection phase or when two servers or machines are attempting to make a connection.

Once a connection is made, this parameter value becomes ineffective. This parameter can take a decimal point value.

$ curl -o file.md --connect-timeout 3 https://github.com/zethra/servy/blob/master/README.md
Set Curl Timeout in Linux
Set Curl Timeout in Linux

The Curl command option -o renames and saves the downloaded file (README.md) as a file.md.

Option 2: Curl’s Max Timeout Parameter

The Curl timeout parameter specifies (in seconds) the maximum duration needed for a file/data transfer operation. It is an effective parameter when dealing with issues like links going down or slow networks.

This parameter can also take a decimal point value.

$ curl -o file.md --max-time 0.5 https://github.com/zethra/servy/blob/master/README.md
Set Curl Max Timeout in Linux
Set Curl Max Timeout in Linux

As per the above screen capture, we set the --max-time value to 0.5 seconds hence the file download could not be complete and therefore resulted in an operation timed out error.

Let us increase the value to 5 seconds and see what happens.

$ curl -o file.md --max-time 5 https://github.com/zethra/servy/blob/master/README.md 
Increase Curl Max Timeout in Linux
Increase Curl Max Timeout in Linux

As expected, the file download operation was completed successfully. We have successfully understood how to set a timeout in Curl.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.