Launching a CentOS7 Virtual Machine on Windows10: Part 2: Networking

In my previous post, Launching a CentOS7 Virtual Machine on Windows10, I went through the steps I took to set up a CentOS7 virtual machine. In this post, I’ll go through how I configured networking. This involves adding a host-only adapter in VirtualBox and adding a static IP to the virtual machine.

Once these are configured, the virtual machine will:

  1. Have access to the internet (via your host machine)
  2. Be accessible via its static IP from your host machine

Prerequisites

Before you start, you will need to set up the virtual machine and PuTTY SSH by following the steps in Launching a CentOS7 Virtual Machine on Windows10.

Step 1: Test the Network on the Virtual Machine

  1. SSH to the virtual machine via PuTTY
  2. In the terminal, run:
    $ ping google.com
    You should see the response ‘Name or service not known’. This is because no DNS resolvers have been configured.
  3. Now try running:
    $ ping 8.8.8.8
    This should give the response ‘Network is unreachable’. This is because the network adapter has not yet been set up.

Step 2: Create a Host-Only Adapter in VirtualBox

You will need to create a Host-Only Ethernet Adapter in VirtualBox. This will bridge the virtual machine’s network to your host machine’s network, allowing it to access the local network and internet.

  1. Make sure your virtual machine is powered off. There are a few ways to achieve this, one of which is to click the ‘X’ at the top right of the main virtual machine terminal window (the one launched by VirtualBox).
  2. In VirtualBox, go to File -> Host Network Manager -> Create. You may get a warning pop up, if so just click ‘OK’. Take a note of the name of the adapter that was created.
  3. Take a note of the IPv4 address/mask of the adapter (in the IPv4 address/mask column in the list of adapters).
  4. Close the ‘Host Network Manager’ window, or click on ‘Machine Tools’ to get back to the list of virtual machines.
  5. Click on your virtual machine, and click ‘Settings’ at the top.
  6. Click ‘Network’.
  7. Make sure that ‘Adapter 1’ is attached to NAT. Under the ‘Adapter 2’ tab, select ‘Enable Network Adapter’.
  8. Select ‘Host-only Adapter’, and choose the name of the Host-Only Ethernet Adapter that you just created.
  9. Click ‘OK’.

Step 3: Configure a static IP address

Now configure a static IP on the virtual machine. The IP address will be based upon the IP/mask of the Host-Only Ethernet Adapter that you noted in Step 2. The IP has a netmask of 24, which means only the first 24 bits are fixed. The last 8 bits can be whatever you choose. For example, if your adapter has an IP/mask of 123.123.123.1/24, you can choose any IP for your virtual machine from 123.123.123.2 up to 123.123.123.255 (The last 3 digits can be 2-255).

  1. Power on your virtual machine (by clicking ‘Start’ in VirtualBox).
  2. Once booted, SSH onto the machine via PuTTY.
  3. Run the following:
    $ sudo su
    $ cd /etc/sysconfig/network-scripts
    $ vim ifcfg-enp0s8
    
  4. This will create a new ethernet interface file. Add the following to the file:
    BOOTPROTO=static
    ONBOOT=yes
    IPADDR=192.168.xxx.xxx
    NETMASK=255.255.255.0
    DEVICE=enp0s8
    PEERDNS=no
    
    You will need to replace the IP address on the IPADDR line with your chosen IP. Save and close the file by pressing the ESC key and then running:
    :wq
    
  5. Open the existing ethernet interface file with:
    $ vim ifcfg-enp0s3
    
  6. Add the following lines to the end of the file:
    DNS1=8.8.8.8
    DNS2=8.8.4.4
    
    Then update the line ONBOOT=no to ONBOOT=yes. Save and close the file by pressing the ESC key and then running:
    :wq
    
  7. Reboot by either of these two options:
    1. Clicking the x in the top-right of the GUI consol and selecting ‘Power off the machine’.
    2. In VirtualBox, selecting the vm from the list, right-click -> ‘Close’ -> ‘Power Off’, then confirm by clicking ‘Power Off’.
  8. Once powered off, click ‘Start’ in VirtualBox to start.

Step 4: Re-Test the Network on the Virtual Machine

  1. SSH to the virtual machine via PuTTY.
  2. In the terminal, run:
    $ ping google.com
    You should now see a response like:
    PING google.com (142.250.186.142) 56(84) bytes of data.
    64 bytes from fra24s07-in-f14.1e100.net (142.250.186.142): icmp_seq=1 ttl=55 time=33.1 ms
    ...
    
    Hit Ctrl-C to quit out of the ping.
  3. Now try running:
    $ ping 8.8.8.8
    This should now give a response like:
    PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
    64 bytes from 8.8.8.8: icmp_seq=1 ttl=115 time=15.1 ms
    64 bytes from 8.8.8.8: icmp_seq=2 ttl=115 time=18.0 ms
    ...
    
    Hit Ctrl-C to quit out of the ping.
  4. On your host machine, open PowerShell.
  5. In the terminal, run (replacing YOUR_IP with your virtual machine’s static IP):
     > ping YOUR_IP
    You should get a response like:
    Pinging 192.168.xxx.xxx with 32 bytes of data:
    Reply from 192.168.xxx.xxx: bytes=32 time<1ms TTL=64
    Reply from 192.168.xxx.xxx: bytes=32 time<1ms TTL=64
    Reply from 192.168.xxx.xxx: bytes=32 time<1ms TTL=64
    Reply from 192.168.xxx.xxx: bytes=32 time<1ms TTL=64
    
    Ping statistics for 192.168.xxx.xxx:
        Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
        Minimum = 0ms, Maximum = 0ms, Average = 0ms
    
  6. If all three tests pass, the network is up and running!

Step 5: Set Up a Direct PuTTY Session

Now that your virtual machine has a static IP, you can set up a PuTTY session that uses the IP address directly, instead of using the port forwarding.

  1. Open PuTTY.
  2. Select ‘Session’ on the left-hand side, if not already selected.
  3. Click on the name of the saved session for your virtual machine, e.g. ‘osbox’ and click ‘Load’.
  4. For ‘Host Name (or IP address)’, replace the entry with ‘osboxes@YOUR_IP’. Be sure to replace YOUR_IP with the static IP address you configured on the virtual machine.
  5. For ‘Port’, enter 22.
  6. For ‘Connection type’, select ‘SSH’.
  7. Under ‘Saved Sessions’, type a new name for your new virtual machine session, e.g. ‘osbox-direct’.
  8. Click ‘Save’.
  9. Click ‘Open’.
  10. You may get a pop-up warning to say that the server’s host key is not cached in the registry. This is because the IP you are using is different and not known to PuTTY. This is OK, click ‘Yes’.
  11. You should see a PuTTY terminal open, and a prompt for your ssh key passphase will appear (if you set one).
  12. Enter the passphrase you created for your ssh key (if you created one).
  13. You should get logged in with your ssh key, and it should have been faster, as it didn’t require port forwarding.

Conclusion

You should now have a virtual machine with a static IP, that is accessible over your local network. You should be able to ping and ssh to your virtual machine IP, and your virtual machine should have access to the internet (via your host machine).

If you want to learn about how to set up a shared folder on your virtual machine, see my follow-on post, Launching a CentOS7 Virtual Machine on Windows10: Part 3: Shared Folder.

Thanks for reading!