KVM with one host IP and a different subnet for machines

5,792

Solution 1

The gateway configured on guest is not an IP of the host machine, I would try this config on guest:

auto eth0
iface eth0 inet static
       address 255.46.187.153
       netmask 255.255.255.255
       gateway 255.9.24.80
       pointopoint 255.9.24.80
       dns-nameservers <host provided nameservers>
       up ip addr add 255.46.187.154 dev eth0

also on host, make sure to add the route to to guest, add this to /etc/network/interfaces

iface eth0 inet static
       (...)
       up route add -host 255.46.187.153 dev br0
       up route add -host 255.46.187.154 dev br0

this way you can use network and broadcast addresses for guests (255.46.187.152 and 255.46.187.159)

some resources, sorry not in english:

http://www.ingent.net/ca/2012/03/server-virtualization-kvm-hetzner/ (catalan)

http://wiki.hetzner.de/index.php/KVM_mit_Nutzung_aller_IPs_-_the_easy_way (german)

Solution 2

If the VM aren't on the same network as the host, then you may need to set up the host as a router for the VMs:

iptables --table nat --append POSTROUTING --out-interface br0 -j MASQUERADE
iptables --append FORWARD --in-interface br0 -j ACCEPT
echo 1 > /proc/sys/net/ipv4/ip_forward

Edit: Sorry for the "MASQUERADE", I simply copy-pasted a script I had lying around. In your case, you don't need it. But it can be useful in a different case, for instance when you want to give your VMs privates IPs.

Share:
5,792

Related videos on Youtube

Jguy
Author by

Jguy

Updated on September 18, 2022

Comments

  • Jguy
    Jguy almost 2 years

    I've already setup a KVM host with proper IP configurations, but my host had me create DHCP and use that to assign the IP's to the machines. I want to see if there's an easier way to do it (or better). Upon my first setting out on this, I didn't find anything that pointed me in the right direction. I'm coming off a fresh install of Debian 6.0 x64, so I have nothing installed. I've logged in, queried for the below information and changed the password from my host set one.

    I have a Debian 6.0 x64 system with the following initial network configuration (substituted 255 in place of my real first octave):

    # tail /etc/network/interfaces
    auto  eth0
    iface eth0 inet static
    address   255.9.24.80
    broadcast 255.9.24.95
    netmask   255.255.255.224
    gateway   255.9.24.65
    
    # default route to access subnet
    up route add -net 255.9.24.64 netmask 255.255.255.224 gw 255.9.24.65 eth0
    

    I have a /29 subnet that I want the virtual machines to use from my host:

    IP: 255.46.187.152 /29
    Mask: 255.255.255.248
    Broadcast: 255.46.187.159

    Usable IP addresses: 255.46.187.153 to 255.46.187.158

    I like the interface of Cloudmin, so I want to try and use that if I can to administrate my guests.

    So, my questions:

    How do I set this up on the host system the best so that I can use the additional Subnet IP's on the guests and have them accessible from the internet?

    I also need to host a DNS server, which means one of these VM's has to have two IP's assigned to it and accessable from the outside world. How can I do that using Cloudmin? I had a question about this here: Multiple IP addresses assigned to one KVM VM But I just reformatted the entire server and am trying to figure out a better way of doing this.

    Machine information:

    # ip route show
    255.9.24.64/27 via 255.9.24.65 dev eth0
    255.9.24.64/27 dev eth0  proto kernel  scope link  src 255.9.24.80
    default via 255.9.24.65 dev eth0
    
    brctl is empty
    
    # ip addr list
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether c8:60:00:54:b5:d8 brd ff:ff:ff:ff:ff:ff
    inet 255.9.24.80/27 brd 255.9.24.95 scope global eth0
    inet6 fe80::ca60:ff:fe54:b5d8/64 scope link
      valid_lft forever preferred_lft forever
    

    Thank you for any help you can provide me.

    EDIT: I've installed kvm and cloudmin:

    aptitude install qemu-kvm libvirt-bin
    wget http://cloudmin.virtualmin.com/gpl/scripts/cloudmin-kvm-debian-install.sh
    ./cloudmin-kvm-debian-install.sh
    

    Rebooted and now my network configuration looks like this:

    # device: eth0
    iface eth0 inet manual
    
    # default route to access subnet
    
    
    iface br0 inet static
        address 255.9.24.80
        netmask 255.255.255.224
        broadcast 255.9.24.95
        network 255.9.24.64
        bridge_ports eth0
        gateway 255.9.24.65
    

    I setup in Cloudmin the Start IP as 255.46.187.153 and End IP as 255.46.187.158. The CIDR is 29 and the gateway is 255.46.187.152.

    I've installed a guest with ubuntuserver 12.04 x64, which was able to get and retrieve internet resources during installation, but now cannot reach anything nor can it be reached from anything. Its network configuration is:

    iface eth0 inet static
     address 255.46.187.153
     netmask 255.255.255.224
     broadcast 255.46.187.159
     gateway 255.46.187.152
     dns-nameservers <host provided nameservers>
    

    And is not able to ping google.com through DNS or direct IP, I can't ping the VM from the outside or the host. any ideas now?

  • Michael Hampton
    Michael Hampton almost 12 years
    What's the MASQUERADE business for? He doesn't seem to be using NAT.
  • Jguy
    Jguy almost 12 years
    Indeed. No NAT here.
  • Jguy
    Jguy almost 12 years
    I remember trying this configuration (hetzner is actually my host) a while ago, and it did not work. I just re-tried it, paying attention to all configs and it still does not work.
  • Jguy
    Jguy almost 12 years
    I originally followed this guide: wiki.hetzner.de/index.php/KVM_mit_Nutzung_aller_IPs_aus_Subn‌​etz and it worked. The only limitation is the fact that I need to assign up to 4 IP's (I think godaddy requires 4) to register my own nameserver. I cant seem to do that with that configuration.
  • Lluís
    Lluís almost 12 years
    I am at hetzner too, and I followed the two guides, both are working but first is simpler (the easy way one)
  • Lluís
    Lluís almost 12 years
    I edited answer to configure additional IP on guest
  • Lluís
    Lluís almost 12 years
    you still need to enable ip_forward as wazoox says (echo 1 > /proc/sys/net/ipv4/ip_forward)
  • Michael Hampton
    Michael Hampton almost 12 years
    @JohnMish As an aside, your domain's nameservers should not be on the same host, or even in the same data center. That defeats the whole purpose of redundancy and high availability.
  • Jguy
    Jguy almost 12 years
    Blah. Can't +1 your answer. I had forgot the ip_forwarding. Thanks for pointing it out!
  • Jguy
    Jguy almost 12 years
    Blah. I had been missing something and missing something. After running through it for the 15,000th time it finally works. Thank you SO MUCH!