In some of my last blogs, I wrote about how you can install Virtual machines on different hypervisors such as KVM, VMware, VirtualBox, and so on.
I use the windows machine as a daily driver Virtual Machine on my Ubuntu machine, having the choice of both operating systems at the same time is great.
Can you use a VPN on a virtual machine?
Some of my blog readers asked me, is it possible to connect the guest VM to the VPN while the host machine connected to the local internet? or does VPN work on VM ?
The answer is, yes, you can use a VPN on a virtual machine and it works great.
In this blog, let’s take a look at how you can access VPN from your Virtual machine while your host machine is connected to the local internet.
For example, you would be somewhere in the US and you can connect to Australia as the VPN gateway in your VM and access your local internet resources via your PC/MAC. And on your VM you could access the Australia internet just like how you would access the internet in Australia as if you are physically present in the country.
isn’t that cool? 🙂 so lets get to it.
A VPN software – Any VPN software with multiple gateways (countries) should work, however, I recommend using Nordvpn, as it works great across the platform, be it Windows, MAC, IOS, Android, Linux and so on, also with the choice of multiple countries as a VPN gateway. Click here to get a nordVPN.
I will get a small commission if you use my link to purchase without any charge from your side 🙂
A virtual machine – I am using windows virtual machine with internet access. Any other VM’s should work as well.
What is VPN ?
VPN stands for Virtual private network, it helps you connect to the private network via the public internet and it also encrypts the traffic, which means no one can spoof into your internet browsing. To put it simply, you can mask your public IP with different regions IP.
Note: There is also another VPN which is used by the enterprise to access enterprise internal resources, while connected from home over the internet.
Though both works in a similar way, we are not talking about enterprise VPN here.
If you are not familiar with VPN, I covered the benefits of using the VPN here, you may check them out.
Why should I connect from VM instead of a local machine?
having VPN access is great but while working on something on your local PC and you wanted to switch to VPN, it might disconnect from some of the sessions that you already opened on your computer so it is really inconvenient at times.
Let’s say, for example, you are from the US and trying to access google, by default based on your public IP your website would always point to google US servers, but what if you wanted to access some sites that are blocked in the US or in your region ?. then VPN can come to your rescue.
Another such use case is when you wanted to search for a product and understand it’s a cost in a different country for example in the UK.
Then you can connect to the UK VPN gateway, and start looking at the products that are available in the UK, more on that later.
However, connecting and disconnecting VPN back on forth based on your need would be a little difficult and inconvenient at times as it would disconnect from some of the sessions that you already opened. so there got to be a better way.
And there is, You can create your own VM and connect those VMs to the VPN, so if you are trying to access your local Content you can do so from your local machine and if you are accessing internet content from other countries you can use VPN, which is always connected via your VM.
Isn’t that awesome ? so let’s begin.
Creation of the Virtual machine
I have covered plenty of blogs related to the creation of VMs, you may check out the blogs below for that.
- Windows 10 in KVM, centos/Redhat or Ubuntu
- Ubuntu on VMware workstation on MAC/Windows.
- Centos on KVM
- Ubuntu in KVM
- Centos on VMware workstation/Fusion.
Connect VM to the internet.
Lets imaging you are using a windows machine as the VM and you wanted to connect to VPN.
First you should be connected to the internet. To connect VM to the internet the easiest way out is to use NAT interface.
NAT will help you connect your VM private network to the public internet.
It will also take care of assigning the IP address and everything to the VM’s.
Note: If you have advanced network setup like switches that are connected to your host, then you could use bridge networking and take care of the Internet sharing from the switch. But here we are using the NAT option.
Configure NAT on the hypervisors.
Nat configuration on KVM.
If you are using KVM just like me then you could use the NAT interface to connect the KVM machine to the internet.
- Shutdown the VM.
- And Goto the VM settings and choose Virtual Network ‘default’: NAT interface in NIC.
- Click on Apply.
NAT configuration on VMWare workstation.
In the case of VMware workstation pro, right click on the VM and click on settings. And in settings you can select NAT as the interface.
NAT configuration on Virtualbox.
The same goes with VirtualBox as well, you should right-click on the VM and settings and in the network select NAT interface.
Check your internet IP
You local machine (PC/MAC) is connected to the internet and you can verify its public IP address , by doing a google search ‘what is my ip’
Both the guest VM and the local host should have the same public IP and the location as both are using the same internet service provider.
Note: Public IP is something used on the internet, in your home network however you should be using something like 192.168.0.1 or 10.0.0.1 from the private range of IP’s.
Below is the output from my local machine as well as from the guest VM, both shows the same information.
Note : to protect privacy I masked the IP address.
How to set up a VPN for a virtual machine?
We have the VM setup and working fine and it is also can access the internet. so lets go ahead an configure the VPN in it.
1. Download the VPN client.
Now open your browser and log in to the nordvpn account an click on download on the left side of the screen.
You can click on Download on the NordVPN.
As you can see there are many options to choose from, since I am using windows I am downloading the windows version from the download for windows options under Apps.
You may download the client that works with your operating system.
After downloaded just double click on it to install.
Click on Finish
When you open the nordVPN client on your VM, it should prompt you to enter your credentials here. You may enter the credentials that you have received from the VPN provider and click on sign in.
Once you logged in, you should be able to see list of gateways (coutries) that you can connect to.
I am going to connect to Canada gateway, so I clicked on Canada. You can see on the bottom of the window that it is trying to connect.
Once it is connected you should be able to see it is connected notification down below.
Now on my guest VM let me open google and type what is my IP, as you can see it is now connected to Canada.
Now lets do one thing, I am going to search amazon on my both the host machine as well as on the guest VM and let’s see the difference.
When I searched for amazon from my local machine it showed me the result from India.
But on my VM, you can see the search results shows for amazon Canada 🙂
And both windows side by side
Also the search results are different on both the host as well as on the guest VM.
Now let me switch my VPN to Australia and see the difference.
After the VPN is connected and when I try to do the search again you can see how the search results are showing for Australia
Here is my IP information, and the location it says AU which means Australia.
Let me know in the comment section below how did it work for you.