What is hostname in terminal prompt? [duplicate]
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$
Any ideas what could be causing this?
Solution 1:
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)
Solution 2:
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"
Solution 3:
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 theLocalHostName
value set by System Preferences behind the scenes - even thoughman hostname
suggests this method, DO NOT USE IT for that reason; runsudo scutil --set HostName ''
to remove it, at which pointLocalHostName
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).