To forward ports to guests VM with KVM and UFW on a Linux host, you can use the following steps:
Make sure that the KVM virtualization is installed and enabled on your Linux host.
Install the UFW firewall on your host by running the following command:
sudo apt-get install ufw
sudo ufw enable
Start the guest VM that you want to forward the ports to.
Find the IP address of the guest VM by running the following command:
sudo virsh domifaddr <guest-vm-name>
Replace <guest-vm-name>
with the name of your guest VM.
sudo ufw allow proto tcp to any port <host-port> to <guest-vm-ip> port <guest-vm-port>
Replace <host-port>
with the port number on the host, <guest-vm-ip>
with the IP address of the guest VM, and <guest-vm-port>
with the port number on the guest VM.
For example, to forward port 8080 on the host to port 80 on the guest VM, you would run the following command:
sudo ufw allow proto tcp to any port 8080 to 192.168.122.50 port 80
sudo ufw status
You should see the port forwarding rule listed in the output.
http://localhost:8080
to access the web server on the guest VM.