Mac refusing to change hostname
Asked Answered
A

4

7

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

Agon answered 10/11, 2011 at 23:3 Comment(2)
This may be helpful -- though since it mentions the Netinfo database, it might also be five years out of date.Fortuitous
Thanks, sarnold but I've already checked this and it's sadly not the problem (or, not the solution!)Agon
A
2

All other answers and help was much appreciated, however after much investigation, the problem appears to lie with my router and iMac: router not allowing the iMac to change its hostname client-side OR possibly sending a weird hostname to the iMac for it to use.

Agon answered 11/11, 2011 at 14:57 Comment(1)
There seems to be a general problem with OSX with this. I have several machines at home, including one running Ubuntu - none of them have a problem setting hostname, only the mac gets confused. A router reset seems to do the trick for it.Countess
X
13

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
Ximenes answered 10/11, 2011 at 23:46 Comment(6)
I'm wondering why HostName is not set. is that because the system uses either of the previous 2 to name that dynamically?Agon
I don't know enough about HostName to have a useful answer to that. But it does show that setting the name in System Preferences -> Sharing doesn't have an effect on HostName.Ximenes
Ok well thanks for your help anyway, I'm glad this is pretty much sorted and Postfix is working again!Agon
HostName is what unix sets, ComputerName/LocalHostName is what the SystemConfiguration framework sets e.g.: /bin/hostname should reflect the former while SystemPreferences the later.Milzie
After the set command, I'm left with a single carrot (>) and not sure where to go from there. Other than quit, but then it doesn't seem to save.Odaodab
The single caret (it's shaped like a carrot but has a different name) means you haven't finished your Unix command and it's looking for more input. What exactly are you typing?Ximenes
M
3

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
Milzie answered 14/2, 2012 at 15:45 Comment(0)
A
2

All other answers and help was much appreciated, however after much investigation, the problem appears to lie with my router and iMac: router not allowing the iMac to change its hostname client-side OR possibly sending a weird hostname to the iMac for it to use.

Agon answered 11/11, 2011 at 14:57 Comment(1)
There seems to be a general problem with OSX with this. I have several machines at home, including one running Ubuntu - none of them have a problem setting hostname, only the mac gets confused. A router reset seems to do the trick for it.Countess
F
1

If you run this tiny program before starting postfix, does it work?

#include <unistd.h>

int main(int argc, char* argv[]) {
    char host[] = "newhostname";
    sethostname(host, sizeof(host));
    return 0;
}

I don't presume to know what else might depend upon your old hostname -- do some testing on all your services after running this.

Details at: http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/gethostname.3.html

Update

To compile and run this little program, save its contents to a file (/tmp/newhostname.c will do) then run:

cd /tmp
make newhostname
sudo ./newhostname

At least I assume your make(1) knows how to compile from C sources to runnable binaries with default rules.

If the compiler isn't already installed maybe someone else will have a better idea.

Fortuitous answered 10/11, 2011 at 23:12 Comment(11)
Is that C? I don't know C :( I tried running it as a C file but it gave me syntax errors.Agon
If you save that file as test.m you can compile it with the command gcc -Wall test.m -o test. Then run it by typing ./test.Ximenes
I don't have brew installed on this Mac yet so I don't have make. @SSteve: I don't have gcc!! What?!?! Oh wait, didn't Apple replace it with lldl or something?Agon
I don't believe it - I've tracked down the problem to my router but I'm not sure how to fix it. It seems the router's DHCP is somehow sending it an invalid hostname. Any idea where I would look to stop it from doing this? Not sure how this happened as it used to work fine a few weeks ago and I don't recall changing any settings on the router..Agon
You get gcc when you install Xcode. Back in the day when you got your OS X on discs, it was included.Ximenes
I have Xcode. Although before I posted to Overflow I reinstalled Lion so maybe that messed things up.Agon
Some routers make it easy to set "static IPs" via DHCP and sometimes they set the hostname too. Not sure where yours puts it, but Tomato puts it under Basic / Static DHCP.Fortuitous
This router sucks. It's a free one from the ISP and there aren't any settings for naming via DHCP except for changing the name on each entry but that seems unnecessary as it displays the hostname correctly anyway. I guess it's possible it sends it to the iMac in a stupid character set or something? In any case, unplugging the ethernet cable from the iMac results in instant hostname change on the iMac to the specified name. Ergo: crappy router software. Cheers for your help!Agon
You can apparently convince OS X to ignore DHCP-provided hostnames -- add a HOSTNAME entry to /etc/hostname and make sure it isn't -AUTO-.Fortuitous
Do you mean /etc/hostconfig ? If so, I already tried that but I just wasn't really happy about it since it felt like an override rather than fixing the problem properly, you know? Because this made the hostname appear correctly in Terminal but still refused to update from System prefs or 'hostname' command (or even scutil) I like to do things 100% to ensure a workaround doesn't lead to future problems. I guess sometimes you just have to take a workaround though.Agon
I know what you mean about fixing problems properly, but if your router is handing out stupid names, it is up to your DHCP client to know when to ignore them. If the /etc/hostconfig is the best way Apple has made available for telling your DHCP client to ignore silly hostnames, that's just the way it is. :(Fortuitous

© 2022 - 2024 — McMap. All rights reserved.