Using a script - How can I tell if another device already exists on the network with the same hostname as my pi?

I'm building an IoT product using a raspberry pi.

Currently I communicate with the pi via a mobile app by using raspberrypi.local host name, but obviously that doesn't work well in production scenario, where the user already might have another pi with the same name or even if I change the hostname, already have my product.

So in the case that the user buys a second product of mine which would inevitably have the same hostname, how can I check if another device with the same names exists on the network once the new pi boots up and is connected to the network?

  • sudo nmap -sn -R should give you all the hostnames.
    – Dougie
    Commented Mar 31, 2021 at 23:18
  • @Dougie this ends up scanning 256 hosts from 0-255 and shows that each one of them is up. Why are we checking for that range of IP addresses and how do I make use of it? Commented Apr 1, 2021 at 9:44
  • Take a look the manual for nmap or is your local Google broken.
    – Dougie
    Commented Apr 1, 2021 at 11:29
  • @Dougie I asked because your answer does not show the hostnames on the network and nor does it provide clarity into how it could detect hostname conflicts. Commented Apr 1, 2021 at 16:43

1 Answer 1


I would not bother as you have to connect to their network to find out what's on it and part of the DHCP 'chatter' is registration of your name with the server.

I would set the host name to the MAC address BEFORE shipping or allow the user to set the name as part of a start up script before network connection is made.

Start the name with a standard text to define your device followed by a dash followed by a chunk of the MAC without colons. e.g.


Normally the MAC address is believed to be unique but this is not true for the Pi due to the number made. The risk is minimal that two will be the same on the same network - there was a post a long time ago on how they are generated the numbers on the RPF forums.

As part of your customer record it would be polite to track the units they have to make sure you never duplicate it.

Do not forget that the fully qualified name for a pi includes the domain 'local'. This may cause issues on the customers network so you need to handle that as well (e.g. start up screen to ask for info).

  • Hmm @Andrew I was hoping to have a more elegant solution than asking the user for a name or pre-setting unique names. But I might have to end up doing that. I had read somewhere that the raspberry pi solves this problem itself when multiple pi's join the same network by having the new pi change it's hostname to raspberrypi<number>, or something along those lines. But I haven't been able to find that article, nor have I been able to replicate it using my own raspberry pi's Commented Mar 30, 2021 at 19:55
  • What issues might come up on the customers network when dealing with local? Commented Mar 30, 2021 at 19:57
  • 1
    Mixing .local with another domain name can give Active Directory managers pain and increases workload - see serverfault.com/questions/76715/… You cannot get certificates for this so any web services from your device will be unencrypted. Some routers / switches will not pass MDNS searches. There is always the remote possibility (and its VERY remote) that ICANN will sell it. MS SBS and Apple kit expect(ed) this and you may not want to impact the set up. On corporate networks management of new devices is key.
    – user130616
    Commented Mar 30, 2021 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.