KVM forward ports to guests VM with UFW on Linux

To forward ports to guests VM with KVM on a Linux machine using UFW (Uncomplicated Firewall), you can use the following steps:

  1. Enable UFW on the host machine by running the command: sudo ufw enable
  2. Allow incoming traffic on the desired ports by running the command: sudo ufw allow [port_number]
  3. Configure the virtual network in the KVM settings to use NAT mode. This can be done by editing the XML configuration file for the virtual machine, or by using a GUI tool such as virt-manager.
  4. Create a new rule in UFW to forward traffic from the host’s network interface to the guest’s IP address on the desired port. You can use the following command:
sudo ufw route allow [protocol] from any to [guest_ip] port [port_number]

For example, to forward incoming TCP traffic on port 80 to a guest with IP address 10.0.0.100, you would use the command:

sudo ufw route allow tcp from any to 10.0.0.100 port 80
  1. Finally, reload UFW to apply the changes: sudo ufw reload

It is important to note that this is a basic example and you may want to adjust the rules depending on your specific use case and security requirements.

Leave a Comment