In some of my last blogs, I wrote about how you can install Virtual machines on different hypervisors such as KVM, VMware, VirtualBox, etc.
I use the windows machine as a daily driver Virtual Machine on my Ubuntu box; having the choice of both operating systems at a time is excellent. And I can switch between them any time I want.
Can you install 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 install a VPN on a virtual machine and it can connect to a VPN within the VM 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 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 in 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 a 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 used by the enterprise to access internal enterprise 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 to the VPN from VM?
Having a VPN access is great, but while working on something on your local machine and you wanted to switch to VPN, what happens is that, it will disconnect you from some of the sessions that you already opened on your computer, so it is 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 will always point to google US servers, but what if you wanted to access some sites that are blocked in the US or in your country ?. 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 available products 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 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 or resources, you can do so from your local machine, and if you are accessing internet content from other countries, you can use VPN, which 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 your host machine as Ubuntu and you wanted to connect to VPN.
First, your windows VM should be connected to the internet. To connect VM to the internet, the easiest way out is to use the 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 connected to your host, 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 can 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
Your local machine (PC/MAC) 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 use 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 IPs.
Below is the output from my local machine and the guest VM. Both showing 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 let’s go ahead and configure the VPN in it.
1. Download the VPN client.
Now open your browser and log in to the NordVPN account and click on download on the screen’s left side.
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 download 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 see a list of gateways (countries) that you can connect.
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 my IP is, as you can see, it directly connected to Canada.
Let’s do one more thing, I am going to search Amazon on both the host machine and the guest VM, and let’s see the difference.
When I searched for amazon from my Ubuntu 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 search again, you can see how the search results are showing for Australia.
Here is my IP information, and the location says AU, which means Australia.
Let me know in the comment section below how did it work for you.