Creazione di un'interfaccia virtuale NAT & Host per VirtualBox
Jump to navigation
Jump to search
Introduzione
Si vuole creare un'interfaccia che permetta di creare una rete con
- delle VM che si vedano tra loro
- delle VM che accedano ad internet
- della VM accessibili dal PC
In pratica una rete Host Only + Nat
Installazione pacchetti
- Installare i pacchetti necessari:
sudo apt-get install uml-utilities
Configurazione interfacce
- Configurare l'interfaccia tun254, fisica ed il bridge, che così potrà essere utilizzato anche da KVM:
sudoedit /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto tun254
iface tun254 inet manual
pre-up tunctl -t tun254
pre-up iptables -t nat -A POSTROUTING -o wlan0 -s 10.11.12.0/24 -j MASQUERADE
pre-up iptables -t nat -A POSTROUTING -o eth0 -s 10.11.12.0/24 -j MASQUERADE
pre-up iptables -t nat -A POSTROUTING -o tap0 -s 10.11.12.0/24 -j MASQUERADE
pre-up iptables -t nat -A POSTROUTING -o usb0 -s 10.11.12.0/24 -j MASQUERADE
pre-up iptables -t nat -A POSTROUTING -o ppp0 -s 10.11.12.0/24 -j MASQUERADE
post-down iptables -t nat -D POSTROUTING -o wlan0 -s 10.11.12.0/24 -j MASQUERADE
post-down iptables -t nat -D POSTROUTING -o eth0 -s 10.11.12.0/24 -j MASQUERADE
post-down iptables -t nat -D POSTROUTING -o tap0 -s 10.11.12.0/24 -j MASQUERADE
post-down iptables -t nat -D POSTROUTING -o usb0 -s 10.11.12.0/24 -j MASQUERADE
post-down iptables -t nat -D POSTROUTING -o ppp0 -s 10.11.12.0/24 -j MASQUERADE
post-down tunctl -d tun254
auto br0
iface br0 inet static
address 10.11.12.254
netmask 255.255.255.0
bridge_ports tun254
bridge_stp off
bridge_fd 0
- Attivarla
sudo invoke-rc.d networking restart
- Verificare che esistano le interfacce
ifconfig tun254
tun254 Link encap:Ethernet HWaddr c6:78:12:8e:19:31
inet6 addr: fe80::c478:12ff:fe8e:1931/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:2048 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
ifconfig br0
br0 Link encap:Ethernet HWaddr c6:78:12:8e:19:31
inet addr:10.11.12.254 Bcast:10.11.12.255 Mask:255.255.255.0
inet6 addr: fe80::c478:12ff:fe8e:1931/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:1657 errors:0 dropped:0 overruns:0 frame:0
TX packets:513 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:227994 (227.9 KB) TX bytes:88648 (88.6 KB)
- Verificare che il nat in uscita sia attivato:
sudo iptables -L -n -v -t nat
sudo iptables -L -n -v -t nat
Chain PREROUTING (policy ACCEPT 16387 packets, 2414K bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 2318 packets, 353K bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 3789 packets, 290K bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 3782 packets, 290K bytes)
pkts bytes target prot opt in out source destination
11 664 MASQUERADE all -- * wlan0 10.11.12.0/24 0.0.0.0/0
0 0 MASQUERADE all -- * eth0 10.11.12.0/24 0.0.0.0/0
0 0 MASQUERADE all -- * tap0 10.11.12.0/24 0.0.0.0/0
0 0 MASQUERADE all -- * usb0 10.11.12.0/24 0.0.0.0/0
0 0 MASQUERADE all -- * ppp0 10.11.12.0/24 0.0.0.0/0
Server DHCP e DNS
Ubuntu 18.04
- In questa versione come DNS è installato systemd-resolvd, che non può bindare un'interfaccia.
- Si usa socat: installarlo:
apt-get install socat
- Per fare in modo di usare come dns 10.11.12.254, occorre attivare socat come:
sudo socat -4 UDP-LISTEN:53,fork,reuseaddr,bind=10.11.12.254 UDP:127.0.0.53:53
- Oppure creare la unit systemctl:
vi /etc/systemd/system/socat.service
# /etc/systemd/system/socat.service [Unit] Description=Socat Service for DNS on tun254 for Virtualbox After=network.target remote-fs.target nss-lookup.target # Se il bind è su una interfaccia vpn # systemctl list-units --no-pager | grep virtual-net # BindsTo=sys-devices-virtual-net-ztc35dwhra.device # After=sys-devices-virtual-net-ztc35dwhra.device [Service] ExecStart=/usr/bin/socat -4 UDP-LISTEN:53,fork,reuseaddr,bind=10.11.12.254 UDP:127.0.0.53:53 ExecStop=/bin/kill -s QUIT $MAINPID Restart=on-abort [Install] WantedBy=multi-user.target
systemctl daemon-reload systemctl enable socat systemctl start socat
Riferimenti
Ubuntu 15.04
- In questa versione, utilizzare l'istanza dnsmaq standard forita da networkmanager
- Fermare dnsmaq
sudo killall dnsmasq
- Creare il file di configurazione
sudoedit /etc/NetworkManager/dnsmasq.d/vboxnat.conf
#interface=br0 #log-queries listen-address=10.11.12.254 dhcp-range=interface:br0,10.11.12.11,10.11.12.99,5m # static lease dhcp-host=08:00:27:87:1f:1f,10.11.12.100 # dns forward local=/vbox.priv/ address=/prox01.vbox.priv/10.11.12.101 address=/prox02.vbox.priv/10.11.12.102 #dns reverse local=/12.11.10.in-addr.arpa./ ptr-record=101.12.11.10.in-addr.arpa.,"prox01.vbox.priv" ptr-record=102.12.11.10.in-addr.arpa.,"prox02.vbox.priv"
- Riavviare Network Manager
sudo service network-manager restart
- Collegare un'interfaccia esterna
- Per debuggare, scommentare log-queries e ripetere le due operazioni precedenti
- Verificare che il binding sia corretto:
sudo netstat -anp | grep dnsma
tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 12088/dnsmasq tcp 0 0 10.11.12.254:53 0.0.0.0:* LISTEN 12088/dnsmasq udp 0 0 127.0.1.1:53 0.0.0.0:* 12088/dnsmasq udp 0 0 10.11.12.254:53 0.0.0.0:* 12088/dnsmasq udp 0 0 0.0.0.0:67 0.0.0.0:* 12088/dnsmasq
Ubuntu precedenti a 15.04
- Installare e configurare un server DHCP (dhcpd3-server o dnsmasq)
sudo apt-get install dnsmaq sudoedit /etc/dnsmasq.con
dhcp-range=interface:tun254,10.11.12.11,10.11.12.99,5m dhcp-host=08:00:27:87:1f:1f,10.11.12.100
sudo invoke-rc.d dnsmasq restart
Configurazioni delle VM
- Configurare le VM come Bridged sull'interfaccia tun254. In questo modo si uscirà sempre su internet tra una delle due interfacce nattate.