Without seeing your curl --version
let me take a stab in the dark based on just running into this exact problem. Is your version of curl built without c-ares? The first line of curl --version
should tell you:
curl 7.71.0-DEV (x86_64-unknown-linux-gnu) libcurl/7.71.0-DEV c-ares/1.10.0
I recently ran into this same issue where DNS timeouts caused curl to exceed the specified timeouts. With verbose turned on it would show the DNS lookup DID timeout at 5 seconds, but then would wait until 20s to completely timeout and close. We root caused it to versions without c-ares (asynchronous DNS resolver library).
I built it from source taken from their github using these instructions but adding ./configure --enable-ares
Here you can see it built with c-ares c-ares/1.10.0
in the version info
$ /usr/local/bin/curl --version
curl 7.71.0-DEV (x86_64-unknown-linux-gnu) libcurl/7.71.0-DEV c-ares/1.10.0
Release-Date: [unreleased]
Protocols: dict file ftp gopher http imap pop3 rtsp smtp telnet tftp
Features: AsynchDNS IPv6 Largefile UnixSockets
and it behaves as expected
$ time /usr/local/bin/curl --max-time 5 google.com -v
* Resolving timed out after 5000 milliseconds
* Closing connection 0
curl: (28) Resolving timed out after 5000 milliseconds
real 0m5.002s
user 0m0.000s
sys 0m0.002s
And built without c-ares
/usr/local/bin/curl --version
curl 7.71.0-DEV (x86_64-unknown-linux-gnu) libcurl/7.71.0-DEV
Release-Date: [unreleased]
Protocols: dict file ftp gopher http imap pop3 rtsp smtp telnet tftp
Features: AsynchDNS IPv6 Largefile UnixSockets
Timeout doesn't work:
time /usr/local/bin/curl --max-time 5 google.com -v
* Resolving timed out after 5000 milliseconds
* Closing connection 0
curl: (28) Resolving timed out after 5000 milliseconds
real 0m20.023s
user 0m0.003s
sys 0m0.000s
If you're building curl from source or it's an option for you likely just need to enable that option.
Edit:
JamesTheAwesomeDude made an excellent point that chasing down where this cURL came from is worth figuring out. In my case it was a fresh EC2 instance running Amazon Linux 2.
$ cat /etc/*-release | grep PRETTY_NAME
PRETTY_NAME="Amazon Linux 2"