Create Local VM With Fixed IP Address

People are using VMs for local test more and more. The VMs are usually accessed by IP. Most likely laptops running the VM will be carried to different places, whether home, office or starbucks. Hence the wifi changes all the time. So the IPs of latop and VM change as well.

Quite natural we want the VM to have a fixed IP, thus we don’t need to change current ip of the VM all the time. So how to achieve that?

Create Local VM With Fixed IP Address



To be simply, we can set fixed IP for the VM by adding a host-only network adapter to the VM. If we use “Host-only Adatper”, the VM will have outbound internet traffic but no inbound internet traffic. This is OK for most cases of local test.

In below, we will mainly discuss about virtualbox, while other virtualizations like vmware, hyper-V should be similar.

To use “Host-only Adapter”, firstly we need to make sure we have created a Host-only Network and specify subnet CDIR, masks etc. In below example, we have created a Host-only Network called vboxnet2. And specified subnet as 192.168.58.0/24.

host only network

host only network

Then we configure the VM to use this network adatper. Thus the vm will get a fixed ip by dhcp like 192.168.58.XXX.

vm network adapter type

  • If you’re using virtualbox, above configuration can be easily automated by vagrant script like below.
# -*- mode: ruby -*-
Vagrant.configure(2) do |config|
  # The OS is Ubuntu 14.04 64 bits
  config.vm.box = "ubuntu/trusty64"
  config.vm.hostname = "sandbox.test.com"
  config.vm.provider "virtualbox" do |vb|
    vb.name = "vagrant-sandbox"
    # Customize the VM to use 2048 MB RAM and 3 cpu cores
    vb.customize ["modifyvm", :id, "--memory", 2048,
                  "--cpus", 3]
  end
  # host-only network
  config.vm.network :private_network, ip: "192.168.58.11"
end

linkedin
github
slack

PRs Welcome

Blog URL: https://www.dennyzhang.com/virtualbox_vm_fixed_ip


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.