« 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
 
(12 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
KEYWORDS: FORWARD, FORWARDING, PORT FORWARDING, KVM, QEMU, LIBVIRT
KEYWORDS: FORWARD, FORWARDING, PORT FORWARDING, KVM, QEMU, LIBVIRT, MAILINABOX, OVH2
 
[[Info::KVM:: Comment accéder à une VM à partir d'un réseau local (c.a.d pas à partir de la machine hôte)]]
 
'''Le 03/09/2024:'''
Sauf que, après cette manip, il restait encore un problème car les ports forwardés n'étaient pas visible de l’extérieur.
La véritable solution est ici: '''http://pcwiki.srv18.pcstreet.org:800/index.php?title=UNix::port_forwarding_entre_ovh2_%26_mx.apsi.net'''
 
'''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 qui posait un problème.
J'ai viré /etc/libvirt/hooks/qemu et 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: (''A priori la première règle suffit à faire marcher les ports forwarding: Wait & See'')
        # 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:'''
  '''Le 27/08/2024:'''
  En fin de compte, c'est très simple (''cf https://wiki.libvirt.org/Networking.html#Forwarding_Incoming_Connections'')
  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
  '''#1)''' '''Stopper la VM''' vers laquelle on veut forwarder un port
   
   
  #2) Créer (''ou éditer s'il existe'') /etc/libvirt/hooks/qemu:
  '''#2) Créer''' (''ou éditer s'il existe'') '''/etc/libvirt/hooks/qemu''':
     (''Exemple pour forwarder le port ovh2::65000 vers mailinabox::65000'')
     (''Exemple pour forwarder le port ovh2::65000 vers mailinabox::65000'')
   
   
Ligne 28 : Ligne 70 :
  fi
  fi


  #3) Relancer la VM: c'est fini !
  #3) '''Relancer la VM''': c'est fini !

Dernière version du 18 septembre 2025 à 06:38

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

Info::KVM:: Comment accéder à une VM à partir d'un réseau local (c.a.d pas à partir de la machine hôte)
Le 03/09/2024:
Sauf que, après cette manip, il restait encore un problème car les ports forwardés n'étaient pas visible de l’extérieur.
La véritable solution est ici: http://pcwiki.srv18.pcstreet.org:800/index.php?title=UNix::port_forwarding_entre_ovh2_%26_mx.apsi.net
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 qui posait un problème.

J'ai viré /etc/libvirt/hooks/qemu et 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: (A priori la première règle suffit à faire marcher les ports forwarding: Wait & See)
       # 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 !