How to publish a computer name to Multicast DNS
I recently bought a Raspberry Pi and I wanted to be able to SSH into it. The problem is - what's its IP address?
If I have a monitor/keyboard attached to the Pi, then I can run ifconfig
and it produces output like:
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.7.207 netmask 255.255.252.0 broadcast 192.168.7.255
inet6 fe80::da3a:ddff:fe45:863f prefixlen 64 scopeid 0x20<link>
ether d8:3a:dd:45:86:3f txqueuelen 1000 (Ethernet)
RX packets 78677 bytes 46740811 (44.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10826 bytes 1248499 (1.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
That means I can SSH into 192.168.7.207
. This works, but it has a huge
drawback: the IP address can change, so I need to attach a monitor/keyboard.
This kind of obviates the need for SSH. It also produces SSH warning messages
each time the IP address changes.
I use a EERO router, which has software which allows me to pin specific devices
to specific IP addresses. So I can pin my PI to 192.168.7.207
. I don't like
to remember a sequence of numbers, so I add an entry to my /etc/hosts
:
192.168.7.207 raspberry-pi
Now I can disconnect my monitor/keyboard and ssh raspberry-pi
. But there are still some drawbacks:
I need to configure the pinned IP address in some proprietary router software. If I change my router, I'll need to redo this work.
I need to configure /etc/hosts
on any device I use to ssh to
raspberry-pi
. If I ever need to change these addresses, it'll be a pain to
track them all down.
This is my current solution, and I'm quite happy with it. I can configure my raspberry-pi to publish its name via Multicast DNS (aka mDNS). The software to do this for Linux is Avahi. On NixOS this can be setup as follows:
{
networking.hostName = "raspberry-pi";
# Enable LAN hostname resolution (so other machines on the same LAN can find
# this one)
services.avahi = {
enable = true;
nssmdns = true;
publish = {
enable = true;
addresses = true;
domain = true;
hinfo = true;
userServices = true;
workstation = true;
};
};
}
With mDNS I can ssh into my Pi with ssh raspberry-pi.local
. Any machine on
the same LAN can resolve this address. I don't need any external configuration.
If you enjoyed this post, please let me know on Twitter or Bluesky.
Posted December 29, 2023.
Tags: #linux