Virtualbox NAT + Host-Only Adapter

7,095

You should not use the same IP address in your guest (like the one on the host interface). If you setup vboxnet0 on the host to IP address 192.168.56.1, then in your guest you should use 192.168.56.2 for example. The gateway for your guest should be the host's IP (in your case 192.168.56.1).

This should do the host<->guest connection. If you want your guest to have internet too, then you should masquerade it's IP (or the network) on your host.

iptables -t nat -I POSTROUTING -s 192.168.56.0/24 -j MASQUERADE
sysctl net.ipv4.ip_forward=1

you may need to set the default policy to forward or to enable your masqueraded network specifically:

iptables -P FORWARD ACCEPT
iptables -t nat -P POSTROUTING ACCEPT

note: You can execute these lines with root, to enable the internet access to your guest. But, these lines should probably go to your firewall configuration of yout host. (i.e. ufw). The sysctl configuration file is: /etc/sysctl.conf, you can set ip_forward there, if you want it always enabled.

Share:
7,095

Related videos on Youtube

DarthBoa
Author by

DarthBoa

Updated on September 18, 2022

Comments

  • DarthBoa
    DarthBoa over 1 year

    My host is Ubuntu 12.04 and guest is a Debian squeeze (LAMP server). I want to allow the host to connect the guest. and allow the guest to connect the internet. The guest should not be enabled from outside. That's the reason why I don't use network-bridge. I want to setup a static IP for the host. I followed this tutorial.

    So I took these steps but I can not connect via ssh from host to guest. I cannot call the server via browser. ping works however!

    I've created a virtual host-only network adapter (vboxnet0) with following settings:

    IPv4-Adress: 192.168.56.1
    IPv4-Netmask: 255.255.255.0
    

    ifconfig in Ubuntu host shows this:

    eth0      Link encap:Ethernet  Hardware Adresse XX:XX:XX:XX:XX:XX  
              inet Adresse:192.168.2.100  Bcast:192.168.2.255  Maske:255.255.255.0
              inet6-Adresse: XXXX:XXX:XXXX:XXXX:X:X/XX Gültigkeitsbereich:Global
    
    
    vboxnet0  Link encap:Ethernet  Hardware Adresse XX:XX:XX:XX:XX:XX  
              inet Adresse:192.168.56.1  Bcast:192.168.56.255  Maske:255.255.255.0
              inet6-Adresse: XXXX:XXX:XXXX:XXXX:X:X/XX Gültigkeitsbereich:Verbindung
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metrik:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:10069 errors:0 dropped:0 overruns:0 carrier:0
              Kollisionen:0 Sendewarteschlangenlänge:1000 
              RX-Bytes:0 (0.0 B)  TX-Bytes:1539501 (1.5 MB)
    

    On the Debian squeeze guest, the /etc/network/interfaces looks like this:

     # The host-only network interface
       auto eth1
       iface eth1 inet static
       address 192.168.56.1
       netmask 255.255.255.0
       network 192.168.56.0
       broadcast 192.168.56.255
    

    I've already tried to change eth1 to eth0 without a result.

    In VirtualBox settings the promiscuous-mode allows VMs and Host to connect.

    I have reached the limits of my knowledge.

    the output of:

    netstat --inet --inet6 -ln | grep :22
    

    is nothing.

    the output of:

    sudo iptables -L INPUT -nv
    

    is:

     Chain INPUT (policy ACCEPT 166 packets, 30786 bytes)
      pkts bytes target     prot opt in     out     source               destination 
    
    • Hauke Laging
      Hauke Laging about 11 years
      What is the output of netstat --inet --inet6 -ln | grep :22 and iptables -L INPUT -nv?
    • S edwards
      S edwards over 10 years
      Well your problem has nothing to do with linux, you should create a private network between your host and your virtual machine through the VB user interface and add a NAT interface to your Virtual machine. you will have internet trhough natted interface and private acces to it with the private network adater christophermaier.name/blog/2010/09/01/…