Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Change your hostname from 'localhost' UNIX
If you're like me, you've been terribly frustrated by the fact that "hostname name" doesn't permanently set the hostname under MacOS X. After a lot of (unsuccessful) poking around, I finally found a note on the macosx-admin group hosted by

Edit /etc/hostconfig with your favorite editor. Change the line



HOSTNAME="your favorite name"

Works like a charm! Still, it bugs me that Apple has disabled many of the old unix standards - I think they'd make the platform much more attractive to a very strong unix talent base if they would try to keep the old standards where they still make sense.


  • Currently 3.00 / 5
  You rated: 5 / 5 (2 votes cast)

Change your hostname from 'localhost' | 8 comments | Create New Account
Click here to return to the 'Change your hostname from 'localhost'' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
tried this...
Authored by: inajar on May 28, '01 04:39:24PM

I tried to set the hostname on my machine like this a few different times in the past two months. It never worked for me. I did find something that did work though.

If you look in the startup items in the system folder for OS X you'll see a Network startup item. If you edit the hostname line in this startup item it will always be correct in the terminal. I haven't had any problems with this method either, like apache not starting correctly etc. Admittedly it would be nicer to find the place where te Network startup item is pulling the hostname variable from and change it there, but I haven't had the time.


[ Reply to This | # ]
Re: tried this...
Authored by: blb on May 28, '01 05:06:20PM

I did look a little into this, and what I found is that it uses ipconfig (and the daemon, ipconfigd) to check for a hostname (the exact line is,

ipconfig getoption "" host_name

which, when I run (as root, so add sudo in front if necessary) is that there's nothing. When this happens in the Network startup script, it then falls back to localhost.
The big question, then, is how do you tell ipconfigd to setup a host_name entry? I have yet to stumble onto any docs for ipconfig/ipconfigd.

[ Reply to This | # ]
Watch out for missing prefs if you change your hostname
Authored by: a1291762 on May 28, '01 06:32:30PM

If you look in ~/Library/Preferences/ByHost you'll see a bunch of files like screensaver.localhost.plist (just an example, I'm not near my Mac right now :)

If you change your hostname, then you'll lose all of these settings.

In case you're wondering, these settings are for people that have their user folder stored on a network so they can set up different prefs for different machines. (This is something that will probably be seen more with OS X Server)

Anyway, after editing /etc/hostconfig, you'll want to run something like:

cd ~/Library/Preferences/ByHost
for file in $files; do
mv $file `echo $file | sed 's/MyOldName/MyNewName/g'`

... yes it's a horrible UNIX script but it works.

here's what happens...

cd ~/Library/Preferences/ByHost
change directory (D'uh)

start a bourne shell because csh (or tcsh) suck at scripts.

create a list of the files here

for file in $files; do
think "for every file"

mv $file `echo $file | sed 's/MyOldName/MyNewName/g'`
rename the file. The sed bit swaps MyOldName for MyNewName.

required to terminate the for loop

go back to your previous shell


Now the bad news :(

Some apps will create preference files with spaces in them. THIS IS REALLY BAD FOR UNIX SCRIPTS!
The line "for file in $files; do" splits the list of files by looking for spaces so it can't handle files with spaces in them. (There's probably a way to get this to work nicer but it would be really long)

... basically you'll have to rename any files with spaces manually (from the Finder or the command line). using mv "File 1.localhost.plist" "File 1.Robert.plist" works because of the quote characters.

Hope that helps...

Link :)

[ Reply to This | # ]
Authored by: UtSupra on May 29, '01 06:27:00AM

This tip seems to reapear every so often...
It doesn't work. The Hostname is manage by NetInfo...
Take this false story down!

[ Reply to This | # ]
Re: Not
Authored by: Anonymous on May 29, '01 11:57:50AM

Well it worked for me while months of tweaking this and that, including NetInfo, di not.

Thanks for the tip!


[ Reply to This | # ]
Authored by: Anonymous on May 29, '01 02:40:52PM

so why not tell us how to do it through NetInfo?

[ Reply to This | # ]
Authored by: aaronfaby on May 31, '01 04:26:52PM

Actually, this does work. If you examine the Network startup script you will see that
if the HOSTNAME directive in /etc/hostconfig is set to -AUTOMATIC-, then the ipconfig
command is used to get the hostname. However, this option for ipconfig appears to
be broken, and trying it returns a kernel/os error message. The startup script then
sets the hostname to whatever is specified as HOSTNAME in /etc/hostconfig if an
error is not returned (but since this is always the case, it will always use whatever
is in the file).

I thought this might have been part of apple's strategy to make OS X less usable
as a server as to force users to purchase X Server, but setting the hostname via
ipconfig is broken on that as well......

So yes, the point is this tip is certainly true.

Aaron Faby

[ Reply to This | # ]
Not - re to hostname on Mac OSX
Authored by: ubergoonz on Dec 11, '02 12:30:01PM

actually, the file /etc/hostconfig is very much specific to netinfo inherent from the NeXT Operating System.

IMOHO, on Mac OSX, hostname changes should be done via netinfo using the niutil command.

I haven't been able to get my hands on sysadmin docu on OS X to understand how to do the needful change. But once's i've got it. I can uupdate here.


[ Reply to This | # ]