Connect Your VM Instance to Your Local Computer

Aman Jaglan
4 min readJul 2, 2024

--

Connecting a Virtual Machine (VM) instance to your local computer is a critical skill for developers and system administrators. It allows you to manage remote servers and leverage cloud resources efficiently. In this guide, we’ll walk you through the step-by-step process to establish a secure SSH connection between your local machine and a VM instance, addressing potential issues you might encounter along the way.

1. Generate an SSH Key Pair (if you don’t already have one)

To start, you need an SSH key pair. If you don’t have one, generate it on your local machine.

On macOS/Linux:

ssh-keygen -t rsa -b 4096 -C "your_name or Email"

This command creates a pair of keys (private and public) in the ~/.ssh directory. The public key is stored in id_rsa.pub, and the private key is in id_rsa.

2. Copy Your Public Key

Copy the content of your public key to the clipboard.

On macOS:

pbcopy < ~/.ssh/id_rsa.pub

On Linux:

xclip -sel clip < ~/.ssh/id_rsa.pub

Alternatively, manually copy the key:

cat ~/.ssh/id_rsa.pub

3. Access the VM Instance via the Cloud Provider’s Console

Each cloud provider (Google Cloud Platform, AWS, Azure) offers a web-based console to manage your VM instances.

For Google Cloud Platform (GCP):

  1. Go to the Google Cloud Console.
  2. Navigate to the “VM instances” page.
  3. Click on the instance you want to manage.
  4. Click on the “SSH” button to open a web-based terminal.

For Amazon Web Services (AWS):

  1. Go to the AWS Management Console.
  2. Navigate to the “EC2 Dashboard”.
  3. Select your instance.
  4. Use the “Connect” button to access the instance via the EC2 Instance Connect feature.

For Microsoft Azure:

  1. Go to the Azure Portal.
  2. Navigate to the “Virtual machines” page.
  3. Select your VM.
  4. Use the “Serial console” or “Bastion” feature to access the instance.

4. Create the .ssh Directory and authorized_keys File (if not already present)

Once you have access to the VM, ensure the .ssh directory exists and has the correct permissions.

sudo mkdir -p /home/ubuntu/.ssh
sudo chmod 700 /home/ubuntu/.ssh

5. Add Your Public Key to authorized_keys

Edit the authorized_keys file to include your public key.

sudo nano /home/ubuntu/.ssh/authorized_keys

Paste your public key into the file, save (Ctrl+O, Enter), and exit (Ctrl+X).

6. Set Correct Permissions

Ensure the permissions are correctly set to secure the SSH directory and keys.

sudo chmod 600 /home/ubuntu/.ssh/authorized_keys
sudo chown -R ubuntu:ubuntu /home/ubuntu/.ssh

7. Connect from Your Local Machine

Now, you should be able to connect to your VM instance from your local machine using your private key.

ssh -i ~/Desktop/id_rsa ubuntu@<your-vm-ip-address>

Troubleshooting Common Issues

Despite following the steps correctly, you might face some issues while connecting. Here are potential problems and their solutions:

Permission Denied (publickey)

  • Cause: The SSH key is not properly configured or added.
  • Solution: Ensure the public key in authorized_keys matches the private key on your local machine. Verify file permissions:
sudo chmod 700 /home/ubuntu/.ssh 
sudo chmod 600 /home/ubuntu/.ssh/authorized_keys
sudo chown -R ubuntu:ubuntu /home/ubuntu/.ssh

No Such File or Directory

  • Cause: The .ssh directory or authorized_keys file does not exist.
  • Solution: Create the directory and file, then add your public key:
sudo mkdir -p /home/ubuntu/.ssh 
sudo nano /home/ubuntu/.ssh/authorized_keys

Incorrect Username

  • Cause: Using the wrong username for the SSH connection.
  • Solution: Ensure you are using the correct username provided by your cloud service (commonly ubuntu or ec2-user).

Successfully Connected

If you see this on your local terminal, it means you are ready to use your VM instance. Personally, I prefer PyCharm for working when connected to the cloud. In my next blog, I’ll show you how to set up PyCharm for connecting to the cloud. Stay tuned!

Conclusion

Connecting a VM instance to your local machine involves a series of precise steps, but once configured, it provides a secure and efficient way to manage remote servers. By following this guide and addressing common issues, you can ensure a seamless connection to your VM instance. Happy coding!

--

--

Aman Jaglan
Aman Jaglan

Written by Aman Jaglan

I talk about finance, product, technology, and data science.

No responses yet