Vagabundo
Eu mudei para usar o Vagrant + Vagrantfiles
exclusivamente para fazer isso com o Virtualbox. Este é um arquivo que eu uso que define 2 VMs no meu laptop.
Características deste Vagrantfile
:
- VMs são Ubuntu 16.04 (xenial64)
- As VMs solicitam endereços IP públicos do servidor DHCP da LAN
- As VMs utilizarão a interface da NIC do seu Host do Virtualbox (veja o arquivo)
- Eu deixei o nome da imagem
centos/7
no arquivo se você quiser trocar
Recursos para o Vagrant
- Site principal
- Instalando o Vagrant
- catálogo disponível publicamente de caixas Vagrant
- arquivo de caixa do CentOS / 7
Exemplo de Vagrantfile
$ cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
##############
### References
# - https://www.vagrantup.com/docs/vagrantfile/machine_settings.html
# - https://www.vagrantup.com/docs/vagrantfile/tips.html
# - https://www.vagrantup.com/docs/networking/forwarded_ports.html
##############
#unless Vagrant.has_plugin?("HostManager")
# raise 'HostManager plugin not installed!'
#end
require 'digest/sha1'
uniqID = "_" + (Digest::SHA1.hexdigest Dir.pwd()).slice(0..8)
image = "ubuntu/xenial64"
#image = "centos/7"
if Vagrant::Util::Platform.windows? then
interfaces = [
"Intel(R) 82583V Gigabit Network Connection",
"Intel(R) 82579LM Gigabit Network Connection",
]
elsif Vagrant::Util::Platform.darwin? then
interfaces = [
"en0: Wi-Fi (AirPort)",
"en1: Wi-Fi (AirPort)",
]
else
interfaces = [
"eth0",
"eth1",
"wlan0",
]
end
Vagrant.configure("2") do |config|
vboxName1 = "box-101"
hostname1 = "box101"
config.vm.define "#{vboxName1}" do |front|
front.vm.box = "#{image}"
#front.vm.network :private_network, ip: "192.168.56.101"
front.vm.network :public_network, use_dhcp_assigned_default_route: true, bridge: interfaces
front.vm.provider :virtualbox do |v|
v.customize ["modifyvm", :id, "--memory", 512]
v.customize ["modifyvm", :id, "--name", "#{hostname1}#{uniqID}"]
end
end
vboxName2 = "box-102"
hostname2 = "box102"
config.vm.define "#{vboxName2}" do |front|
front.vm.box = "#{image}"
#front.vm.network :private_network, ip: "192.168.56.102"
front.vm.network :public_network, use_dhcp_assigned_default_route: true, bridge: interfaces
front.vm.provider :virtualbox do |v|
v.customize ["modifyvm", :id, "--memory", 512]
v.customize ["modifyvm", :id, "--name", "#{hostname2}#{uniqID}"]
end
end
end
Uso
inicie as duas VMs $ vagrant up
inicie uma VM no horário
$ vagrant up box-101
$ vagrant up box-102
ssh para VM box-101
$ vagrant ssh box-101
Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-130-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud
0 packages can be updated.
0 updates are security updates.
/usr/bin/xauth: file /home/vagrant/.Xauthority does not exist
vagrant@ubuntu-xenial:~$
confirmar endereço IP
vagrant@ubuntu-xenial:~$ ip a l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 02:97:71:8a:f0:d8 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::97:71ff:fe8a:f0d8/64 scope link
valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:e6:43:ac brd ff:ff:ff:ff:ff:ff
inet 172.16.12.208/24 brd 172.16.12.255 scope global enp0s8
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fee6:43ac/64 scope link
valid_lft forever preferred_lft forever
vagrant@ubuntu-xenial:~$
NOTA: É um comportamento esperado que você terá 2 interfaces. O Vagrant / Virtualbox sempre configura uma interface semelhante a enp0s3
, que está associada à interface 10.0.2.15/24
. Este é o NIC que o Vagrant está usando quando está entrando na VM entre outras coisas.
Testando a conectividade de rede
Com o Vagrantfile
acima e as 2 VMs iniciadas (box-101 & box-102), podemos verificar 3 cenários de conectividade de rede. OBSERVAÇÃO: Antes de começarmos, anote os endereços IP da VM.
Antecedentes
Para a caixa 101 (172.16.12.208):$ ip a l enp0s8
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:e6:43:ac brd ff:ff:ff:ff:ff:ff
inet 172.16.12.208/24 brd 172.16.12.255 scope global enp0s8
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fee6:43ac/64 scope link
valid_lft forever preferred_lft forever
Para o box-102 (172.16.12.116):
$ ip a l enp0s8
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:df:3b:06 brd ff:ff:ff:ff:ff:ff
inet 172.16.12.116/24 brd 172.16.12.255 scope global enp0s8
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fedf:3b06/64 scope link
valid_lft forever preferred_lft forever
Cenários
Cenário # 1: box-102 → box-101## On box-101 run:
$ nc -l 12345
## On box-102 run:
$ curl -v telnet://172.16.12.208:12345
* Rebuilt URL to: telnet://172.16.12.208:12345/
* Trying 172.16.12.208...
* Connected to 172.16.12.208 (172.16.12.208) port 12345 (#0)
^C
Cenário # 2: Host do VirtualBox → box-101
## On box-101 run:
$ nc -l 12345
# On Virtualbox Host
$ curl -v telnet://172.16.12.208:12345
* Rebuilt URL to: telnet://172.16.12.208:12345/
* Trying 172.16.12.208...
* Connected to 172.16.12.208 (172.16.12.208) port 12345 (#0)
^C
Cenário # 3: Servidor Remoto → box-101
## On box-101 run:
$ nc -l 12345
## In Remote Server
$ curl -v telnet://172.16.12.208:12345
* About to connect() to 172.16.12.208 port 12345 (#0)
* Trying 172.16.12.208...
* Connected to 172.16.12.208 (172.16.12.208) port 12345 (#0)
^C