Last Updated: 2021-05-16
After changing the IP address of my virtual machine (using Vagrant), I stumbled across a name resolution issue, both in my PHP program and from the command line of my virtual machine. Essentially I could not long access external URLs.
<?php $sftp->login($username, $key) || $sftp->login($username, $password) PHP Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /home/vagrant/code/vendor/phpseclib/phpseclib/phpseclib/Net/SSH2.php on line 1163
Equivalent failure from the command line:
$ ping www.google.com ping: www.google.com: Temporary failure in name resolution
What was up?
The issue was that I had changed the Vagrant box to use the IP address
127.0.0.1, and this IP address was seemingly used by the router for getting
DCHP settings. Therefore my virtual machine could not get a name server.
I temporarily fixed by appending
/etc/resolv.conf (the DNS file) in the
Vagrant machine with the Google Public DNS server, thereby replacing the one
provided by DHCP which proved problematic.
# The >/dev/null bit stops it printing to screen as well echo "nameserver 188.8.131.52" | sudo tee /etc/resolv.conf > /dev/null
This is not a long-term fix, because when DCHP is used, this file (i.e. the nameservers) should, amongst other things, be automatically filled with the right info for the network.