Change the hostname in Mac OS X [osx]

When I log into the network at my job my Mac’s hostname always turns to:

I have my local hostname set to:


So What I would like to do is set my Mac’s hostname to my local hostname. You can do this all from Terminal in a single line.

Run this command in Terminal:

sudo scutil --set HostName larryx.local

This is also helpful if you’re in Terminal and have a really long hostname at your prompt. If you want to view your current hostname, run this command in Terminal:


Update (February 13, 2012): Some people have reported that their hostname is not updating. Please try closing your current Terminal session and starting up a new one. Then type “hostname” and you should see your changes.


This is what it looked like for me:

Last login: Fri May 23 09:55:20 on ttys000
larryx:~ lgordon$ hostname
larryx:~ lgordon$ sudo scutil --set HostName larryx.local
larryx:~ lgordon$ hostname

Care of: CodeSnippets

Update (March 29, 2011): After making this post I never thought that this would have helped so many people out there. I’m really glad that this has helped you and I thank you for your kind words.

May 23, 2008
2:56 pm
Tags: , ,

  • suredazzle


    How are you? I am trying to set up on Mac similiar to your.


    etc/hosts don’t change this entry. echo $HOSTNAME. It returns melissa-imac.local.

    No HOSTNAME file in etc/.

    Where should I backup or begin?


  • Pingback: Just wondering…. » Blog Archive » MacOSX and hostname()

  • HX

    Nice tip, thanks. I was looking to change hostnames of my MacBook and iPhone. Turns out it works on both.

  • Lisa

    Thanks! Apple’s support site was no help!

    • It’s really strange that Apple’s support site has no info on such a small but important topic

  • Dom

    For some reason the command suggested doesn’t work at all for me:

    [dpo@pod:~ ]$ hostname
    [dpo@pod:~ ]$ sudo scutil –set HostName rocky
    [dpo@pod:~ ]$ sudo scutil –get HostName
    [dpo@pod:~ ]$ hostname

    Any ideas? I am running 10.5.8. I have to change the hostname from time to time to make some grumpy license managers happy. Editing /etc/hostconfig works but is less elegant.

    • Unacoder

      the hostname on your current console will not change, you need to close the console and open a new one.

      Also, I changed all three values: ComputerName, HostName, LocalHostName

  • Ace

    Dom, make sure you use double dashes, e.g. “–set” when you enter the

    “Sudo scutil –set HostName” command.

  • How ya doing? You picked the ones closed to my heart too, despite the fact I know I am guilty of #1 when I don’t pay attention.

  • Tore

    Tahnks a lot, your solution really works, it is the only place I found where it says how to do it, apple solution doesn’t work.

  • Stripes

    This is awesome! It works perfect for me. As much as I love my Mac, there are some things that I wish were more straightforward UNIX-y.

  • Great! It worked well, thank you.

    Notice that if you change the hostname this way you may need to restart (or re-log into) the machine because the internet / lan may go down. It seems that bonjour or other name-resolving services can’t figure out that the hostname is changed and don’t update the name.


  • Pingback: Bits And Pix » OSX: Set Mac hostname (i.e. avoid perforce workspace issues)()

  • Pingback: Trocando o Hostname no Mac OS | @Italux()

  • Dieter

    Thank you for the post.
    My computer name was greyed out so I used this command.
    It worked,
    However, I had to use ComputerName instead of HostName.
    Thank you again.

  • Dieter

    Thank you for the post.
    My computer name was greyed out so I used this command.
    It worked,
    However, I had to use ComputerName instead of HostName.
    Thank you again.

  • Pingback: links for 2011-01-12 « Midashuang's Blog()

  • Johndo AsIplease

    thank you, worked like a charm on osx 10.6, have my mbp on a domin, make sure to use the same path “example.domain.local” or whatever or you could eff up the link with AD.

  • Thanks,it works.

  • Chilzap

    Thanks, It just works now.

  • Kenny

    Thanks.. I have been trying to join my AD and all other posts I have read failed.. Found yours and Voila’ worked like a charm.. 🙂

  • Pingback: Fix XAMPP HostnameError001 on Mac OS X « Larry Gordon()

  • Thank you! I had been searching for it for a while now.

  • Wow

    Goto System Preferences ->Sharing ->Web Sharing – below Computer Name, hit Edit… ur done

    • Something

      no dude, his procedure is for the “hostname”, not the computer name. I don’t know why these are separate it seems. 

      • c00lryguy

        Yeah, it changes the hostname. I just did it on Lion. Works fine.

        • I just tried this on an iMac running Lion.  It did not work for me.  hostname still reports the name of the host that I migrated from (using Migration Assistant).  Any ideas?

          • Thekostya

            Just run  new session in terminal.

          • Dude

            quit and relaunch your terminal session

          • Hunk

            restart the terminal

          • anonymous

            STFU, you ungrateful, aholes!

        • No, doesn’t work for me, either on Lion or (Snow) Leopard

        • Charlie Jennings

          Just because you did something and it works it doesn’t mean it’s going to work for all.

    • Charlie Jennings

      Some people are fucktards

  • Elopez

    Thank you man! I was looking exactly for this!

  • MisterGrim

    Thank you sir, worked like a charm!

  • works for me on snow leopard. Quick and easy. Thanks a lot!

  • anatoly

    Thank you 🙂 Still works like a charm on OSX Lion.

  • Very nice… thanks for putting this out there… very easy to do and easy to find via google too! 

  • Hi Larry,

    Thanks for posting this… such a simple thing… but so useful!

    Much appreciated!

  • Jnunez

    Man this worked great. After the hostname change, log out of terminal and log back in, you should see that the hostname has changed in the terminal command line as well. 

  • Thanks for that reminder, I had forgotten how to use scutil. If all you wanted was a shorter hostname in the bash shell, you could also have changed the bash prompt to show only the first part of the hostname, up to the first point. See here for an excellent explanation:

  • Jason Hernandez

    Thank you for the info! Now I can contribute a FIX back to the community

    I have been having an issue since I upgraded my box to OSX 10.7 Lion – It periodically “falls off” the Windows AD Domain. The most visible effect of this is that when the screen saver with a password kicks in, you cannot unlock the screen. Also, when you are doing things that require admin privs, the password doesn’t work. Seriously aggravating.

    – Set the hostname manually to the fully qualified domain name
      – Run this magic command from the Terminal (command line): sudo scutil –set HostName InsertYourHostnamehere
    – Reboot

    That’s seriously all you have to do. But what a P.I.T.A. to figure out.

    Related reference:

  • Tduncalf

    Awesome, thanks! Mine kept defaulting back to (null) for some reason.

  • open terminal and type 
    $sudo hostname “nameofyournmachine”

    • Pfc. Parts

      But this only works until you reboot the machine. Larry’s solution is mentioned in the man page for hostname:

      HOSTNAME(1) BSD General Commands Manual HOSTNAME(1)

      hostname — set or print name of current host system

      hostname [-fs] [name-of-host]

      The hostname utility prints the name of the current host. The super-user can set the hostname by supplying an argument. To keep the hostname between reboots, run `scutil –set HostName name-of-host’.

  • nate5765

    if i change the hostname, can it harm my cpu in any way? also, is this the same as dns?
    so like if I have a minecraft multiplayer server running on my cpu, if i change the hostname can they use that to log in instead of my ip?

  • Jesper

    Worked fine for me on Lion. Thank you very much!

  • Elzorro144

    it should be noted that once you enter those above commands you have to RESTART terminal or open a new window before the changes are shown.

  • Cuongnt

    Very good

  • Pingback: Hack your Mac | Pearltrees()

  • Montgomery Newcom

    In addition to HostName, LocalHostName and ComputerName can be changed.  LocalHostName will change the NetBOIS name.  Presumably, ComputerName is used somewhere, so it should probably be changed too.  It might be used on the login screen.

  • This worked for me on Lion:
    sudo hostname

  • Helped me a lot. Thanks!

  • Thanks!

  • Pfc. Parts

    Thanks Larry, I’ve been beating my head on the wall for a few days, wish I’d found this trick earlier.

    I brought up a DNS/BIND caching forwarding server on my mini a few weeks ago and all the sudden my cshell prompts started displaying “ns~:” at the prompt instead of the machine’s given name (alphonse). I tried everything I could think of in /var/named, deleted the “ns” alias, re-ordered the entries, I was tearing hair.

    I guess somewhere this got cached and I couldn’t change it without the scutil hint. Bingo.

  • Rahul

    thank you very much, my computer name keeps changing and no one has been able to given a fix for that, this lets me get my name back, thanks.

  • kevinwang2

    Thanks so much! I had exactly the opposite issue: my hostname mysteriously changed to something.local, which led to all my mails sent from system mail program rejected in the past two days without me knowing…

  • Trane Francks

    This has been an annoyance any time I reboot my MacBook when I’m on a client network. Nice to stick this into a shell script that I can quickly call to set things right. Big thumbs up!

  • glenncogar

    Thanks! This helped me fix a problem with my Mac’s host name and Apache not starting 🙂

  • Ahmed T

    still works on 10.11 after all these years, I think Cupertino is wise enough to confine the new morons to GUI