My Mac's hostname keeps adding a "-2" to the end

This is a known bug with the discoveryd process on 10.10 Yosemite. Recent rumors is that it will be replaced by its predecessor, mDNSresponder, come 10.10.4 to help alleviate network issues. Although the actual cause is unknown, it has been said that it is related (at least in part) to improper name resolution in DNS when the computer wakes from sleep. It sees itself as a duplicate on the network, and as such it appends the suffix to the title.

While there are some "fixes" YMMV - It's probably best to just keep renaming it the way you do, and then wait for the next release of OSX to come out and fix it so you don't sacrifice any functionality like Wake on LAN.

Source


That rename comes from any one other device on the network that remembered the IP address of your mac before it went to sleep or renewed its DHCP lease.

You would want to power down all Apple TV and Apple airport/time capsule devices and then restart your router (if an Airport device isn't doing the routing) at that point, the iOS and Mac devices should let you rename things again slightly more permanently than if you feel like you're battling an automatic useless machine.

It's a pain, and Apple had what it thought was a fix in 10.10.2 and 10.10.3 - we'll see if more patches help...

See:

  • My Mac mini's computer name keeps changing when it resumes from sleep
  • Prompt changed in my terminal

As other answers mention, this is a bug in Yosemite and will hopefully be gone when 10.10.4 releases. Until then, I have the following Keyboard Maestro action set up, which will automatically reset the system name whenever the system wakes up.

Mount Diskstation

Triggered by any of the following:

At System Wake

Will execute the following actions:

Shell Script

COMPUTER_NAME=Zephyr
sudo scutil --set ComputerName $COMPUTER_NAME
sudo scutil --set HostName $COMPUTER_NAME
sudo scutil --set LocalHostName $COMPUTER_NAME
sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string $COMPUTER_NAME

Display results in a window.

I also have the following in /etc/sudoers:

ats     ALL=(ALL) NOPASSWD: /usr/sbin/scutil --set ComputerName Zephyr, /usr/sbin/scutil --set LocalHostName Zephyr, /usr/sbin/scutil --set HostName Zephyr, /usr/bin/defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string Zephyr