Bytes, Pipelines & Queries #1 – How to SSH into a Remote VM from WSL
January 31, 2025 • 3 min read
Welcome to the this series of blog posts I am calling “Bytes, Pipelines & Queries” – a chain of mini blog posts where I capture my learnings, interesting tips and challenges from my journey in the Data Engineering Zoomcamp led by Alexey Grigorev of DataTalks.Club In this first post, I cover how to SSH into a remote VM from WSL.
Secure Shell (SSH) is an essential tool for engineers who work with remote servers. Whether you're managing cloud infrastructure, deploying applications, or debugging issues, understanding SSH is a must-have skill. In this guide, I'll break down the process of SSHing into a remote virtual machine (VM) and throw in some power tips along the way.
What is SSH?
SSH (Secure Shell) is a protocol that lets you securely connect to remote machines over an encrypted connection. It's the go-to method for accessing cloud instances, debugging servers, and transferring files securely.
Think of it like a secure doorway into your VM, allowing you to run commands as if you were sitting in front of the machine.
Prerequisites
Before diving in, ensure you have:
- ✅ A remote VM (it could be on Google Cloud(my preference), AWS, Azure, or any other)
- ✅ Windows Subsystem for Linux (WSL) installed and configured. Read here
- ✅ An SSH client (WSL comes with one by default)
- ✅ The private key associated with your VM (e.g.,
id_rsa
ormy-key.pem
)
Generating an SSH Key Pair
If you don’t have a private key yet, you can generate one using:
ssh-keygen -t rsa -b 4096 -f ~/.ssh/[KEY_FILENAME] -C [USERNAME]
- The
-t rsa -b 4096
flag generates a strong RSA key with 4096 bits. - The
-f
flag specifies the filename for the key pair. - The
-C
option adds a comment (typically your email or username) for identification. - Your private key will be saved in
~/.ssh/[KEY_FILENAME]
and the public key in~/.ssh/[KEY_FILENAME].pub
.
Upload the public key ([KEY_FILENAME].pub
) to your VM under ~/.ssh/authorized_keys
to enable SSH access. This allows the VM to recognize your public key and authenticate you. In Google Cloud, this is done under Compute Engine → VM Instances → Metadata → SSH Keys.
Step 1: Find Your VM's Public IP
In GCP, go to Compute Engine → VM Instances and grab the External IP of your instance.
# Example
34.123.45.67
Step 2: Set Up an SSH Config File (Best Practice)
Instead of typing long SSH commands every time, you can configure an alias in your SSH config file.
Edit the SSH Config File
You can edit the SSH config file using your preferred text editor. You can open it in VS Code or any code editor or enter the command below to do it with nano
:
nano ~/.ssh/config
Then, add the following configuration:
Host [Your VM Alias]
HostName [Your VM's Public IP]
User [Your Username]
IdentityFile ~/.ssh/my-key.pem
Now, you can connect by simply executing a command.
💡 Why use an SSH config file?
- Saves time by avoiding long commands
- Manages multiple VMs easily
Step 3: Connect to the VM Using SSH
With the SSH config in place, simply run:
ssh [Your VM Alias]
This will connect you to your VM.
Step 4: Connecting to the Remote VM in VSCode
Visual Studio Code makes SSH access even easier. If you haven't installed the Remote - SSH extension, do so from the Extensions Marketplace. With VS Code, you can open folders and edit files directly from the remote VM. Here's how:
Steps to Connect:
- Press
Ctrl + Shift + P
and select Remote-SSH: Connect to Host. - Enter 'ssh Your VM Alias' (or 'ssh Your Username@Your VM's Public IP').
- The first time you attempt this, Windows will generate a new config file on the Windows path
C:\Users\<username>\.ssh\config
. Find it and replace it with all the content from the .ssh directory in WSL: '/home/username/.ssh' or '~/.ssh'. Using the WSL path kept giving me an 'Access Denied' error because the system kept mixing up the paths. - Once connected, you can open folders and edit files directly from VSCode.
Step 5: Handling Common SSH Issues
🚧 Permission Denied (Public Key)
- Ensure you’re using the right config file or key file.
- Check if the VM's firewall allows SSH (port 22)
🚧 Connection Timed Out
- Confirm your VM is running and has a public IP
- Check firewall rules (
gcloud compute firewall-rules list
on GCP)
Wrapping Up
Mastering SSH is a must-have skill for engineers working with remote machines. With these tips, you'll be confidently connecting to VMs across different cloud providers like a pro 👌.
🚀 Happy SSHing!