Whenever you read on the internet of how to connect to other devices on your home network using a hostname instead of an IP address when they the machine doesn't broadcast it, you generally get the following two answers:
- add an entry to your hosts file manually yourself
- configure your router to send out the name.
There's a couple of issues with this, firstly I've never owned a router with this functionality and I've never been with an ISP which will even let you use your own router - so that option is out.
The other option of entering it manually into your hosts file seems to most including myself seems somewhat of cross purposes as the whole point is that it's all automatic and like most I just never bother.
The solution
Somehow it's starting us all in the face - you can piggyback on the Avahi broadcast service your Raspberry Pi or any other linux device is already running. It's not complicated either, just a quick tweak to a file and your done.
If you don't already have Avahi broadcasting your Raspberry Pi's hostname it's just one command away with no setup:
sudo apt install avahi-daemon
and you are firing out your hostname. Avahi only uses fully qualified domain names which means it has to have some sort of suffix, the default it uses is .local so your raspberrypi will be reachable over the network as raspberrypi.local to any machine including macs and Apple devices.
How to broadcast other hostnames
It's easy:
sudo nano /etc/avahi/hosts
then add the IP and hostname you want to broadcast at the bottom, such as:
192.168.0.100 switch.local
Save and exit then restart the daemon:
sudo avahi-daemon restart
In my case it's the old Sky broadband router (SR101) which I've repurposed as an ethernet switch so I can have a wired connection for all my Raspberry Pis seeing as my current router has only two ethernet ports. 192.168.0.100 is it's IP address to which I connect to it's web configuration page so I can now connect to it using it's hostname im my browser
switch.local/
Job's a good 'n.