KVM forward ports to guests VM with UFW on Linux

Originally published at: https://www.cyberciti.biz/faq/kvm-forward-ports-to-guests-vm-with-ufw-on-linux/

My Debian/Ubuntu/CentOS Linux server using KVM as a hypervisor. I would like to forward ports to guests VM with UFW iptables. How do I forward ports on a Linux server running libvirt/KVM to specified ports on VM’s, when using NAT?

Thanks Vivek for again one interesting post.

One remark: it might be of interest to explain the role of the subnet, which is not shown in the schema.
As I understand it, it is the sole subnet on the “internet” from which the port forwarding will be activated, as the forward rule added in the first part is limited to the “source” (-s)
To activate it for any external address, the -s parameter should be either absent or

Also, I’d like to point that the hook could be located in /etc/libvirt/hooks/network so that it doesn’t get executed everytime a machine is started but only at start of the network.

Thanks again for all your tutorials.

yes, I would update page soon with newer hook info. Thanks!

1 Like