315

When I setup my computer I named it "Scott's MacBook Pro". And so when I go to System preferences->Sharing, it says Scott's MacBook Pro there as my computer name. However, opening up a terminal gives me this prompt:

Last login: Sun Oct  7 11:02:49 on ttys003
new-host-4:~ scott$ 

When I login to Backblaze, they list my computer as "new‑host‑4_2012_10_07". Did I not fully set my computer name? Should I use the traditional hostname command?

at01
  • 5,595
  • 2
    To my knowledge this is what DNS reports it to be. If your IP-number does not have a name, the name from Preferences is used (perhaps the .local domain)) – Thorbjørn Ravn Andersen Oct 07 '12 at 18:30
  • @ThorbjørnRavnAndersen - my .local domain from the Sharing preferences has been scotts-macbook-pro.local – at01 Oct 07 '12 at 20:15

8 Answers8

396

If you use:

sudo scutil --set HostName name-you-want

it will work a bit better. From the scutil(8) man page:

--get pref
    Retrieves the specified preference.  The current value will be
    reported on standard output.
Supported preferences include:
      ComputerName   The user-friendly name for the system.
      LocalHostName  The local (Bonjour) host name.
      HostName       The name associated with hostname(1) and gethostname(3).

--set pref [newval] Updates the specified preference with the new value. If the new value is not specified on the command line then it will be read from standard input.

Supported preferences include: ComputerName LocalHostName HostName

The --set option requires super-user access.

John
  • 3,961
  • 7
    Worked for me after running that command and rebooting Terminal. – Johnathan Elmore Jul 28 '14 at 16:04
  • Worked as charm – mercury Mar 12 '17 at 01:56
  • This works for me, but upon rebooting, it then resets the hostname in the terminal back to what it was prior to me changing it. – jwir3 Oct 10 '18 at 15:56
  • Works in Mojave. The terminal was showing emulator02 even though I changed it in settings -- or tried. I just ran all three supported preferences and restarted terminal; now I can get the hostname right. – Daniel Feb 04 '19 at 19:11
170

After following Jeffrey J. Hoover's tip,

sudo scutil --set ComputerName "newname"
sudo scutil --set LocalHostName "newname"
sudo scutil --set HostName "newname"

I would add these last two steps.

  • Flush the DNS cache by typing: dscacheutil -flushcache
  • Restart your Mac.

EDIT: It didn't work for me until I restarted my mac.

Danger14
  • 1,999
  • 1
    Are you a former Windows user, carrying over the practice of rebooting after every little change, or do you actually have evidence that it's necessary to restart? – iconoclast Aug 12 '14 at 19:37
  • 2
    @iconoclast Last used Windows primarily in 2004. Restarting worked for me and 16 other people. Wouldn't hurt. – Danger14 Aug 13 '14 at 19:31
  • 10
    Meaning it didn't work until you restarted? (Restarting *can* hurt when you actually do serious work on your computer, and therefore have lots of applications open. Getting everything back how you had it before reboot can be a *****.) Also, the fact that 16 people found some part of your answer helpful doesn't mean nothing worked until they restarted. – iconoclast Aug 13 '14 at 19:32
  • Yes, it didn't work until I restarted. Thanks for the info; didn't know it can be harmful. – Danger14 Aug 13 '14 at 19:34
  • 17
    Worked for me without restarting, just quit Terminal completely and re-open. – Alexander Wigmore Aug 20 '14 at 13:06
  • 9
    I first used scutil --get to discover that ComputerName and LocalHostName had been correctly set by using System Preferences (Sharing). However, it told me that HostName was not set. Thus, I just used sudo scutil --set in order to set HostName. Then, I didn't need to use dscacheutil or do a reboot as immediately a check of hostname showed that things were fixed for me and opening a new Terminal tab showed my prompt was now fixed too. (OS X 10.9.4) – Quintin Willison Aug 28 '14 at 08:13
  • 4
    You shouldn't even need to restart terminal to reflect the changes, just source the configuration responsible for your prompt. – Steve Buzonas Jan 20 '15 at 16:33
  • 1
    Why should we flush the DNS cache? – Mark A Sep 24 '18 at 02:21
58

For those who are not looking for a command line solution, you can change it under

System Preferences -> Sharing -> Computer Name: 
27

You can define what you want to see before the $ in your terminal by modifying the file ~/.profile.

For example if you add to the file ~/.profile the following line:

# h is the host name, w the complete path 
export PS1="\h:\w$ "

you will see the host name and the complete path of the current directory:

host_name:current_directory_path$

You can also modify my example by using the following options in the export command:

\d – Current date
\t – Current time
\h – Host name
\# – Command number
\u – User name
\W – Current working directory (i.e: Desktop/)
\w – Current working directory, full path (i.e: /Users/Admin/Desktop)
Maverik
  • 5,829
  • 6
  • 50
  • 55
  • Why -1? He said that he cannot see its host name in the Terminal. – Maverik Oct 07 '12 at 18:38
  • 4
    wasn't me with the -1 :). Thank you for your answer, I guess I'm not just looking for how to change my name in the terminal, but clearly my computer is still referenced by the new-host-4 ugly name as services like Backblaze use it. My previous MacBook Pro had a nicer name that I chose, I don't know why my mountain lion MacBook Pro isn't using the name I have in the Sharing preferences – at01 Oct 07 '12 at 20:17
14

You can run

sudo hostname Name-Of-My-Computer

in Terminal to change the name.

nohillside
  • 100,768
Galas
  • 141
7

In my case, I found that it was a problem with our DNS server, as the reverse DNS lookup didn't scavenge because there was duplicate DNS entries for the IP address and hostname.

Ethan Lee
  • 3,994
2

I didn't do anything to my computer, but eventually for some reason the hostname and computer name all registered as the name I initially set! In fact I had the following terminal open the whole time and this is exactly a cut and paste:

new-host-4:~ scott$ hostname
new-host-4.home
new-host-4:~ scott$ hostname
Scotts-MacBook-Pro.local
at01
  • 5,595
2

I am adding an answer here because I have recently run into this problem as well.

In OSX 10.7.4 and possibly prior, there appears to be a bug in the Edit Hostname option in the Sharing preference panel. It sets the hostname to the last thing set instead of the current this set.

I had to run sudo hostname [preferred hostname] in order to set it correctly, after setting it in Sharing.

bassplayer7
  • 14,106
JDL
  • 121
  • 1