DNS for Your Virtual Machines
For me, the holy grail of working with virtual machines is
<code class="prettyprint">$ ssh root@my-vm</code>
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.
-
Read this article. It will explain the basics, but follow the instructions below because there are a few differences in the process on Fedora.
-
Add the following line to
/etc/NetworkManager/NetworkManager.conf
under the [main] block:
<code>dns=dnsmasq</code>
This line tells NetworkManager to run a dnsmasq process.
- Download this script that will take care of writing out a
hosts
style file that dnsmasq will use for name resolution.
<code class="prettyprint">$ curl -o /usr/bin/virt-hosts https://raw.github.com/awood/virt-utils/master/virt-hosts && chmod 755 /usr/bin/virt-hosts</code>
$ 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.
<code class="prettyprint">$ echo "/var/lib/libvirt/dnsmasq/default.leases IN_MODIFY /usr/bin/virt-hosts -ur" > /etc/incron.d/virt-hosts</code>
- Add the following line to
/etc/sysconfig/network-scripts/ifcfg-em1
<code>DOMAIN="default.virt"</code>
-
$ 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.