Kvm::comment forwarder un port vers une VM

De wiki_pcwiki2
Aller à la navigationAller à la recherche

KEYWORDS: FORWARD, FORWARDING, PORT FORWARDING, KVM, QEMU, LIBVIRT

Le 27/08/2024:
En fin de compte, c'est très simple (cf https://wiki.libvirt.org/Networking.html#Forwarding_Incoming_Connections)

#1) Stopper la VM vers laquelle on veut forwarder un port

#2) Créer (ou éditer s'il existe) /etc/libvirt/hooks/qemu:
    (Exemple pour forwarder le port ovh2::65000 vers mailinabox::65000)

#!/bin/bash

if [ "${1}" = "mailinabox" ]; then

  # Update the following variables to fit your setup
  GUEST_IP="192.168.122.227"
  GUEST_PORT=65000
  HOST_PORT=65000

  if [ "${2}" = "stopped" ] || [ "${2}" = "reconnect" ]; then
   /sbin/iptables -D FORWARD -o virbr0 -p tcp -d $GUEST_IP --dport $GUEST_PORT -j ACCEPT
   /sbin/iptables -t nat -D PREROUTING -p tcp --dport $HOST_PORT -j DNAT --to $GUEST_IP:$GUEST_PORT
  fi
  if [ "${2}" = "start" ] || [ "${2}" = "reconnect" ]; then
   /sbin/iptables -I FORWARD -o virbr0 -p tcp -d $GUEST_IP --dport $GUEST_PORT -j ACCEPT
   /sbin/iptables -t nat -I PREROUTING -p tcp --dport $HOST_PORT -j DNAT --to $GUEST_IP:$GUEST_PORT
  fi
fi
#3) Relancer la VM: c'est fini !