Thursday, 9 August 2018

Vagrantfile - FOSSTECH Style - Disable NAT

I have been using vagrant to setup my demo system and I don't really like how vagrant setup my virtual machine so I did few changes to it.

I need my virtual machines to be able to interact with each other so I will need my virtual machines to be bridged. I also like to interact using standard ssh using password instead of using vagrant ssh, so my setup will enable password based ssh and disable vagrant ssh.

My environment router is 192.168.100.254 so you will need to adjust the file a little bit to your environment.

Create a directory to contain your virtual machine:
# mkdir node01
# cd node01

Create a file called Vagrantfile containing the following:

 Vagrant.configure("2") do |config| 
 # Use SUSELeap 15.0
 config.vm.box = "opensuse/openSUSE-15.0-x86_64"

 # Configure Hostname and Public IP Address
 config.vm.hostname = "node01"
 config.vm.network "public_network", bridge:"eth0", ip:"192.168.100.51"

 # Make sure the VM has 2 CPU with 4 GB RAM
 config.vm.provider "virtualbox" do |vb|
       vb.memory="4096"
       vb.cpus="2"
 end

 config.vm.provision "shell", inline: <<-SHELL
       # Add default route
       echo "default 192.168.100.254 - eth1" > /etc/sysconfig/network/routes
       # Disable the default NAT interface - this will disable vagrant ssh too
       sed -i "s/STARTMODE.*/STARTMODE=off/" /etc/sysconfig/network/ifcfg-eth0
       # Update resolv.conf
       echo "search fosstech.biz" > /etc/resolv.conf
       echo "nameserver 8.8.8.8" >> /etc/resolv.conf
       echo "nameserver 8.8.4.4" >> /etc/resolv.conf
       # Allow password sshd
       sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' \ /etc/ssh/sshd_config
       systemctl restart sshd
       systemctl restart network &
 SHELL
end


Start your virtual machines:
# vagrant up 

If you never start openSUSE-15.0-x86_64 image before, it will download the image from vagrant and configure the virtual image based on the Vagrantfile above. 

Once the virtual machine is ready, you can ssh into the VM as vagrant user. 

# ssh vagrant@192.168.100.51

The password of vagrant is vagrant too. 

Enjoy your virtual machine !!!

No comments:

Post a Comment