6

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

twistedpixel
  • 1,181
  • 4
  • 12
  • 31
  • [This may be helpful](http://www.mcs.anl.gov/research/projects/mpi/mpich1-old/docs/mpichman-chp4/node131.htm) -- though since it mentions the Netinfo database, it might also be five years out of date. – sarnold Nov 10 '11 at 23:08
  • Thanks, sarnold but I've already checked this and it's sadly not the problem (or, not the solution!) – twistedpixel Nov 10 '11 at 23:15

4 Answers4

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
SSteve
  • 9,937
  • 5
  • 44
  • 68
  • I'm wondering why HostName is not set. is that because the system uses either of the previous 2 to name that dynamically? – twistedpixel Nov 11 '11 at 00:03
  • 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. – SSteve Nov 11 '11 at 00:21
  • Ok well thanks for your help anyway, I'm glad this is pretty much sorted and Postfix is working again! – twistedpixel Nov 11 '11 at 00:35
  • 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. – valexa Feb 14 '12 at 16:01
  • 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. – Steve Robbins Apr 13 '13 at 06:03
  • 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? – SSteve Apr 13 '13 at 14:32
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
valexa
  • 4,245
  • 28
  • 48
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.

twistedpixel
  • 1,181
  • 4
  • 12
  • 31
  • 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. – Manish Patel Oct 27 '13 at 12:57
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.

sarnold
  • 96,852
  • 21
  • 162
  • 219
  • Is that C? I don't know C :( I tried running it as a C file but it gave me syntax errors. – twistedpixel Nov 10 '11 at 23:18
  • 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`. – SSteve Nov 10 '11 at 23:26
  • 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? – twistedpixel Nov 10 '11 at 23:42
  • 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.. – twistedpixel Nov 10 '11 at 23:46
  • You get `gcc` when you install Xcode. Back in the day when you got your OS X on discs, it was included. – SSteve Nov 10 '11 at 23:47
  • I have Xcode. Although before I posted to Overflow I reinstalled Lion so maybe that messed things up. – twistedpixel Nov 10 '11 at 23:52
  • 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](http://en.wikipedia.org/wiki/Tomato_(firmware)) puts it under _Basic / Static DHCP_. – sarnold Nov 11 '11 at 00:18
  • 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! – twistedpixel Nov 11 '11 at 00:40
  • 1
    You can apparently convince OS X to [ignore DHCP-provided hostnames](https://discussions.apple.com/thread/849032?start=0&tstart=0) -- add a `HOSTNAME` entry to `/etc/hostname` and make sure it isn't `-AUTO-`. – sarnold Nov 11 '11 at 00:52
  • 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. – twistedpixel Nov 11 '11 at 15:02
  • 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. :( – sarnold Nov 12 '11 at 00:05