-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_vm_to_vm_ssh.sh
More file actions
executable file
·52 lines (40 loc) · 1.49 KB
/
setup_vm_to_vm_ssh.sh
File metadata and controls
executable file
·52 lines (40 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
# Setup SSH keys between VMs for Unison to work
# Load configuration
if [ -f "config.env" ]; then
source config.env
else
echo "Error: config.env file not found. Please copy config.env.example to config.env and update with your values."
exit 1
fi
echo "=== Setting up VM-to-VM SSH keys for Unison ==="
echo ""
# Generate SSH key on VM1 and copy to VM2
echo "Setting up SSH key from VM1 to VM2..."
ssh -o StrictHostKeyChecking=no $VM_USER@$VM1_IP << EOF
# Generate SSH key on VM1 if it doesn't exist
if [ ! -f $SSH_KEY_PATH ]; then
ssh-keygen -t rsa -b 2048 -f $SSH_KEY_PATH -N ""
fi
# Show the public key
echo "VM1 public key:"
cat $SSH_KEY_PATH.pub
EOF
echo ""
echo "Now copying VM1's public key to VM2..."
# Get VM1's public key and add it to VM2's authorized_keys
VM1_PUBKEY=$(ssh -o StrictHostKeyChecking=no $VM_USER@$VM1_IP "cat $SSH_KEY_PATH.pub")
ssh -o StrictHostKeyChecking=no $VM_USER@$VM2_IP << EOF
# Create .ssh directory if it doesn't exist
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# Add VM1's public key to authorized_keys
echo "$VM1_PUBKEY" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
echo "VM1's public key added to VM2's authorized_keys"
EOF
echo ""
echo "Testing VM1 -> VM2 passwordless SSH..."
ssh -o StrictHostKeyChecking=no $VM_USER@$VM1_IP "ssh -o StrictHostKeyChecking=no $VM_USER@$VM2_IP 'echo VM1 can SSH to VM2 without password'"
echo ""
echo "=== VM-to-VM SSH setup complete! ==="