Skip to main content

Raymii.org Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

Proxmox VE - One Public IP

Published: 10-07-2014 | Author: Remy van Elst | Text only version of this article


Table of Contents


This guide will show you how to set up Proxmox with only one public IP. We willconfigure an extra interface bridge and make sure VM traffic is NATed. I have afew dedicated servers, some run Proxmox. Most of them however have only a fewIP's. Therefore the VM's in proxmox cannot all have a public IP. For most ofthem that is not a problem. If needed I run a proxy or set up iptables toforward ports to the VM's.

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)

This guide is tested on a proxmox machine running proxmox version 3.2.

What we will have at the end is a VM with an SSH port reachable on the publicIP:

Container/VM ------------ Proxmox Server -------------- Public Internet10.21.21.5:22 --- 10.21.21.5:22 NAT to 1.2.3.4:2222 --- 1.2.3.4:2222

Proxmox by default creates one interface, vmbr0. That config looks like this:

# /etc/network/interfacesauto vmbr0iface vmbr0 inet static    address 1.2.3.4    netmask 255.255.255.0    network 1.2.3.0    broadcast 1.2.3.255    gateway 1.2.3.1    bridge_ports eth0    bridge_stp off    bridge_fd 0

Replace 1.2.3.X with your public ip, network, gateway and such. Do note thatthere might be more interfaces, like vmbr1 for ipv6.

We create a new bridge which will enable NAT when the interface gets UP. Add thefollowing to the file:

# /etc/network/interfaces:auto vmbr2iface vmbr2 inet static    address 10.21.21.254    netmask 255.255.255.0    bridge_ports none    bridge_stp off    bridge_fd 0    post-up echo 1 > /proc/sys/net/ipv4/ip_forward    post-up iptables -t nat -A POSTROUTING -s '10.21.21.0/24' -o vmbr0 -j MASQUERADE    post-down iptables -t nat -D POSTROUTING -s '10.21.21.0/24' -o vmbr0 -j MASQUERADE    post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 2222 -j DNAT --to 10.21.21.5:22    post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 2222 -j DNAT --to 10.21.21.5:22

The first part:

address 10.21.21.254netmask 255.255.255.0bridge_ports nonebridge_stp offbridge_fd 0

defines the IP address and subnet mask of the new interface. It also tells thenetwork stack that the bridge has no actual ports (like eth0) and that theSpanning Tree Protocol should be disabled.

post-up echo 1 > /proc/sys/net/ipv4/ip_forward

Enables IP forwarding when this interface gets up. This allows the machine toforward packets.

post-up iptables -t nat -A POSTROUTING -s '10.21.21.0/24' -o vmbr0 -j MASQUERADEpost-down iptables -t nat -D POSTROUTING -s '10.21.21.0/24' -o vmbr0 -j MASQUERADE

These two lines enable the actual NAT-ing of packets from the source network'10.21.21.0/24' and vmbr0 as the output interface. If your WAN interface has adifferent name, change that here. The first line enables the natting when theinterface gets up, the second line deletes the firewall rule when the interfacegoes down.

post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 2222 -j DNAT --to 10.21.21.5:22post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 2222 -j DNAT --to 10.21.21.5:22

These two rules enable and disable the actual port forwarding of tcp port 2222on the WAN IP to tcp port 22 on internal IP address 10.21.21.5. Here as well theWAN interface (this time, the input interface) is vmbr0.

If you for example want to expose tcp port 80 of a VM with IP 10.21.21.6 on thepublic IP's port 80, you should also add these lines:

post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.21.21.6:80post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.21.21.6:80

When you create a KVM VM, make sure it is attached to the bridge vmbr2. Itshould also have a static IP configured in the range you define. OpenVZ venetinterfaces with an IP in this range automagiaclly work.

Don't forget to restart the network afterwards:

/etc/init.d/networking restart
Tags: bash, kvm, openvz, proxmox, proxmox-ve, ssh, tutorials, virtualization