How to configure static IP address for a Hyper-V VM (Ubuntu 19.10 Quick Create) to avoid updating ssh config after each reboot?

5,544

Solution 1

I had the same problem, and found a step-by-step guide from Microsoft for setting up an internal switch with NAT, and connecting VMs to it.

It consists of the following steps in PowerShell

New-VMSwitch -SwitchName "SwitchName" -SwitchType Internal
Get-NetAdapter       // (note down ifIndex of the newly created switch as INDEX)
New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex <INDEX>
New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix 192.168.0.0/24

This uses 192.168.0.0/24 as the subnet for the virtual switch, where 192.168.0.1 is the IP of the host, which acts as a gateway.

Now, the VM can be connected to the new switch in Hyper-V Manager.

Note that unlike the Default Switch, there is no automatic network configuration via DHCP, so inside the VM, you will have to configure a static IP (e.g., 192.168.0.2) in the VM.

Solution 2

All you have to do now is

ssh <MULTPASS_VM_NAME>.mshome.net

and you are done!

Share:
5,544

Related videos on Youtube

Wlad
Author by

Wlad

I am a freelance software test automation engineer. With Robot Framework and Python I do test automation on steroids. To my customers I provide eXtreme Test Automation Agile Test Automation Continuous Testing &amp; Continuous Quality I help my customers to establish continuous integration to build CI/CD pipelines to gather software quality metrics and KPIs to make educated decisions to get Quality @ Speed in software development projects to keep their testing efforts up to speed with agile development AND to make test automation engineers out of testers/developers On a daily basis I work with local &amp; remote scrum teams modern source control repositories (Git, Github, BitBucket) DevOps tools (Docker, Jenkins, Ansible, Kubernetes, SonarQube/Cloud) Github Actions, Circle-CI, Azure Pipelines, etc. Atlassian Jira/Confluence YOU DEAL WITH CODE? DON'T TRUST. VERIFY.

Updated on September 18, 2022

Comments

  • Wlad
    Wlad over 1 year

    I'm using VS-Code remote development plugin to edit code on a hyper-v VM (which is a Ubuntu 19.10 - quick create) which is running on Windows 10 on my Laptop. The VM uses hyper-v's default switch for networking. The VS-Code remote development plugin allows to edit a ssh config file (C:\Users\username\ssh\config) which makes it easy to connect to the VM. Here' how my ssh config looks like:

    Host hypervubuntu
        HostName 172.18.10.76
        User my_UbuntuVM_username
    

    Settings of my hyper-v default switch (it looks like it's set to use a static IP but actually this settings change after each reboot of Windows): enter image description here

    The issue is that on every Windows reboot the IP address of the VM (and hyper-v's default switch) change requiring to edit ssh config to allow VS-Code to connect to the VM again. The change of IP also leads to further issues like the need to restart the VM and to confirm the authenticity of "new" host on every new ssh connect.

    I've tried setting a static IP in VM's network settings but this seems not to persist since they change back to "Automatic (DHCP)" after each reboot of the VM.

    As suggested in another post I've tried to create a new virtual switch w/ a static IP cause it's seems that Hyper-V's default switch is not meant to have a static IP. But this is what I could not get to work at all.

    Which parts have to be configured to allow VS-Code reconnect smoothly even after reboot of Windows or VM?

    DISCLAIMER: my network skill level == noob :\

    EDIT:

    Steps I needed to perform after great answer from @AlexKrauss to set static IP in my Hyper-V Ubuntu 19.10 Desktop VM:

    I. located and opened network config file

    cd /etc/netplan/
    sudo nano 01-network-manager-all.yaml
    

    II. replaced it's content as follows

    network:
      version: 2
      renderer: networkd  
      ethernets:
        eth0:
          addresses:
          - 192.168.0.2/24
          gateway4: 192.168.0.1
          nameservers:
            addresses:
            - 8.8.8.8
            - 8.8.4.4
          dhcp4: no
    

    III. applied & checked changes

    sudo netplan apply
    ifconfig -a
    

    IV. adjusted IP address in ssh config used by VSCode

    Host hypervubuntu
        HostName 192.168.0.2
        User my_UbuntuVM_username
    
  • Wlad
    Wlad almost 4 years
    Thanks a lot! How is your user experience so far w/ this settings? I have some follow up questions: What happens if the IP of host changes? For example because my laptop gets a new IP via DHCP from my router. Should I configure my router to always assign the same IP to my laptop? What happens when I'm on the go and connect to a new WiFi / router? Will this require to adjust the IP in the created Switch?
  • Alex Krauss
    Alex Krauss almost 4 years
    @Wlad This should work fine even if your external IP changes. Think of a (virtual) network 192.168.0.0/24 which connects the host (your laptop) with the VM. On that network, the host always has IP 192.168.0.1. The external network is separate from that and your laptop will have a different IP there.
  • GaryO
    GaryO almost 3 years
    Not quite, because you'll still get the new ssh key fingerprint confirmation each time the guest IP addr changes.