« Kvm::comment forwarder un port vers une VM » : différence entre les versions

De wiki_pcwiki2
Aller à la navigationAller à la recherche
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 4 : Ligne 4 :
  Sauf que après cette manip, la VM a des problèmes réseau (''par exemple: impossibilité de faire des apt-get'')
  Sauf que après cette manip, la VM a des problèmes réseau (''par exemple: impossibilité de faire des apt-get'')
  J'ai relancé OVH2::iptable et le problème avait disparu.
  J'ai relancé OVH2::iptable et le problème avait disparu.
  C'est donc l'interdépendance entre l'iptable du serveur hôte  (OVH2) et l'iptable lancé par la VM dans le hooks
  C'est donc l'interdépendance entre l'iptable du serveur hôte  (OVH2) et l'iptable lancé par la VM dans le hooks.
  J'ai donc ajouté un fichier OVH2:/etc/iptables.d/rules/virsh:
  J'ai ajouté un fichier OVH2:/etc/iptables.d/rules/virsh:
  #!/bin/bash
  #!/bin/bash
  # Wed Aug 28 07:33:43 UTC 2024
  # Wed Aug 28 07:33:43 UTC 2024
Ligne 18 : Ligne 20 :
   
   
  #              ssh apache smtp smtp smtp imap sieve
  #              ssh apache smtp smtp smtp imap sieve
  for intPort in 222 80 443 25  465  587  993  4190
  for intPort in '''222 80 443 25  465  587  993  4190'''
  do
  do
         # DNAT:
         # DNAT:
Ligne 24 : Ligne 26 :
         # you can use the -j DNAT target of the PREROUTING chain in NAT to specify a destination IP address and port  
         # you can use the -j DNAT target of the PREROUTING chain in NAT to specify a destination IP address and port  
         # where incoming packets requesting a connection to your internal service can be forwarded:
         # where incoming packets requesting a connection to your internal service can be forwarded:
         /sbin/iptables -t nat -A PREROUTING -i eno3 -p tcp -d ${PUBLIC_IP} --dport ${intPort} -j DNAT --to ${INTERNAL_IP}:${intPort}
         '''/sbin/iptables -t nat -A PREROUTING -i eno3 -p tcp -d ${PUBLIC_IP} --dport ${intPort} -j DNAT --to ${INTERNAL_IP}:${intPort}'''
   
   
         # POSTROUTING & IP MASQUERADING:
         # POSTROUTING & IP MASQUERADING:

Version du 28 août 2024 à 13:36

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

Le 28/08/2024:
Sauf que après cette manip, la VM a des problèmes réseau (par exemple: impossibilité de faire des apt-get)
J'ai relancé OVH2::iptable et le problème avait disparu.
C'est donc l'interdépendance entre l'iptable du serveur hôte  (OVH2) et l'iptable lancé par la VM dans le hooks.

J'ai ajouté un fichier OVH2:/etc/iptables.d/rules/virsh:

#!/bin/bash
# Wed Aug 28 07:33:43 UTC 2024
#  cf: https://www.cyberciti.biz/faq/how-to-configure-ufw-to-forward-port-80443-to-internal-server-hosted-on-lan/

# Includes et configurations pour la VM mailinabox
. /etc/iptables.d/iptables.conf

# Redirection des ports de OVH2 vers la VM mailinabox
INTERNAL_IP="192.168.122.227"
PUBLIC_IP="51.68.207.177"

#              ssh apache smtp smtp smtp imap sieve
for intPort in 222 80 443 25   465  587  993  4190
do
       # DNAT:
       # If you have a server on your internal network that you want make available externally, 
       # you can use the -j DNAT target of the PREROUTING chain in NAT to specify a destination IP address and port 
       # where incoming packets requesting a connection to your internal service can be forwarded:
       /sbin/iptables -t nat -A PREROUTING -i eno3 -p tcp -d ${PUBLIC_IP} --dport ${intPort} -j DNAT --to ${INTERNAL_IP}:${intPort}

       # POSTROUTING & IP MASQUERADING:
       # To allow LAN nodes with private IP addresses to communicate with external public networks, 
       # configure the firewall for IP masquerading, which masks requests from LAN nodes 
       # with the IP address of the firewall’s external device such as eth0:
       # /sbin/iptables -t nat -A POSTROUTING -o eno3 -j MASQUERADE
       # OR:
       # /sbin/iptables -t nat -A POSTROUTING -s 192.168.1.0/24 ! -d 192.168.1.0/24 -j MASQUERADE
done
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 !