41

When I open Terminal I expect it to show what my PS1 is set to \h:\W \u\$.

However it's not displaying my hostname Eriks-MacBook, as displayed in System Preferences.

Instead, it's showing a random string unknownb88d120cd4b2.

How can this be fixed or reset?

This machine is brand new, and the preferences/settings haven't been changed.

p.campbell
  • 4,478
Erik W
  • 513

7 Answers7

29

Gordon gives a good answer to the origin of your hostname.

If you want the name that you set in System Preferences -> Sharing -> Computer Name to show up in your prompt, replace \h with $(scutil --get ComputerName). E.g my prompt is set with

PS1="[\u@:$(scutil --get ComputerName) \W]\\$ "
ajs
  • 306
  • 3
  • 3
27

Terminal is showing you the first label of your BSD hostname (assuming your shell is BASH). If your BSD hostname is yourhostname.mynetwork.com then Terminal will display only yourhostname-

So from where does the BSD hostname come? It can come from several places:

• from the file: /etc/hostconfig

• else from the file: /Library/Preferences/SystemConfiguration/preferences.plist (System ▸ System ▸ HostName)

• else the result of a reverse DNS query for your primary IP address (so you might notice a totally different hostname showing up when you visit an internet café than when connected at home)

• else your "Bonjour" hostname in System Preferences > Sharing (preferences.plist again... System ▸ Network ▸ HostNames ▸ LocalHostName)

• finally, if none of the above have been set, the BSD hostname will be simply localhost

By the way, I answered the same question a while ago over here: Mac OS X Terminal - where does the prompt name come from

username
  • 1,428
14

OS X tries a number of things to find its "hostname". Unfortunately I don't know the exact list (and order), but I think what's happening here is that it's discovering a DNS name associated with its IP address, and using that instead of the Bonjour name it's advertising for itself (the one defined in Sharing preferences).

So why's it finding a DNS name? My guess is that your local router/DHCP server/DNS server is dynamically assigning it one, based on its ethernet hardware address (presumably b8:8d:12:0c:d4:b2). You may be able to adjust the router's settings to stop it doing this, or you could probably set the computer's DHCP client ID (in the Network Preferences, Advanced settings) to control what name it assigns you.

6

For me:

  • scutil --get HostName was empty,
  • /etc/hosts was fine,
  • /etc/hostconfig didn't exist,
  • the Bonjour name was right,
  • all the preference files were right, but hostname would stubbornly use a default name (iMac in my case).

Turns out the bad name was associated with the DHCP lease on the ethernet interface (but not the WiFi interface). To convince the DHCP lease to use the new name, I had to:

  1. Go to System Preferences --> Network --> Ethernet and change the "Configure IPv4" drop down to "Off".
  2. Click "Apply".
  3. Click "Advanced", then select the WINS tab.
  4. Change the NetBIOS Name (which showed the old name) to the new name.
  5. Click "OK", then "Apply".
  6. Finally, change "Off" back to "Using DHCP".

Once the new lease was established, but the router and the local machine reported the new name and there was no sign of the old any more.

A bad DHCP lease was definitely the culprit, but fixing the NetBIOS Name before renewing the lease might have been the clincher.

3

Not sure if you are still having problems or not, this appears to have been fixed sometime between Yosemite and Sierra.

For new computers I start with the Sharing System Preferences, entering the [new name] as the Computer Name desired. I also enter the [new name] in all of the active DHCP Client ID fields for each network interface. After I've done that I open Terminal to confirm it reports a similar name, some characters are not allowed so the system will modify the name if necessary. You can change the name using the steps below, just replace the [new name] block with your desired computer name.

  1. Open Terminal
  2. At the prompt enter the following- sudo scutil --set HostName [new name]
  3. Open a new Terminal window to confirm the new name was accepted.

If the name is not to my liking I proceed with the above before binding it to Active Domain.

1

In my case I fixed this by quitting terminal then renewing my DHCP lease.

Apple menu -> System Prefs -> Network -> WiFi (or Ethernet) -> Advanced -> TCP/IP tab -> Renew DHCP Lease

Restarted terminal and the prompt was back to normal.

Martin CR
  • 119
1

I had the same proble and I just used this command scutil --set HostName "name"