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.
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 188.8.131.52:2222 --- 184.108.40.206:2222
Proxmox by default creates one interface,
vmbr0. That config looks like this:
# /etc/network/interfacesauto vmbr0iface vmbr0 inet static address 220.127.116.11 netmask 255.255.255.0 network 18.104.22.168 broadcast 22.214.171.124 gateway 126.96.36.199 bridge_ports eth0 bridge_stp off bridge_fd 0
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
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:
Tags: bash, kvm, openvz, proxmox, proxmox-ve, ssh, tutorials, virtualization