I've written this small utility script:
for h in $SERVER_LIST; do ssh $h "uptime"; done
When a new server is added to $SERVER_LIST
, the script is stopped with:
The authenticity of host 'blah.blah.blah (10.10.10.10)' can't be established.
RSA key fingerprint is a4:d9:a4:d9:a4:d9a4:d9:a4:d9a4:d9a4:d9a4:d9a4:d9a4:d9.
Are you sure you want to continue connecting (yes/no)?
I've tried yes
:
for h in $SERVER_LIST; do yes | ssh $h "uptime"; done
with no luck.
Is there a way to parametrize ssh
to automatically accept any new key?
Use the StrictHostKeyChecking option, for example:
This option can also be added to ~/.ssh/config, e.g.:
Note that when the host keys have changed, you'll get a warning, even with this option:
If your hosts are not often reinstalled, you could make this less secure (but more convenient for often-changing host keys) with the
-oUserKnownHostsFile=/dev/null
option. This discards all received host keys so it'll never generate the warning.With Ubuntu 18.04, there's a new possibility :
StrictHostKeyChecking=accept-new
since OpenSSH>=7.6.From
man ssh_config
:You can use the following command to add the fingerprint for a server to your known_hosts
NOTE: Replace < ip-address > and < hostname > with the IP and dns name of the server you want to add.
The only issue with this is that you will end up with some servers in your known_hosts twice. It's not really a big deal, just mentioning. To ensure there are no duplicates, you could remove all the servers first by running the following first:
So you could run:
One thing to keep in mind when removing just to re-add, you are essentially removing the security of verifying the fingerprint. So you would definitely not want to run this script before each execution of your utility script.
I'm a bit late with this response, but the sensible way would be to do a ssh-keyscan on the new machine before you run the uptime gathering.
Disabling the sanity check for convenience sake sounds like a bad plan, even if you think you're totally in control of the environment.
Add this entry into
~/.ssh/config
fileIf it complains about access permission for
~/.ssh/config
, then tryIn order to add a list of servers automatically we can do below:
Add servers IP in file servers-list
The IPs should be added in below format.
Output of
cat servers-list
Change above IPs by replacing yours.
Below command will add all servers from the list.
I tried the approaches suggested in this thread. Best fit to my needs is summarized below:
ssh -o StrictHostKeyChecking=accept-new -o ConnectTimeout=10 -i <filepath to .pem RSA key> <user>@<ip>
Though it's too late to answer this, wanted to share my contribution.
I just made a quick bash script to ease my life.
Basically, whenever you ssh into a machine, it removes the old host key and adds the new key. Add that as an alias to ssh, then you no longer need to worry about ssh keys not matching. The script will rotate them on every login.
Example Usage
bash script.sh [email protected]
Code Explanation
The code takes the hostname@ip as argument and splits it into hostname and ip using the
cut
command with@
as delimiter.Then it removes the ip address from the known_hosts file using the ssh-keygen command.
Then it runs the ssh command with one additional option
-o StrictHostKeyChecking=accept-new
. This allows it to automatically accept the new key.I have also added a try-catch sequence to it, so that in case of errors, it falls back to the default behaviour of ssh command.