The approach to take depends on the specific scenario. It would be nicest if there already is a key that exists in the user’s
authorized_keys on all of the servers that can be used to authenticate when adding the new key to the remote hosts, because otherwise you have to type in your password for each host.
The simplest way to automate copying a ssh key to a remote machine is using
ssh-copy-id, and if you have servers called
remote100 it could be done using something like the following:
for server in $(seq -f 'remote%.0f' 1 100); do
ssh-copy-id -i ~/.ssh/id_rsa "$server"
Which would automatically take the public key for
id_rsa, ssh into all of the servers in sequence, and add the public key to
authorized_keys. If this is the only key you would have on the servers you will be prompted for your password to add them though, since the
ssh-copy-id program needs to actually ssh into the servers.
One way to automate it even further would be to use something like
expect to automate logging in with a password since OpenSSH’s ssh client doesn’t have a command-line option to specify the password, or alternatively by using a wrapper like