How-to

curl Certificate Errors: -k and Better Options

curl -k makes TLS errors go away by switching off verification. That is fine for a quick test on your own box and wrong almost everywhere else. Here are the precise alternatives.

What does curl -k do?

-k, long form --insecure, tells curl to continue even when the server's certificate cannot be verified. Traffic is still encrypted, but curl no longer checks that the certificate was issued by a trusted authority, that it is valid today, or that it matches the host name. Anyone able to intercept the connection could present their own certificate and read everything. Use it for one-off checks against a machine you control, never in scripts, cron jobs or application code.

curl -k https://203.0.113.25/health

What do common certificate errors mean?

ErrorCauseBetter fix
self-signed certificateNo authority signed itTrust that certificate with --cacert, or issue a real one
unable to get local issuer certificateChain incomplete or CA unknownServe the full chain on the server
certificate has expiredPast its end date, or clock wrongRenew it; check timedatectl
no alternative certificate subject name matchesName differs from the URL hostUse the right name, or --resolve to keep it

How do I test a new server by IP without -k?

Keep the real host name in the URL and tell curl which address to use. Certificate checks and virtual host selection then work exactly as they will after the DNS change:

curl --resolve shop.example.net:443:203.0.113.25 -sSI https://shop.example.net/

This is the right way to check a migration target before switching DNS. Connecting to a bare IP address almost always fails verification, because certificates are issued for names.

How do I trust a private CA or self-signed certificate?

Point curl at the certificate that should be trusted:

curl --cacert /etc/ssl/internal/ca.pem https://grafana.internal.example/
export CURL_CA_BUNDLE=/etc/ssl/internal/ca.pem

For a whole machine, add the CA to the system store so every tool trusts it: copy it to /usr/local/share/ca-certificates/internal-ca.crt and run update-ca-certificates on Debian and Ubuntu, or copy it to /etc/pki/ca-trust/source/anchors/ and run update-ca-trust on Enterprise Linux.

Can I pin a server's key instead?

Yes. Pinning makes curl accept only a certificate with a specific public key, which is stricter than normal verification:

openssl s_client -connect api.example.net:443 -servername api.example.net </dev/null 2>/dev/null \
  | openssl x509 -pubkey -noout | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | base64
curl --pinnedpubkey 'sha256//Gx3p...base64...=' https://api.example.net/

It suits machine-to-machine calls between your own servers, for example a service in Singapore calling one in Amsterdam. Plan for rotation: when the key changes, the pin must change too.

How do I fix an incomplete chain on my own server?

Browsers often hide this problem by fetching missing intermediates; curl and most APIs do not. Check what the server sends:

openssl s_client -connect shop.example.net:443 -servername shop.example.net -showcerts </dev/null | grep -E '^ ?[0-9] s:|i:'

If only one certificate appears, configure the web server with the full chain file. With certbot that is fullchain.pem, not cert.pem, in the nginx ssl_certificate line.

Is there an environment where -k is fine?

Local testing of a service on the same machine, such as curl -k https://127.0.0.1:8443/ against an app with a temporary certificate, is a reasonable use. So is a first look at a box you just installed. Free certificates from Let's Encrypt take a minute to set up with certbot, so the time to stop using -k comes quickly. Other request details, such as headers, are in Headers with curl.

How do I see a server's certificate details?

Ask curl to show the handshake, or ask OpenSSL directly. curl -vI https://shop.example.net/ 2>&1 | grep -E 'subject:|issuer:|expire date' prints who the certificate is for, who signed it and when it ends. A cron job running that check across your sites, alerting when expiry is under 14 days away, catches renewal failures before visitors do.

Keep Reading

Headers with curl

Show response headers, send or remove request headers, pick single header values and test caching, compression and virtual hosts with curl.

Open

The curl command

A practical tour of curl for server admins: fetching, saving, timing, debugging, retrying, resolving and the flags that make scripts reliable.

Open

SSH vs SSL/TLS

Both encrypt traffic, but SSH runs your admin sessions while TLS protects websites and APIs; here is where each fits.

Open

Follow redirects with curl

Make curl follow 301, 302, 307 and 308 redirects with -L, limit hops, keep POST bodies, see the final URL and debug redirect loops.

Open

FAQ

Quick Answers

It skips certificate verification, so curl accepts any certificate. The connection is encrypted but not authenticated.

Only for quick tests against your own machine. Never use it in scripts or production code.

Pass it with --cacert, or add it to the system trust store.

Use --resolve with the host name, port and new IP so the certificate name still matches.

Often the server sends an incomplete chain. Configure the full chain file, such as fullchain.pem.