SSHing into your VM Instance

Out of the box, local virtual machines make great, isolated testing platforms for software systems, but working just through the serial console emulator window can make development pretty cumbersome. The GUI can be clunky, sluggish, and unfamiliar, and can seem pretty excessive if all we really need out of our virtual machine is a terminal window.

This guide will walk you through how to set up your Linux VM as a local server that you can login to using ssh (Secure SHell).

Note that this guide will refer to your virtual machine (VM) as a guest machine, and your computer (running the VM) as its host machine.

Set up OpenSSH on Debian

You need to first update the apt package repository cache by running the following command:

# apt update

Note: On Debian, SSH server comes as openssh-server package. In order to install OpenSSH:

# apt install openssh-server

Press y and then press Enter to continue.

The default behavior of OpenSSH server in Debian is that it will start automatically as soon as it is installed. To check whether OpenSSH server is running:

# systemctl status ssh

In case OpenSSH server is not running, you can run the following command to start OpenSSH server:

# systemctl start ssh

SSH Into Your Local VM

By default, VMware does not assign static IP addresses to local VMs. This means that VMware can change your VM’s IP address. It is possible to stop VMware from doing that (and you may do so if you wish), but here are some easier alternatives.

If your operating system version is somewhat recent, then you can delegate the work of finding your VM’s IP address to your local machine instead of doing it yourself. This is possible because of mDNS resolution (aka zeroconf networking):

[host]$ ssh <username>@<your-vm-name>.local

where <your-vm-name> refers to the name you gave your VM when you were going through the Debian setup.

The hostname <your-vm-name>.local will be resolved to your VM’s current IP address for you. This makes setting up an SSH config file very simple, and requires the least amount of work in the long-run.

SSHing using VMware GUI

NOTE: This option is currently not supported if you are on an M1/M2 Mac; please use SSHing with mDNS Resolution instead.

This is the simplest method because it requires zero setup and you won’t have to find the local VM IP address first. Here are the official VMware guides on how to use the SSH GUI:

If you select “Remember password”, the window prompting you for your username/password won’t pop up anymore and clicking the “Connect to SSH” button will open a terminal directly.

It is possible to retrieve the current IP address of your local VM manually. Here are two options:

  1. Select your VM in the VMware VM library window. If your VM is running, its IP address will be shown alongside the other system properties.

  2. Run ip addr in the terminal application inside your VM.

Once you obtain the current IP address, you may login to your guest machine using:

[host]$ ssh <username>@<ip-addr>

NOTE: Since VMware doesn’t assign static IP addresses by default, the IP address you find may change across sessions, which will make the SSH command fail. You’ll have to requery the IP address if that happens and replace the old IP address in your SSH command.

Now we don’t have to wait for your VM’s GUI to render, and interact with it via a text-only command line terminal interface.

Setting Up SSH Keys

Are you tired of entering your password to log into your VM? If so, you’ll want to set up your SSH keys so that you can authenticate without a password. This is highly recommended.

First, if you haven’t already, generate an Ed25519 key pair on your host machine and add it to your ssh-agent following this excellent guide by GitHub. And while you’re at it, add your key to your GitHub account. This is necessary for authenticating with GitHub, since it no longer supports authenticating over HTTPS.

Now that you have your key, you can set up your VM to authenticate the same way. Assuming that you named your Ed25519 key pair id_ed25519 (the default), run the following command:

[host]$ ssh-copy-id <username>@<hostname>

Where <hostname> is one of the following:

This copies the public Ed25519 key on your host machine to the ~/.ssh/authorized_keys file on your guest machine, which sshd will use to authenticate the SSH client on your host machine. For more details, see this Digital Ocean guide.

At this point, you’ll be able to SSH into your VM without being prompted for your password.

If you’re interested in GitHub SSH authentication, you’ll want to forward your host’s SSH credentials into your VM. This enables you to authenticate with GitHub without entering your password or copying your private key.

First, let’s create our SSH config file if we haven’t already:

[host]$ mkdir -p ~/.ssh
[host]$ touch ~/.ssh/config

Here are two configuration options that you can put in ~/.ssh/config using your favorite text editor:

Specific Rule (Recommended)

Host osvm
    HostName <hostname>
    User <username>
    ForwardAgent yes
    AddKeysToAgent yes

Where <hostname> is one of the following:

Blanket Rule (Not recommended)

Host *
    ForwardAgent yes
    AddKeysToAgent yes

This says that for any host you SSH into successfully, forward your credentials. Note that if you decide to do this, you should be careful to only SSH into trusted machines. We avoid having to deal with specifying the target machine’s information by applying this blanket rule.

After adding one of the two above configurations, you’ll then be able to SSH into your VM with the following shorter command:

[host]$ ssh osvm

Alternatively, if you don’t want to deal with SSH configurations, you may simply generate a new SSH key inside your VM. Be sure to add that key to your GitHub profile.

You can test your GitHub connection with the following command:

[guest]$ ssh -T

If you see a greeting with your username, you’re all set! Happy coding!

Using Visual Studio Code (Optional)

If you’re a fan of Visual Studio Code (VS Code), you can work on your VM directly from a VS Code window on your host machine, allowing you to take advantage of all of VSCode’s features (such as quickly opening any file without navigating directories using Ctrl + P, which becomes really useful when dealing with Linux kernel source code) in a local-quality development experience. Take a look at our VSCode guide for more information.