Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac refusing to change hostname

I've been trying to figure out how to use postfix on my Mac and something has gone horribly wrong and I can't seem to fix it.

I believe the problem is related to starting Postfix.

Basically, the Mac seems to refuse to change its hostname. In bash, the user appears as "admin@(null)", if I type 'hostname' I'm given "(null)" also.

Changing the hostname in Sharing from System Preferences causes the second example to change (where it says, for example, "Other users can access shared folders on this computer, and administrators all volumes, at afp://null/ or “lion2”.") but the first stays as null.

I've even tried /etc/hostconfig manually setting hostname but nothing works.

Is there somewhere else the hostname is trying to be set but is perhaps corrupt? Or contains an invalid character or something?

This is causing Postfix not to work and report:

postfix: warning: valid_hostname: invalid character 40(decimal): (null) postfix: fatal: unable to use my own hostname

Please, I really hope someone can help me fix this. I've been trying for hours now.

Cheers,

Scott

like image 618
twistedpixel Avatar asked Nov 10 '11 23:11

twistedpixel


2 Answers

Have you tried scutil?

sudo scutil --get pref will show the current value and sudo scutil --set pref name will set the value to name. pref can be one of these:

           ComputerName   The user-friendly name for the system.

           LocalHostName  The local (Bonjour) host name.

           HostName       The name associated with hostname(1) and gethostname(3).

Here's what I get on my machine:

$ sudo scutil --get ComputerName
SteveBook2
$ sudo scutil --get LocalHostName
SteveBook2
$ sudo scutil --get HostName
HostName: not set
like image 86
SSteve Avatar answered Oct 10 '22 18:10

SSteve


You should have tried running /bin/hostname directly to set the hostname on the unix/bsd layer, the values from scutil are SystemConfiguration settings which is a higher layer that unix is oblivious to.

It is perfectly normal for sudo scutil --get HostName to return not set even if running /bin/hostname shows you a hostname.

To set the hostname run sudo hostname Foo.bar (this is basically identical to sethostname() BSD call in the code given by another answer)

Optionally you could then run sudo scutil --set HostName Foo.bar to keep the SystemConfiguration settings in sync

NOTE: The HostName in SystemConfiguration can be different from the LocalHostName and ComputerName , it can also be different from what /bin/hostname returns but it is best that they are all in sync, so you could also do :

sudo scutil --set LocalHostName Foo 
sudo scutil --set ComputerName Foo
like image 31
valexa Avatar answered Oct 10 '22 20:10

valexa