169

My computer name in System Preferences > Sharing is set to "archos", but is showing as "iphone" on Terminal. It just started doing this after I loaded Xcode for doing iPhone development:

Last login: Mon Nov  7 14:46:55 on ttys001
iphone:~ travis$ echo $HOSTNAME
archos.local
iphone:~ travis$ 

Any ideas what could be causing this?

bmike
  • 235,889

10 Answers10

132

It's perfectly normal for this to occur; when you login Terminal remotely bash does a reverse DNS lookup. It will only be the same if the hostname is not specified on the network you're connecting from and there is no reply from the DHCP server, or the reverse lookup against the remote DNS server fails to resolve.

You can easily over-ride the default setting by using this command in Terminal:

sudo scutil --set HostName archos

You can check it by using:

nslookup nn.nn.nn.nn

( or )

host nn.nn.nn.nn

(where nn signifies your Mac's ip address)

mmmmmm
  • 30,160
l'L'l
  • 9,105
  • How can you undo that? – Gio Dec 01 '11 at 18:45
  • 9
    @Gio Delete the HostName key under System from /Library/Preferences/SystemConfiguration/preferences.plist. See this answer. – Lri May 09 '13 at 10:42
  • 5
    @Gio: @Lauri's post has excellent background info, but if you want a shortcut: to delete, run sudo scutil --set HostName ''. – mklement0 May 05 '14 at 04:51
  • 6
    Setting HostName is not advisable, because it creates a separate value that then persists even when you later try to change the hostname via System Preferences > Sharing. By contrast, setting LocalHostName updates the same value, and is normally also picked up by the shell and hostname (the ComputerName value should be updated in tandem). Or does setting HostName behave differently in a way that is advantageous here? – mklement0 May 05 '14 at 05:00
  • My fix for this was to remove a dhcp entry on my router and then reboot (the router). It had an old name for my mac cached (well, actually I had a fixed assignment in the dhcp settings)

    After the router rebooted my mac apparent hostname (in bash prompt for e.g.) matches the one I set in Preferences->Sharing and I can see the new name registered in the dhcp display on the router.

    PS. This difference only happened since installing Yosemite. It is a sensible change but one that will confuse people.

    – Matthew Hannigan Nov 06 '14 at 21:40
  • I believe you will want to set your HostName to match your System Preferences > Sharing > "Computers on your local network can access your computer at: XXX.local" name (ex: --set HostName 'XXX.local'), otherwise some reverse lookups may fail. – rogerdpack Dec 19 '14 at 22:22
  • 2
    another to test the host name is hostname -f – ericn Aug 26 '15 at 03:08
  • 3
    I had this difficulty on 2 machines and the ideas proposed in the answer by J.Hoover below, and in the comment by by mklement0 above, did not work. Namely, you are both advising that one only change the LocalHostName and the ComputerName, however, changing these never changed the Terminal to reflect the Computer Name specified in the System Preferences. I believe the reason for this, in my case, is that both machines were Time Machine backups and retained the locally stored credentials from the old image despite me renaming at a later date. Only HostName worked for me. FYI to others. – oemb1905 Dec 23 '15 at 23:06
  • 1
    @mklement0 I'm not sure what all variables are at play here, but I ended up at this issue in 2021 using macOS 11.1 because my LocalHostName was set to something sensible, my HostName was "not set" (I checked), and this was causing the terminal to do the reverse lookup mentioned in this answer and report random other devices on my local network as the host name in my terminal prompt. Explicitly setting HostName fixed it. I'd love to have one setting that just works everywhere, but unsetting HostName apparently ain't that. – GrandOpener Jan 05 '21 at 21:51
77

There are some helpful comments below this answer that you might want to read first.

Go to System Preferences -> Sharing and update your Computer Name.

Then in Terminal, you can view settings via:

sudo scutil --get ComputerName
sudo scutil --get LocalHostName
sudo scutil --get HostName

You can also change the values using the scutil command:

sudo scutil --set ComputerName "newname"
sudo scutil --set LocalHostName "newname"
sudo scutil --set HostName "newname"
bmike
  • 235,889
  • 6
    By changing the name in System Preferences->Sharing and then using scutil to set HostName, both ComputerName and LocalHostName were already set to the new name for me (in Mavericks). Nice to know that these other settings exist, though. – Jim Stewart Nov 15 '13 at 19:02
  • 3
    @JimStewart: Indeed, that's why it's better to NEVER set HostName, because doing so will make that value persist even when you later try to change your hostname via System Preferences. Conversely, the LocalHostName is picked up by the shell and hostname, so HostName is never needed. – mklement0 May 05 '14 at 04:50
  • In my case I had an incorrect LocalHostName value. Running sudo scutil --set LocalHostName '' deleted LocalHostName and now my prompt is using ComputerName, which has the correct value. – mark Jan 31 '17 at 01:41
17

There are two potential reasons for the shell ($HOSTNAME variable, '\h' in $PS1) and the hostname utility to report a hostname that differs from the one shown in System Preferences > Sharing:

  • sudo scutil --set HostName newName was used to persistently change the hostname - unfortunately, this value is independent of the LocalHostName value set by System Preferences behind the scenes - even though man hostname suggests this method, DO NOT USE IT for that reason; run sudo scutil --set HostName '' to remove it, at which point LocalHostName should again be reported; for background, see this post by @Lauri Ranta.

  • [Update: The following is no longer true as of OSX 10.10] If your /etc/hosts file has an entry that matches your machine's IP address, that entry's name will be reported.

Only if neither of the above is true will the shell and hostname reflect the value set via System Preferences.


If you want to change your hostname from the shell, run both of the following:

  • sudo scutil --set LocalHostName <newName>
  • sudo scutil --set ComputerName <newFriendlyName>

where <newFriendlyName> can be a friendlier version (spaces, punctuation) of <newName> (alphanumerics and dashes only).

mklement0
  • 1,065
  • unfortunately, just setting those two didn't seem to be enough in my case: https://gist.github.com/rdp/71d5368087b94b5ea55a – rogerdpack Dec 19 '14 at 17:47
  • @rogerdpack: And you've run sudo scutil --set HostName '' first (case matters) and made sure that there's no entry in /etc/hosts? scutil --get HostName should report Hostname: not set. – mklement0 Dec 19 '14 at 18:03
  • yeah scutil --get HostName HostName: not set /etc/hosts seems benign... – rogerdpack Dec 19 '14 at 19:39
  • 1
    @rogerdpack: Silly question: your PS1 variable doesn't happen to have a hard-coded hostname rather than using \h, does it? As an aside: /etc/hosts is no longer consulted as of OSX 10.10 (I've updated my answer). – mklement0 Dec 19 '14 at 20:15
  • doesn't seem to echo $PS1 \h:\W \u\$ my DNS system just be more messed up than is typical :) – rogerdpack Dec 19 '14 at 20:30
  • 2
    Beautiful. Removing the hostname (by setting it to the empty string as you suggested: sudo scutil --set HostName '') again allows OS X to map the computer name to its IP address (e.g., this now works ping <computername>). – Brent Faust Apr 22 '15 at 22:10
