This post will demonstrate how to access a static IP outside the virtual machine. Recently I got a scenario where I had set up a static IP from the terminal and was trying to access the IP from outside VM through Putty but I was not able to, so I did some R&D and found that my VM was restricted from accessing the internet of the host machine. This means I had to set up the network in such a way that my VM can get internet connectivity. So follow the below-mentioned steps to properly configure your network.
Here in this post, I have used Oracle Linux 7. The same process will be followed in RedHat Linux and CentOS.
1. Host Network Manager
– Open Oracle VM VirtualBox and select the VM for which you want to configure your network.
– Click on File and select the Host Network Manager option. A new dialog box will appear.
You can see that the adapter is already set. You just need to uncheck (disable) the DHCP Server option.
2. Network Setting
– Now go to the setting of the VM and click on the network option. There will be 4 adapters and we need to set the first two.
* Adapter 1 -> Enable Network Adapter and choose NAT in Attached to option.
* Adapter 2 -> Enable it as well and select Host-only Adapter in Attached to option.
and click OK.
Network Address Translation (NAT) allows the guest machine, here our VM, to access the internet from the host machine (The Windows machine). The Host machine can not access the VM in this case so to overcome this, we need the second adapter.
The Host-only Adapter enables the host (Windows machine) and guest (VM) to communicate while being on the same network.
Now we are done with the setting of VM, let’s start the machine and do the remaining changes in the terminal. Once started, log in with the root user and follow the steps listed below. I have not taken the screenshots and will try to mention all the steps so that you don’t get any problem.
1. Make IP Static
Since we have enabled two Adapters, one with the NAT and another with the Host-only Adapter network type, we have to change the second one to static. Go to the below-mentioned path.
Edit the file by pressing i to enable the insert mode and add the following:
TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none IPADDR=192.168.56.12 PREFIX=24 GATEWAY=192.168.56.1 DNS1=18.104.22.168 DNS2=22.214.171.124 DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=enp0s4 UUID=5a8f5169-49d8-4570-a4a6-841600abed6c ONBOOT=yes
2. Network Manager Text User Interface (nmtui)
We can also add a new connection and make our IP static by using the Network Manager Text User Interface (nmtui). Write nmtui on the terminal and hit Enter, a new dialog box will appear from where we can add or edit an existing connection.
1. Select Edit a connection
2. If you have not added the Ethernet connection while setting up the VM then select Add or else select the Ethernet connection and press Edit.
Fill the Profile Name, change the IPv4 CONFIGURATION to <Manual> and add the required fields as per your network. Once done then click OK.
This process will also configure the static IP. You can follow this when you have no GUI available.
Now when you ping the external IP (Static IP) from your host machine, it will show the response and also check the host machine’s IP in your VM, it will also show a positive result.