SSH Connection: Two Local Machines with One Virtual Machine
In this guide, we check how to use the same SSH key pair from two different local machines to access the same Virtual Machine (VM). You’ll need to follow these steps on both local machines and the VM, assuming you’re using OpenSSH as your SSH client and server.
The steps are for general use, but it applies to ThreeFold and the Dashboard.
1. Generate or Use an Existing SSH Key Pair
Option A: If You Already Have a Key Pair
If you already have a private key file (id_rsa
) and its corresponding public key file (id_rsa.pub
), proceed to the next step.
Option B: Generating a New SSH Key Pair if Necessary
If not, you can generate one using OpenSSH with:
ssh-keygen -t rsa
This command generates a new key pair in your ~/.ssh/
directory. It’s recommended to use -t rsa
for compatibility.
2. Copy the Public Key
Copy the contents of id_rsa.pub
(not the private key, just the public key) to the VM’s authorized_keys file. This can be done manually by logging into the VM or remotely using SSH with a password and then copying the key directly via:
ssh user@vm_ip "mkdir -p ~/.ssh; echo 'your_public_key' >> ~/.ssh/authorized_keys"
Replace 'your_public_key'
with the actual content of your id_rsa.pub
, and make sure to include the VM’s SSH path correctly.
Note: The public key is automatically copied into the VM running on the TFGrid e.g. when you deploy with the Dashboard.
3. Restrict Access (Optional)
If you have multiple local machines accessing the same VM, consider adding restrictions in the authorized_keys file on the VM to only allow access from specific IP addresses or hostnames using the following format:
command="echo 'Your command output'; exit 0" ssh-rsa your_public_key user@host1
Replace 'your_public_key'
with your actual public key, and update user@host1
to match each of your local machine’s usernames and hostnames.
4. Configure SSH Agent
On both local machines:
-
Ensure that the SSH agent is running (
ssh-agent -s
). -
Add the private key to the SSH agent for use by all users on the system with
ssh-add /path/to/your/id_rsa
.
This allows you to access the VM without typing your password each time, as long as the SSH client can connect to the agent process.
5. Connect
On both local machines:
Use the following command to connect to the VM using the shared key pair:
ssh -i /path/to/your/id_rsa user@vm_ip
Replace /path/to/your/id_rsa
with the path where you store your private key (id_rsa
).
Important Considerations
-
Key Security: Ensure that both local machines’ SSH agent processes are secure, especially if multiple users have access to them. The security of the shared key depends on these settings.
-
SSH Agent Persistence: If one machine’s SSH agent goes away (e.g., due to logout or restart), you might need to reconnect it to use your keys.
Using a single SSH key pair across different machines for accessing the same VM can simplify management, especially when multiple users and machines need to access it. However, securing this key and ensuring that its storage complies with security policies is crucial.