KVM: Fix NAT issues

January 13, 2018 in hypervisor, linux ‐ 1 min read

I just ran into the issue that my virtual machines are not beeing natted properly anymore. It took me some time to fix it so here are the commands in case you run into the same issues.

Edit Iptables NAT and SNAT

sudo iptables -t nat -A POSTROUTING -s 192.168.122.0/24 ! -d 192.168.122.0/24 -p tcp -j MASQUERADE --to-ports 1024-65535 sudo iptables -t nat -A POSTROUTING -s 192.168.122.0/24 ! -d 192.168.122.0/24 -p udp -j MASQUERADE --to-ports 1024-65535 sudo iptables -t nat -A POSTROUTING -s 192.168.122.0/24 ! -d 192.168.122.0/24 -j MASQUERADE sudo iptables -t nat -A POSTROUTING -s 192.168.122.0/24 -o enp2s0 -j SNAT --to-source YOUR-PUBLIC-IP-ADDRESS

Afterwards it should look like this:

 

image

Now the clients should be able to talk to the outside world again.

image

Saving your Work

sudo iptables-save > /path/to/config.file

Cheers, Ori