Search code examples
vagrantphpstormxdebughyper-v

Hyper-V: Connect Xdebug debugger running on guest to debugging client on host


Using Vagrant, I am running a Ubuntu box with PHP-FPM and Xdebug 2 in Hyper-V. The network I am using is Hyper-V's default switch.

From my host, I can connect to the Ubuntu box using SSH and the web server is also accessible using the VM's IP address in my browser address bar. From inside the VM, I can access the internet and I can also ping the host by the IP address of the Hyper-V default switch. So the network seems to be set up correctly.

However, the Xdebug debugger running in the VM won't connect to my IDE (PhpStorm) running on my host.

The xdebug.log shows (172.17.96.1 is the IP address of the Hyper-V default switch)

[2641] Log opened at 2022-09-15 12:59:46
[2641] I: Checking remote connect back address.
[2641] I: Checking header 'HTTP_X_FORWARDED_FOR'.
[2641] I: Checking header 'REMOTE_ADDR'.
[2641] I: Remote address found, connecting to 172.17.96.1:9000.
[2645] E: Time-out connecting to client (Waited: 200 ms). :-(
[2645] Log closed at 2022-09-15 12:59:46

even though PhpStorm on my host seems to be listening on every IP address:

PS C:\Users\me> netstat -a

Active Connections

  Proto  Local Address          Foreign Address        State
  ...
  TCP    0.0.0.0:9000           MyLaptop:0        LISTENING
  TCP    0.0.0.0:9003           MyLaptop:0        LISTENING
  ...

If I use telnet in the VM to connect to my host, it doesn't timeout, but it also doesn't connect successfully:

vagrant@ubuntu-18:~$ telnet 172.17.96.1 9000
Trying 172.17.96.1...

It stays at "Trying" forever.

How can I make the Xdebug debugger connect from the VM to PhpStorm on my host?


Edit to include interfaces of my host and of my guest

ipconfig on my host

I shortened my IPv6 address on the WiFi adapter

PS C:\WINDOWS\system32> ipconfig

Windows IP Configuration


Ethernet adapter VirtualBox Host-Only Network:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::84b9:f362:fa7f:ae2d%13
   IPv4 Address. . . . . . . . . . . : 192.168.56.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

Unknown adapter Local Area Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Local Area Connection* 1:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Local Area Connection* 10:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter WiFi:

   Connection-specific DNS Suffix  . : speedport.ip
   IPv6 Address. . . . . . . . . . . : 2003:**********
   Temporary IPv6 Address. . . . . . : 2003:**********
   Link-local IPv6 Address . . . . . : fe80::bc1a:df06:b465:974b%24
   IPv4 Address. . . . . . . . . . . : 192.168.2.200
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : fe80::1%24
                                       192.168.2.1

Ethernet adapter Bluetooth Network Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter vEthernet (Default Switch):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::99ae:3e6e:68bc:a228%30
   IPv4 Address. . . . . . . . . . . : 172.17.96.1
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . :

Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::41c8:f5fb:fbd4:3bef%60
   IPv4 Address. . . . . . . . . . . : 172.19.32.1
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . :

Interface config in the VM

vagrant@ubuntu-18:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.111.24  netmask 255.255.240.0  broadcast 172.17.111.255
        inet6 fe80::215:5dff:fe38:101  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:38:01:01  txqueuelen 1000  (Ethernet)
        RX packets 651  bytes 102218 (102.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 757  bytes 118391 (118.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 90  bytes 7020 (7.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 90  bytes 7020 (7.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Pinging the default switch IP from inside the VM

vagrant@ubuntu-18:~$ ping 172.17.96.1
PING 172.17.96.1 (172.17.96.1) 56(84) bytes of data.
64 bytes from 172.17.96.1: icmp_seq=1 ttl=128 time=0.294 ms
64 bytes from 172.17.96.1: icmp_seq=2 ttl=128 time=0.527 ms
64 bytes from 172.17.96.1: icmp_seq=3 ttl=128 time=0.366 ms
64 bytes from 172.17.96.1: icmp_seq=4 ttl=128 time=0.526 ms
64 bytes from 172.17.96.1: icmp_seq=5 ttl=128 time=0.704 ms
64 bytes from 172.17.96.1: icmp_seq=6 ttl=128 time=0.375 ms
^C
--- 172.17.96.1 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5125ms
rtt min/avg/max/mdev = 0.294/0.465/0.704/0.137 ms

Solution

  • I am running a Ubuntu box with PHP-FPM and Xdebug 2

    Xdebug 2 is no longer supported, please upgrade to Xdebug 3.

    The xdebug.log shows (172.17.96.1 is the IP address of the Hyper-V default switch)

    That is not true, it shows the IP address from where the HTTP request initiated from, because you have remote IP address discovery turned on. This does not work in all situations, especially with virtualisation. This HTTP address is not necessarily an IP address that the Linux guest can talk to on the host.

    You will likely need to use the public IP address of your host, which is 192.168.2.200 for xdebug.remote_host:

    Wireless LAN adapter WiFi:
    
       Connection-specific DNS Suffix  . : speedport.ip
    …
       IPv4 Address. . . . . . . . . . . : 192.168.2.200
    

    And also disable the auto discovery.