8

In addition to Jeffrey J. Hoover's tip (above),

I would add these last two steps:

  • Flush the DNS cache by typing: dscacheutil -flushcache
  • Restart your Mac.
Danger14
  • 1,999
7

I agree that Jeffrey Hoover's tip was the correct answer. Flushing the DNS cache and restarting the Mac is a nice step too. This is variant of Jeff Hoover's reply:

sudo -i
Password:

for n in HostName ComputerName LocalHostName; do
    scutil --set $n new_hostname
done

exit
n8felton
  • 1,014
  • 2
    You're missing a -: it must be --set to work. Also, as I commented at Jeff's answer: it's probably better to not set HostName. – mklement0 May 05 '14 at 20:51
5

System Preferences -> Sharing -> Computer Name --> set it to whatever

And then restart your computer. No console commands were necessary. Works for OS X 10.10.2

Terry Bu
  • 151
  • 1
    On 10.14.6, I had a situation where the Sharing -> Computer Name was set the way I wanted it yet hostname returned "MacBook-Pro". I even changed it and still had the problem. I've never seen this issue before. The scutil commands worked for me. I'm wondering where / how you are suppose to do that via the GUI interface. – pedz Oct 02 '19 at 16:02
5

I had the problem that only the shell itself (zsh in my case) failed to show the correct computer name. Simply restarting the shell solved it:

exec /bin/zsh
5

Use this

sudo systemsetup -setcomputername $name
Quanlong
  • 1,861
4

As mentioned in a separate answer, another option is to override the default PS1 prompt, like executing the following line (or adding it to your ~/.bash_profile file):

 $ PS1="$(scutil --get ComputerName):\W \u\\$ "

refs:

https://superuser.com/a/414903/39364

http://www.cnet.com/news/keep-your-command-prompt-host-name-static-in-os-x-and-other-unix-systems/

3

In addition to the previous answer, remember that you can use a static address in your router. You have to enter the static IP and the MAC address of your network card, then you set a name for it. The hostname will be automagically updated to the specified name. That's it.

noun
  • 131