Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

My computer name in System Preferences > sharing is set to "archos" but is showing as "iphone" on the 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$ 

Any ideas what could be causing this?

share|improve this question
    
what do you get when you type echo $HOSTNAME –  Daniel Lawson Nov 8 '11 at 6:14
1  
this is weird...I am getting 'archos.local' for echo $HOSTNAME. I did the initial writeup when I was at the office, but on my home network it is back to normal. I wonder if this has something to do with my work network? –  Travis Nelson Nov 8 '11 at 6:45

6 Answers 6

up vote 25 down vote accepted

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)

share|improve this answer
    
How can you undo that? –  Gio Dec 1 '11 at 18:45
4  
@Gio Delete the HostName key under System from /Library/Preferences/SystemConfiguration/preferences.plist. See this answer. –  ؘؘؘؘ May 9 '13 at 10:42
1  
@Gio: @Lauri's post has excellent background info, but if you want a shortcut: to delete, run sudo scutil --set HostName ''. –  mklement0 May 5 at 4:51
1  
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 5 at 5: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 6 at 21:40

You may also want to change some other things using the scutil command:

sudo scutil --set ComputerName "newname"
sudo scutil --set LocalHostName "newname"
sudo scutil --set HostName "newname"
share|improve this answer
    
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
    
@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 5 at 4:50

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: 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).

share|improve this answer
    
unfortunately, just setting those two didn't seem to be enough in my case: gist.github.com/rdp/71d5368087b94b5ea55a –  rogerdpack 2 days ago
    
@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 2 days ago
    
yeah scutil --get HostName HostName: not set /etc/hosts seems benign... –  rogerdpack 2 days ago
    
@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 2 days ago
    
doesn't seem to echo $PS1 \h:\W \u\$ my DNS system just be more messed up than is typical :) –  rogerdpack 2 days ago

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.
share|improve this answer

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

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

exit
share|improve this answer
    
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 5 at 20:51

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:

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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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