27

I have just updated my Mac to macOS Sierra 10.12. My hostname is ninjaPixels-Macbook-Pro and if I run:

ping ninjaPixels-Macbook-Pro.local or ping ninjaPixels-Macbook-Pro

I get an error of unknown host.

host ninjaPixels-Macbook-Pro.local returns the error: Host ninjaPixels-Macbook-Pro.local not found: 3(NXDOMAIN)

This is an issue as part of our codebase at work needs to run the following python: socket.gethostbyname(socket.gethostname()) and it fails because my computer seems to have forgotten who it is.

I can successfully run ping localhost and ping 127.0.0.1

I’ve tried the following:

  • Rebooting.
  • Switching off the Firewall.
  • Changing my local hostname, using System Preferences > Sharing > Edit.
  • sudo killall -HUP mDNSResponder
  • dscacheutil -flushcache
  • sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
  • sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
6
  • out of left-field... I've seen similar issues if IPv6 was mapped to localhost in hosts, mapping ::1 127.0.0.1 instead seems to fix it.
    – Tetsujin
    Commented Sep 21, 2016 at 18:10
  • @Tetsujin Thanks. I just tried modifying hosts with that but sadly no cigar.
    – ninjaPixel
    Commented Sep 21, 2016 at 18:16
  • 1
    What Sharing do you have enabled, if any? You need to have some form of sharing enabled (or Service enabled under Sharing) in order to ping $HOSTNAME. On a clean install of macOS Sierra, Firewall turned off and nothing shared, I can replicate a ping $HOSTNAME error. It I turn on, e.g. File Sharing, I can ping $HOSTNAME from macOS and ping macOShostname.local from OS X on the same network. Commented Sep 21, 2016 at 18:58
  • @ninjaPixel Do you have a dns server in your network, what's the search domain (in SysPrefs > Network > Interface > DNS) and finally what's the hostname of your mac (e.g. ninjapixel.example.com) if any?
    – klanomath
    Commented Sep 21, 2016 at 19:20
  • @user3439894 Thank you, you solved it! I needed to enable some form of sharing. If you want to post that as an answer, I'll accept it. Cheers!
    – ninjaPixel
    Commented Sep 21, 2016 at 19:32

1 Answer 1

30

Assuming the Firewall is turned off, then in order to ping $HOSTNAME, there has to be some form of sharing or service enabled in System Preferences > Sharing, or add an entry to the /etc/hosts file, depending on how/what you're trying to access by $HOSTNAME. Example, 127.0.0.1 computer_name where computer_name is what's reported by echo $HOSTNAME or in lieu of 127.0.0.1 the actual IP Address of the Network Adapter on your system.

4
  • 1
    Adding 127.0.0.1 computer_name works for me. Commented Jun 4, 2017 at 17:05
  • 8
    After turning on File Sharing ping macOShostname.local started to work, even after turning File Sharing off. Commented Jul 10, 2017 at 22:44
  • 1
    that is so weird, it worked for me for like 2 weeks and stopped working out of now where .. changing my etc/hosts file worked fine as well ...
    – djnose
    Commented Nov 24, 2019 at 9:26
  • Adding 127.0.0.1 hostname works for me. Thanks. Commented Nov 12, 2021 at 13:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .