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?

2
  • 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)) Oct 7, 2012 at 18:30
  • @ThorbjørnRavnAndersen - my .local domain from the Sharing preferences has been scotts-macbook-pro.local
    – at01
    Oct 7, 2012 at 20:15

8 Answers 8

394

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.
4
  • 7
    Worked for me after running that command and rebooting Terminal. Jul 28, 2014 at 16:04
  • Worked as charm
    – mercury
    Mar 12, 2017 at 1: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, 2018 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 4, 2019 at 19:11
169

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.

8
  • 2
    @iconoclast Last used Windows primarily in 2004. Restarting worked for me and 16 other people. Wouldn't hurt.
    – Danger14
    Aug 13, 2014 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, 2014 at 19:32
  • 17
    Worked for me without restarting, just quit Terminal completely and re-open. Aug 20, 2014 at 13:06
  • 8
    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) Aug 28, 2014 at 8:13
  • 4
    You shouldn't even need to restart terminal to reflect the changes, just source the configuration responsible for your prompt. Jan 20, 2015 at 16:33
58

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

System Preferences -> Sharing -> Computer Name: 
7
  • 16
    This didn't work for me. The name at terminal command prompt still remained as localhost
    – timurb
    Sep 5, 2014 at 15:34
  • 7
    Did you restart the terminal?
    – Gon
    Mar 30, 2016 at 6:09
  • I defs had to restart Terminal, but this was the easiest method.
    – skybondsor
    Nov 15, 2017 at 17:47
  • This did not work for me because Terminal kept showing the old name. But, John's answer did solve it - on Mojave.
    – Rehmat
    Jul 10, 2019 at 15:09
  • 4
    This is the easiest and best solution. It just requires a restart once you're done Oct 10, 2020 at 11:31
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)
2
  • Why -1? He said that he cannot see its host name in the Terminal.
    – Maverik
    Oct 7, 2012 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 7, 2012 at 20:17
14

You can run

sudo hostname Name-Of-My-Computer

in Terminal to change the name.

3
  • 9
    The man page for hostname says to run scutil --set HostName name-of-host if you want to keep the hostname across reboots
    – daviewales
    Feb 8, 2013 at 5:30
  • For me it worked very well, even restarting the computer. Thank You. Nov 6, 2014 at 0:37
  • This worked for me after I restarted my Mac (running El Capitan) Dec 23, 2015 at 15:52
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.

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

Not the answer you're looking for? Browse other questions tagged .