Change your Mac Hostname via Terminal

Sep 6, 2010 - 28 Comments

Terminal in OS X For most people if you want to change your Mac computer name you just do it through the Sharing system preference, it’s quick and very easy. For those of us that are more geekishly inclined, we like to do things through the Terminal.

Here’s how to change your Mac hostname with the command line and make it permanent (well, permanent until you change it again):

sudo scutil –-set HostName new_hostname

Simply replace new_hostname with whatever you want your hostname to be changed to, for example I want to change a Mac’s hostname to MacBookPro, I will use this command:

sudo scutil –-set HostName MacBookPro

(Note the “–” before set is two dashes next to each other, –set)

You will be asked for your admin password since you’re using the sudo command.

After the command is executed you can verify that the changes took place by typing:

hostname

If you want to watch this done, the short video below walks through the steps by using scutil:

You can also set a temporary hostname change by using the following command:

sudo hostname new_hostname

This will reset itself after your Mac reboots though, so if you want a permanent hostname change, use the above command instead. Thank you to commenter Jim for pointing this out!

Note that in OS X Mavericks and newer, you can also use the hostname command with a flag to set the hostname to be permanently changed:

sudo hostname -s YourHostName

Again, sudo requires admin privileges to complete the job.

That’s all there is to it. By default Mac OS X will usually assign the hostname as whatever the admin account username is. Changing your Mac’s hostname can make it easier to find your Mac on a network and to connect to.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Manish Patel in Command Line, Mac OS X, Tips & Tricks

28 Comments

» Comments RSS Feed

  1. Arnan says:

    Keep in mind that this does NOT work for users who have an empty (no) password. Since sudo (or terminal in general) demands a password of at least 1 character.

  2. Jim says:

    With the command you give, the change will last only until the next reboot; it’s not permanent, and it doesn’t change the config files that need changing, under /Library/Preferences/SystemConfiguration.

    According to the man page for ‘hostname’, to make the change permanent — this is only for OS X client not server — you must run this command:

    scutil –set HostName name-of-host

    …where you replace ‘name-of-host’ with your new hostname.

    On OS X Server you would use the ‘changeip’ utility to update all the plists under /Library/Preferences and /Library/Preferences/SystemConfiguration. Despite its name, changeip can be used to change the server’s IP address, hostname, or both.

  3. George says:

    It’s “–set”, not “-set”.

  4. Josh The Geek says:

    This is really annoying. Every now and then, I open Terminal and see my hostname is wrong. I use apache and have many local test sites, and it is always changed to one of the site addresses. In console, I see ‘setting hostname to ‘macbook-pro.local”, and less than five seconds later, ‘setting hostname to ‘test1.drupal.localhost”, always by configd. What’s interesting is when firefox logs something about the same address, which I’ve not accessed in a month. ??? I use scutil –set HostName macbook-pro.local to reset it. This helps, now I wont need to. Thanks!

  5. […] command to do that is: scutil –set HostName YOURHOSTNAME (found info here: http://osxdaily.com/2010/09/06/change-your-mac-hostname-via-terminal/ – […]

  6. […] gefunden osxdaily.com. Danke Jim […]

  7. Paul says:

    I notice that my iMac sets its hostname to “eyeconnect (pauls-imac).home”. I want to know why an application which I deleted all traces of (or at least thought I did) is inserting its presence in my hostname? Can anyone explain it? It’s driving me nuts!

  8. rvn says:

    how can i remove localhost on my command line?

    • Bob Robillard says:

      set your PS1 variable (probably in .bashrc). Like:

      taffey:~ duke$ PS1=”>”
      >pwd
      /Users/duke
      >PS1=”CLI prompt: ”
      CLI prompt:
      CLI prompt:
      CLI prompt: PS1=”`hostname`:”

  9. Chrisb says:

    Useful!!! Thanks.

  10. It doesn’t work

    I wrote:
    “scutil -set HostName helplessnerd”

    And the only response I’ve got:
    “scutil: unknown, type “help” for command info”

    Can somebody help me?

  11. Philip says:

    Thanks, it was very useful

  12. Steve says:

    It does not work. What is wrong with Apple? It still says the wrong host name and I can’t seem to figure out how to change it. Any suggestions?

  13. Andy says:

    Thanks.. Very helpful mate

  14. […] appears locally, from file sharing and networking, and even Bonjour services, with the help of the scutil command. This allows you to have a custom hostname for Terminal and SSH, another friendlier name […]

  15. […] Mac OSX instructions for changing hostnameIf your hostname is not a valid URL endpoint, like […]

  16. Bogdan says:

    You can use:

    sudo scutil –-set HostName new_hostname

    to avoid the annoying security dialog and do everything in the command line. It will ask you for the admin login password in the command line.

    – Bogdan

  17. SV says:

    The people that said to use scutil were mostly right. I can vouch that for Mac OS X 10.6 and higher (Snow Leopard to Mavericks), that you need to use the scutil command three times. A reboot afterwards, wouldn’t be such a bad idea either (not absolutely necessary, but should flush the dns cache). Here is 2 methods:

    [ method 1 ]
    sudo scutil –-set HostName new_hostname
    sudo scutil –-set ComputerName new_hostname
    sudo scutil –-set LocalHostName new_hostname

    [ method 2 ]
    sudo -i
    for n in HostName ComputerName LocalHostName; do scutil –set $n new_hostname; done
    exit

    Note: new_hostname needs to be changed to the name you want to use!

  18. Larry McQueary says:

    Hi,

    I’d like to encourage the author to amend this article. Setting the hostname directly using “scutil –set HostName ” (it’s not set by default) can, and will, cause local hostname resolution problems for a lot of people (particularly home users).

    If you use this method to set HostName to something that does not end in “.local”, you should expect problems with processes that need to get the local hostname (gethostname(3)) to e.g. use its default network interface for something (gethostbyaddr(3)). For example, set up a local server.

    You can verify what I’m talking about by trying to “ping “. It may be that it works sometimes, but it won’t work reliably every time, and one day you’ll bang your head against the wall later trying to understand why it isn’t working.

    • Larry McQueary says:

      Looks like part of my post may have been interpreted as markup, and was elided.

      …You can verify what I’m talking about by trying to “ping new_hostname_not_ending_in_dot_local”. It may be…

    • Pal says:

      Interesting, I have set the hostname with this method many times and have never had a problem with packet loss or otherwise. Are you trying to ping localhost or what? You may be experiencing other networking issues unrelated to your local computers hostname change.

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates