Skip to content

Instantly share code, notes, and snippets.

@olooney
Created March 23, 2020 16:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save olooney/de31ba41183979287a4b6e7cfc00b0f1 to your computer and use it in GitHub Desktop.
Shell script to update the .ssh/known_hosts file for all users on a given machine when the host key of a target machine has changed
TARGET_HOST=prometheus
# for each user with a home directory
for USER in `ls /home`; do
# if the user has a known_hosts files
echo "checking /home/$USER/.ssh/known_hosts..."
if [ -f /home/$USER/.ssh/known_hosts ]; then
# remove the outdated key
ssh-keygen -R $TARGET_HOST -f /home/$USER/.ssh/known_hosts
# append the new host key
ssh-keyscan -H $TARGET_HOST | grep ssh-rsa >> /home/$USER/.ssh/known_hosts
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment