Change your Mac Hostname via Terminal
Need to change the hostname of a Mac? 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.
This tutorial will show how to change your Mac hostname with the command line and make the setting change permanent (well, permanent until you change it again):
How to Change Host Name on Mac from the Command Line
To get started, launch the Terminal application in Mac OS and then use the following command syntax:
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.
Another Method for Setting the Mac Hostname
With modern MacOS releases from Mac 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.
Checking the Current Mac Hostname from the Command Line
After the above 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:
Setting a Temporary Hostname Change
You can also set a temporary hostname change by using the following command:
sudo hostname new_hostname
This particular approach is temporary and will reset itself after your Mac reboots though, so if you want a permanent hostname change, use the above command instead. You can still use the hostname command but it will require the -s flag as discussed in the aforementioned instructions. Thank you to commenter Jim for pointing this out!
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.
You lost me with the prompt method because I am not a geek. I changed my name from what it used to be to the current. At that time, I named my computer after my name, “my first and last name’s computer”. Recently, I changed my last name and need to change it all through. My question: How do I make this change of name both in my Yahoo/Gmail/hotmail and the computer itself?
Thanks.
Thanks for this, it worked! I am wondering how I might change the part where, in your demo, it says OSXDaily after the host name?
In a Windows network, when a computer gets an address from DHCP the computer’s configured hostname is entered into the DNS with A and PTR (reverse DNS) lookup. What I have observed is that when an OSX machine gets an IP address via DHCP it will do a reverse DNS lookup on that IP address and set the hostname to whatever the DNS server returns.
The DHCP service is supposed to clean up when IP addresses are reassigned, but DNS still tends to get polluted with old PTR records. I cope with this by configuring aggressive timings on DNS record scavanging. It doesn’t fix the problem entirely, but I find it reduces these occurences dramaticaly.
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.
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…
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.
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!
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
[…] Mac OSX instructions for changing hostnameIf your hostname is not a valid URL endpoint, like […]
[…] 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 […]
Thanks.. Very helpful mate
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?
change the host name in /etc/hostconfig
that’s the onlything that worked for me.
Thanks, it was very useful
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?
Make sure you use a double dash like — for set flag:
scutil –set HostName “helpnessnerd”
Thank you! but I still get the same response.
Really confusing…
I think when you re-start the terminal, you will see your changes. :)
You need to use this command
sudo scutil –set HostName newhostname
Useful!!! Thanks.
how can i remove localhost on my command line?
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`:”
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!
[…] gefunden osxdaily.com. Danke Jim […]
[…] command to do that is: scutil –set HostName YOURHOSTNAME (found info here: https://osxdaily.com/2010/09/06/change-your-mac-hostname-via-terminal/ – […]
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!
It’s “–set”, not “-set”.
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.
Thank you for pointing this out Jim, I adjusted the article accordingly.
-Manish
Hi Manish,
Just to not, it’s suppose to be “–set” with the double dash there. Not sure if the — will be converted, but here’s the one with spaces in between “- – set”.
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.