Setting up a new environment of Linux boxes either Physical servers, VMs or Containers, one thing to consider is how you will login or manage those devices.
In Linux world, SSH is the most common way to manage headless servers or VMs.
SSH keys must be added to ~/.ssh/known_hosts to preload to the host keys before connecting to the new servers.
There quite a few ways to do it, via bash script, or simply logging in and copying manually the key.but quite tedious and not the practical way if there are hundreds of more servers.
ssh-keyscan server1 server2 server3 >> ~/.ssh/known_hosts
This will suppress the question whether you want to add the keys or not, and type yes to continue.
ssh-keyscan won't require any manual intervention once the connectivity is established it will just add all the keys available on the remote servers to known_hosts.
Yes rather than typing ssh-keyscan server1 server2 it would be better to have a list on a text file and use while loop to read the file and just run it.
Example:
while IFS= read -r server; do
[[ -z "$server" ]] && continue
echo "Connecting to $server..."
ssh-keyscan $server >> ~/.ssh/known_hosts
done < serverlist.txt
Yes, the serverlist.txt.should be available where the command will be run, and the script can still be improved to include logging whether able to connect to the remote server or not.
That's it. Till next time. Enjoy scripting and keep exploring.
Be patient in times of trials and tribulations, for the reward is great to those who pass through it.
Trust in God the giver of all graces.
Comments
Post a Comment