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: StrictHostKeyChecking accept-new trusts a new server once and refuses it when its key changes

Linux
~/tips/tip-ssh-stricthostkeychecking-accept-new

ssh -o StrictHostKeyChecking=accept-new [email protected] uptime

Lets ssh add the key of a new server without asking, but still refuses a server whose key changed.

Scripts that run ssh against fresh machines often end up with StrictHostKeyChecking no, because the yes/no question blocks them. There is a safer middle road.

The ssh_config man page lists the values. yes never adds host keys automatically and refuses hosts whose key changed. ask, the default, adds a new key only after you confirm it. no or off adds new keys by itself and lets connections to changed hosts go on, subject to some restrictions. accept-new adds new host keys automatically but does not permit connections to hosts with changed keys.

ssh -o StrictHostKeyChecking=accept-new [email protected] uptime

The first connection to a new host works without a question, and the key goes into known_hosts. Later connections are checked against it, and a changed key stops the connection. no gives up exactly that check.

For a group of hosts in your config:

Host lab-*
    StrictHostKeyChecking accept-new

The weak spot is the first connection. accept-new trusts whatever answers the first time, so someone in the middle at that moment would be accepted for good. On my own LAN I find that risk small. For a machine across the internet I would check the fingerprint once through another route, such as the provider’s console, and stay on ask.

When a key changes for a good reason, a reinstall for example, ssh refuses until you remove the old entry with ssh-keygen -R.

Further reading: ssh_config(5)

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link