2021 Linux VM Setup: Part 1 - How to install Ubuntu 20.04 on a laptop with Windows 10 Hyper-V
There's a few tricks to getting a Linux VM development environment working on a Windows 10 laptop properly, here's my process.
The biggest issue with working off a laptop with VMs is network access. The default network bridge in Hyper-V requires a persistent internet connection and if you assign a static IP to the Linux VM on your home subnet, you will not be able to connect to sites hosted by the VM while on a different network.
Ideally, our Windows 10 laptop has a DHCP connection and our Linux VM has a static IP, is shell only, no gui, and by running a web service on the VM, we can access it via a browser in Windows 10, even if there is no internet connection.
Windows 10 Pro or Enterprise is required, Windows 10 Home can not run Hyper-V.
Download Ubuntu 20.04 Server LTS iso, create a new Gen 2 Hyper-VM VM and install Ubuntu via the iso on it. Use defaults for Ubuntu install process and have it install SSH automatically. Once installed shut the VM down if running. I recommend naming the default user you create the same as you Windows 10 username, for Samba simplicity.
My Hyper-V VM defaults are as follows:
- Firmware - Disable Secure Boot
- Memory - Enable Dynamic Memory, 512MB minimum, normal memory weight
- Processor - 4 Virtual Processors
- Hard Drive - 50GB Gen 2 Virtual Disk
- Integration Services - uncheck Backup and Guest Service
- Checkpoints - Disabled
- Automatic Start Action - Always start this VM automatically, with a 30 second delay
Delete the Default Network Adapter installed on the VM. We're going to create a new one.
Open PowerShell as an Administrator.
Replace the IP Address and subnet below with one that doesn't exist at home, work or anywhere you might go. If you use an IP that conflicts with a real network connection the VM may not work properly.
New-VMSwitch -SwitchName "NATSwitch" -SwitchType Internal
New-NetIPAddress -IPAddress 192.168.12.1 -PrefixLength 24 -InterfaceAlias "vEthernet (NATSwitch)"
New-NetNAT -Name NATNetwork -InternalIPInterfaceAddressPrefix 192.168.12.0/24
Attach newly created network to your Ubuntu VM, start the VM, open up the connection and log in (you won't be able to ssh in yet).
Run the ip link
command to get the default network connection name. 99% chance it's eth0
.
ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000link/ether 00:15:5d:0c:94:0d brd ff:ff:ff:ff:ff:ff
sudo vim /etc/netplan/00-installer-config.yaml
and edit the yaml file with below, changing the nameservers to preferred and IP address to what you changed above.
network:
ethernets:
eth0:
dhcp4: false
addresses:
- 192.168.12.10/24
gateway4: 192.168.12.1
nameservers:
addresses: [1.1.1.1, 8.8.8.8]
version: 2
Restart network service sudo netplan apply
Verify changes are successful ip addr show dev eth0
You should now have a static IP for your Linux VM that works anywhere!
If you want to connect to a DNS name instead, on Windows 10, edit the C:/Windows/System32/drivers/etc/hosts
file, adding the IP and preferred DNS name to the list.
Let's get a few more basics done before we can call this tutorial complete.
Disable Cloud-Init on Ubuntu (restart to take effect)
sudo touch /etc/cloud/cloud-init.disabled
Edit /etc/apt/sources.list
create a copy of each uncommented line pointing to your regions ubuntu update servers and remove the region code, for example:
deb http://au.archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse
This will set these as backup servers if your regional package server has an outage.
Update all the packages
sudo apt update && apt upgrade
Setup basic firewall rules
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw limit ssh
sudo ufw allow samba
sudo ufw disable
sudo ufw enable
You should now be able to SSH into the server. If you download and install MS Terminal you can add the below to the settings.json file to have it auto-connect to the server on startup.
"commandline": "ssh matt@192.168.12.10"
Now, onto Samba basics.
sudo apt install samba
sudo /etc/samba/smb.conf
and enable write access on home folder.
If you want to save and access files via Windows on another path, then create that folder and with chown
and chmod
assign yourself write access to the folder.
Add it to the smb.conf file.
[projects]
path = path/to/projects
valid users = matt
browsable = yes
writable = yes
read only = no
Add your user to smbpasswd.
sudo smbpasswd -a matt
sudo smbpasswd -e matt
Restart Samba
sudo systemctl restart smbd
Restart the VM and see if you can SSH in and connect to file shares in File Explorer. If you have the same username and password across Windows and Linux, file shares should be seamless.
This is part of a collection of blog posts, detailing my laptop setup preferences.
2021 Linux VM Setup: Part 1 - How to install Ubuntu 20.04 on a laptop with Windows 10 Hyper-V
2021 Linux VM Setup: Part 2 - Setting up Git & Github on Ubuntu 20.04
2021 Linux VM Setup: Part 3 - Docker & Docker-Compose Setup on Ubuntu 20.04
2021 Linux VM Setup: Part 4 - zsh, VS Code & Terminal Customisation on Ubuntu 20.04
2021 Linux VM Setup: Part 5 - Windows VS Code Setup for Ubuntu 20.04 VM