brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: curl --netrc reads passwords from ~/.netrc instead of the command line

Curl
~/tips/tip-curl-netrc-credentials

password secret

Lets curl take the login for a host from a locked-down .netrc file, so no password shows in the command.

Typing a password after curl -u bothers me, because it ends up in the shell history and, for a moment, in the process list. A .netrc file keeps it out of the command.

With -n (--netrc), curl looks in the .netrc file in your home directory for a login and password that match the host you call. The file format is plain text, one entry per host:

machine host.example.com
login myself
password secret

Then the request does not need any credentials on the command line:

chmod 600 ~/.netrc
curl -n https://host.example.com/api/status

The chmod matters. The manual says curl does not complain when the file has the wrong permissions, but it should be neither world-readable nor group-readable, so it is up to you to lock it down. A default entry works as a catch-all and has to come after all machine entries, and there can be only one.

The manual also mentions why I bother. For -u it says curl hides the argument from process listings where it can, but that is not enough to protect credentials, because they are visible for a moment before being cleared. It recommends reading sensitive data from a file instead.

Related options: --netrc-file takes an explicit path, and --netrc-optional makes the file optional instead of mandatory. If you set the NETRC environment variable, curl uses that file name, but the manual says that is only supported since curl 8.16.0. And -u overrides netrc if you give both.

The caveat is that the file holds the password in clear text. It is better than a command line, but I would still not put it somewhere a backup job copies around, and the manual notes that the credentials apply to a host regardless of protocol and port number.

Further reading: curl man page: –netrc, –netrc-file and –user

More Curl tips curl --trace-ascii – https://example.com/ Retry-After: 120 curl -fsS --fail-early \ url = "https://example.com/data/latest.json" curl -4 -s -o /dev/null -w '%{remote_ip} %{http_code}\n' https://example.com/ All Curl tips (71) All topics

Link