Change the hostname in Mac OS X [osx]

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

larryx.na.corp.ipgnetwork.com

I have my local hostname set to:

larryx.local

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:

hostname

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.na.corp.ipgnetwork.com
larryx:~ lgordon$ sudo scutil --set HostName larryx.local
Password:
larryx:~ lgordon$ hostname
larryx.local

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.

Date
May 23, 2008
Time
2:56 pm
Category
Uncategorized
Tags
Tags: , ,


  • suredazzle

    Hi,

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

    Hostname
    melissa.na.corp.abcxyz.com

    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?

    Thanks,
    suredazzle

  • 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!

    • http://www.resumewritingservice.biz/ resume writing

      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
    pod.local
    [dpo@pod:~ ]$ sudo scutil –set HostName rocky
    Password:
    [dpo@pod:~ ]$ sudo scutil –get HostName
    rocky
    [dpo@pod:~ ]$ hostname
    pod.local

    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.

  • http://www.wordpressmassinstaller.com Demarcus Fanner

    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.

  • http://makevoid.com Francesco

    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.

    Bye!

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

  • http://www.digglife.cn digglife

    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

  • http://profiles.google.com/pravinp Pravin Paratey

    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.

        • http://www.facebook.com/profile.php?id=1110162736 Rob Redmon

          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

        • http://www.lamnk.com Lamnk

          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!

  • http://www.rare-earth-tech.com Rajeev J

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

  • anatoly

    Thank you :) Still works like a charm on OSX Lion.

  • http://jfgrissom.myopenid.com/ JFGrissom

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

  • http://jfgrissom.myopenid.com/ JFGrissom

    Hi Larry,

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

    Much appreciated!
    Jay

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

  • http://www.oriste.com/ oriste

    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: http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html

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

    Fix:
    - 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: http://forums.macrumors.com/archive/index.php/t-1188443.html

  • Tduncalf

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

  • http://www.facebook.com/ezequiel.pitty Ezequiel Pitty

    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)

      NAME
      hostname — set or print name of current host system

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

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

  • http://www.robv.net Rob V

    This worked for me on Lion:
    sudo hostname

  • http://www.SNTMNT.com/ Vincent van Leeuwen

    Helped me a lot. Thanks!

  • http://www.facebook.com/people/Raul-Lopez/1498327244 Raul Lopez

    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.

  • http://www.professionalthesiswriters.com/ professional thesis writers

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

  • 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

  • LG

    How did you change all three values?  What were the commands you ran?  Thanks!