For me, the holy grail of working with virtual machines is
$ ssh root@my-vm
I am tired of manually updating /etc/hosts or looking at arp tables1. There’s got to be a better way. And there is! Here’s how. This works with Fedora 20. Your mileage may vary with other distros.
Add the following line to /etc/NetworkManager/NetworkManager.conf
under the [main] block:
dns=dnsmasq
This line tells NetworkManager to run a dnsmasq process.
hosts
style file that dnsmasq will use for name resolution.
$ curl -o /usr/bin/virt-hosts https://raw.github.com/awood/virt-utils/master/virt-hosts && chmod 755 /usr/bin/virt-hosts
$ echo "addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts" >> /etc/NetworkManager/dnsmasq.d/virt-hosts
This line tells NetworkManager to add the default.addnhosts
file to the list of places that dnsmasq looks at for name resolution.
$ yum install -y incron
$ systemctl enable incrond.service && systemctl start incrond.service
Set up incron to run virt-hosts
every time we detect a change in the status of a virtual machine.
$ echo "/var/lib/libvirt/dnsmasq/default.leases IN_MODIFY /usr/bin/virt-hosts -ur" > /etc/incron.d/virt-hosts
Add the following line to /etc/sysconfig/network-scripts/ifcfg-em1
DOMAIN="default.virt"
$ systemctl restart NetworkManager
$ ssh root@your-vm
Done!
1 The arp table solution seems really simple, but half the time my VMs vanish from the arp table and I can’t get their IP anymore.
Hopefully getting something that resembles a DNS server polling from LDAP set up tonight. I love virtual machines.