Created
March 23, 2020 16:53
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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