Skip to content

Instantly share code, notes, and snippets.

@philroche
Last active October 21, 2021 12:23
Show Gist options
  • Save philroche/546eee987105a5e0cf11e6718db918ab to your computer and use it in GitHub Desktop.
Save philroche/546eee987105a5e0cf11e6718db918ab to your computer and use it in GitHub Desktop.
Configure SSH for access across nodes - 2021 PTG directord hackfest
# Get SSH-config for nodes
vagrant ssh-config node0 > node0.ssh-config
vagrant ssh-config node1 > node1.ssh-config
# get teh SSH key path from the ssh-config
node0_ssh_key=$(ssh -F ./node0.ssh-config -G node0 | grep -m1 -oP "(?<=identityfile ).*")
echo ${node0_ssh_key}
node1_ssh_key=$(ssh -F ./node1.ssh-config -G node1 | grep -m1 -oP "(?<=identityfile ).*")
echo ${node1_ssh_key}
# Concatenate the ssh-configs
rm -rf ssh-config.local
rm -rf ssh-config.remote
cat node0.ssh-config > ssh-config.local
cat node1.ssh-config >> ssh-config.local
cat ssh-config.local
cp ssh-config.local ssh-config.remote
# Combine the authorized keys for both nodes
ssh -F ./ssh-config.local vagrant@node0 cat /home/vagrant/.ssh/authorized_keys > node0.authorized_keys
ssh -F ./ssh-config.local vagrant@node1 cat /home/vagrant/.ssh/authorized_keys > node1.authorized_keys
rm -rf authorized_keys.remote
cat node0.authorized_keys > authorized_keys.remote
cat node1.authorized_keys >> authorized_keys.remote
cat authorized_keys.remote
# Upload the combined authorized keys to the two nodes
vagrant upload authorized_keys.remote "/home/vagrant/.ssh/authorized_keys" node0
vagrant upload authorized_keys.remote "/home/vagrant/.ssh/authorized_keys" node1
# Upload one of the keys as /home/vagrant/.ssh/id_rsa so that custom config file is not required
vagrant upload ${node0_ssh_key} "/home/vagrant/.ssh/id_rsa" node0
vagrant upload ${node0_ssh_key} "/home/vagrant/.ssh/id_rsa" node1
ssh -F ./ssh-config.local vagrant@node0 chmod 600 /home/vagrant/.ssh/id_rsa
ssh -F ./ssh-config.local vagrant@node1 chmod 600 /home/vagrant/.ssh/id_rsa
# Replace the ssh key path to the local path on the nodes
sed -i "s:${node0_ssh_key}:/home/vagrant/.ssh/id_rsa:g" ssh-config.remote
sed -i "s:${node1_ssh_key}:/home/vagrant/.ssh/id_rsa:g" ssh-config.remote
# Upload the SSH config to the nodes
vagrant upload ./ssh-config.remote "/home/vagrant/.ssh/config" node0
vagrant upload ./ssh-config.remote "/home/vagrant/.ssh/config" node1
ssh -F ./ssh-config.local vagrant@node0 chmod 600 /home/vagrant/.ssh/config
ssh -F ./ssh-config.local vagrant@node1 chmod 600 /home/vagrant/.ssh/config
# SSHing to nodes from Host
ssh -F ./ssh-config.local node0
ssh -F ./ssh-config.local node1
# SSHing to nodes from nodes
ssh node0
ssh node1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment