VirtualBox networking using vagrant - vagrant

I need to configure a vm into using vagrant and virtualbox as provider. The issue is related to how to allow network comunication between the virtual machine and the host machine.
Concretly, I need:
Each developer is using a log viewer in his host machine in order to be able see the log messages generated into vm applications. So I need to send log messages to an network address(host) from guest.
Each developer machine has its own ip address, so I'm not able to reference to a single ip since each developer has its host ip address.
Any ideas?

This is how I've set up my VMs lately, hopefully it points you in the right direction.
My requirements were:
enable internet access on a single guest basis
enable access guest to guest
enable access host to guest
avoid IP changes when changing physical network (lan home, lan work, random wlans)
avoid IP clash with various clients VPNs
avoid showing machines on the physical network
To do this use 2 network interfaces on all guests.
Default NAT, enables internet access;
HostOnly network, enables communication host-to-guest and guest-to-guest.
Steps:
Create hostonly network with DHCP (Virtualbox comes with a default one, I customized it)
hostonly network setup
hostonly network DHCP
add network adapters to the guest
NAT(default)
hostonly-network
Start the VM, check with ip a and ip r:
ip addr
ip route (has docker installed too, ignore that)
Create Vagrantfile to provision VM configured like above (I'm using bento/ubuntu-20.04).
NAT is on by default
join the hostonly-network with "modifyvm" (avoid :private_network as it creates a new network that will clash with the one already available), add to Vagrantfile:
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--nic2", "hostonly"]
vb.customize ["modifyvm", :id, "--hostonlyadapter2", "vboxnet1"] #use proper network name here
vb.customize ["modifyvm", :id, "--cableconnected2", "on"]
end
vagrant up
vagrant ssh
ip a will show eth1 as down
try sudo ifup eth1, in my case this failed
edit /etc/network/interfaces and add this:
allow-hotplug eth1
iface eth1 inet dhcp
sudo ifup eth1 should work now and get an ip from the DHCP of the host-only network
Probably these last steps could be added to vagrant shell provisioning but I'm still new to it.
Edit: add this section to Vagrantfile for the steps above:
config.vm.provision "shell", inline: <<-SHELL
if ! ifquery eth1 > /dev/null 2>&1; then
sudo echo "allow-hotplug eth1" >> /etc/network/interfaces
sudo echo "iface eth1 inet dhcp" >> /etc/network/interfaces
sudo ifup eth1
ip -4 a show dev eth1
fi
SHELL
At this point the vagrant vm should be accessible, able to access the web, able to access the hostonly network (other guests and the host through the configured ip, 192.168.178.1 in my case), these should all work:
ping 8.8.8.8 #web
ping 192.168.178.1 #host
ping 192.168.178.3 #other guest
Final result
Hopefully this enables all relevant communication for your use case also.

Related

How can I access a vagrant guest from another virtualbox guest?

The scenario is that my dev environment is on a Vagrant box on my laptop (host) and I would like to do browser testing in a vitualbox vm, so I need to see one vm from another.
The vagrant box's port is :8080 which is forwarded to the host on the same port :8080. So I can see the server from the host at localhost:8080
Which address should I be using for the browser testing vm?
The testing vm's default gateway?
The vagrant vm's ip?
The host's virtual network ip?
And should I be using a NAT or host only adapter on the browser testing vm?
That makes for a lot of combinations, all of which I believe I have tried. What else do I need to understand here?
In your use case, you should be using Bridged networking (Public Network in Vagrant). If the VMs reside on the same host, you can even use internal (Private Network in Vagrant).
If using Public Network, the VM's 2nd NIC will be able to obtain an IP address from the DHCP server in your network (e.g. your home router).
Simply add the following code block in your Vagrantfile and do a vagrant reload
Vagrant.configure("2") do |config|
config.vm.network "public_network"
end
You should be able to get the IP address by using vagrant ssh and ifconfig / ip addr show.
In case you don't want to go with public_network just like me then you should do the steps below using private_network:
Open Vagrantfile from your project root
Search for config.vm.network
Add this line config.vm.network "private_network", ip: "192.168.33.10". Remember this is not the IP of your base machine it's a virtual-box IP address and your machine IP should be different. You can say it's a fake IP address so change it to anything else like 192.168.30.20.
Reload your vagrant using vagrant reload.
Now go to your other virtual guest in my case it's the Windows Guest 2. My base is Linux Mint Vagrant box is on Ubuntu Guest 1. Open C:\Windows\System32\drivers\etc\hosts file as admin and do the above IP's entry in there like 192.168.33.10 local.youralias.com. And save the file, after that you can now browse the site now at http://local.youralias.com/.
In case your guest 2 is also Linux just edit this file sudo vi /etc/hosts, and add this line at top of it 192.168.33.10 local.youralias.com. Now save and exit and browse the URL :)
Enjoy! Happy coding.
Adding to accepted answer, you can actually set IP and specify which network interface to use.
My setup on linux box via wifi and static IP:
You can find your wifi interface name by running ifconfig command.
Vagrant.configure("2") do |config|
config.vm.network "public_network", :bridge => 'wlp8s0', ip: "192.168.1.199"
end
This may have many source cause. In my case, I use vagrant fedora boxe.
I tried:
First using the private_network that I attached to a host only adapter and launched httpd service to test the connection between guest and host
config.vm.network "private_network", type: "dhcp", name: "vboxnet2"
config.vm.network "forwarded_port", guest:80, host:7070
but I was not able to ping my guest machine from the host and could no telnet the httpd service opened
Second using public_network and launched httpd service to test connectivity
config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)", use_dhcp_assigned_default_route: true
I could ping my guest from my host but I could not telnet the httpd service.
For this two use case, the issue was that the port 80 on the fedora guest host was blocked by the firewall. Here is what fixed the issue and get all working for both privat_network and public_ntwork:
firewall-cmd --permanent --add-port 80/tcp #open the port permanently
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --list-port # list to check if the port was opened
systemctl stop firewalld # stop and open the firewall service
systemctl start firewalld
Old question, new answer: [disclaimer: i am not a vagrant expert]
both solutions might work but the solution in the "vagrant way of thinking" is that some component in your guest (rinetd?) should forward any requests to unknown ports to the host. From the host the request could then be mapped (via vagrant port forwarding) to a services that is running in the other guest.
So, to resume:
1.in guest-1 we do localhost:1234. Guest-1 will detect that this port is not available and forward to host
2. the host will check the vagrant port forwarding and forward to guest-2
3. in guest-2 we have some nice service listening to post 1234
4. done.

vagrant provisioned virtualbox cannot access the internet when booted up with an active VPN connection on the windows 7 host

I have a problem with networking while setting up a development virtual machine
The host environment is:
Windows 7x64
Vagrant 1.7.2
Virtualbox 4.3.20 r96997
has an active WLAN internet connections
can connect to a VPN
The relevant sections from the Vagrantfile are:
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.define 'crm' do |node|
node.vm.network :private_network, ip: '192.168.56.2'
end
end
With this, the guest machine has two network interfaces:
eth0: NAT
eth1: the private network
After a vagrant up the machine is always accessible through the private IP.
However, if at boot time the VPN connection
is active, then the guest machine cannot access the internet
is not active, then the guest machine can access the internet
On the host machine, with an active VPN connection, both public and VPN-restricted hosts are accessible.
Question: how should I modify the Vagrantfile to ensure that the guest machine always has internet access, regardless whether it was booted with an active VPN connection on the host machine or not?
Update:
adding
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
solves the internet connectivity issue, however, it brings the boot time of the guest machine to barely tolerable (many minutes)
ok. i had the same problem but i am using windows. i did fix it. what i did is that opened virtualbox.
go to settings
select network
change second adapter
select under " attached to: " ' Host-only Adapter'
under name. i selected " virtualbox host-only ethernet apdapter " do not select the name that have numbers at the end of the name example : #1, #2 and so on.
i went to my device manager and delete all the virtual network drivers "not your physical network drivers". again this was in windows 8.
i restart my pc
confirm my chance have applied
i vagrant up on my project directory and it was working

How to tunnel host traffic through guest in Virtualbox using Vagrant

I have a Ubuntu host and an Ubuntu guest. I want to achieve that my host traffic is tunnelled through my guest. Thus enabling me to do analysis on the host traffic from the guest. I am assigning two interfaces to the guest (in addition to the built in NAT interface which is provided by Vagrant). I am creating a bridge interface on the host called br0. One of the guest interfaces is connected via bridging to eth0 (the host interface which is connected to the Internet) and the other guest interface is connected via bridging to the host br0 interface. On the host I am deleting my default gateway and adding a new one with:
route del default
route add default dev br0
After that I can vagrant ssh into my guest and set up a bridge interface there as well with.
brctl addbr br0
brctl addif br0 eth1
brctl addif br0 eth2
After this setup the host traffic should be routed through the guest. If I run
tcpdump -vni br0
inside the guest I can see packets but I can't connect to the Internet. There is something missing. Any ideas?
I had to enable IPv4 forwarding in the guest.
Edit /etc/sysctl.conf to enable forwarding permanently:
net.ipv4.ip_forward = 1
Run this to activate the setting without a reboot:
sysctl -p /etc/sysctl.conf

Vagrant keeps creating unwanted network interfaces

I'm creating a VM definition and I want to specify a single "private_network" on the primary interface of the box (eth0, it's Linux)
Despite having only one config.vm.network statement, I keep getting the desired network set on eth1, while eth0 is assigned an ip from a 10.0.2.0/24 subnet which I have never defined.
How can I prevent this and have my desired 192.168.x.y/24 set on eth0?
Best,
Edoardo
eth0 is by default used by Vagrant for NAT (VirtualBox NAT networking mode), which allow you to vagrant ssh into the box (port forwarding rules - host 2222 <=> guest 22).
That's why you have 2 NICs even though you have only 1 config.vm.network in Vagrantfile.
I don't think it can be disabled, BUT I am not 100% sure, you may want to look into vagrant source code to dig further.
https://superuser.com/questions/957631/how-to-force-vagrant-to-have-a-single-bridged-network-interface
You can try to add the adapter: 1 to the config. Although I still find some problem with it. See if it will help you starts from somewhere.
Example:
config.vm.network "public_network", bridge: "Broadcom BCM5709C", adapter: "1", ip: "192.168.x.xx"
You will still have the eth1 which is host-only network...

vagrant add an interface without setting an IP address

I'm working through the puppet openstack instructions, and one of the requirements is as follows:
Each of the machines running the Openstack services should have a minimum of 2 NICS.
One for the public/internal network. This nic should be assigned an IP address
One of the virtual machine network. This nic should not have an ipaddress assigned
I've tried to specify a private network in Vagrant without an address, but hit the following error when calling vagrant up:
* An IP is required for a private network.
My first thought is to run a provision script to set the network, but there are warnings not to modify the network configuration files (the box is CentOS):
[vagrant#localhost ~]$ cat /etc/sysconfig/network-scripts/ifcfg-eth2
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=no
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.55.5
NETMASK=255.255.255.0
DEVICE=eth2
PEERDNS=no
#VAGRANT-END
Any ideas will be appreciated!
In the end, I created an 'unmanaged' network card by adding a virtualbox internal network:
vb.customize ["modifyvm", :id, "--nic2", "intnet"]
This works:
config.vm.network :private_network, ip: ""
You can try this:
node.vm.network :private_network, "ip": '0.0.0.0', auto_network: true

Resources