macos Mac 拒绝更改主机名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8087637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:35:48  来源:igfitidea点击:

Mac refusing to change hostname

macospostfix-mtahostname

提问by twistedpixel

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.

我一直试图弄清楚如何在我的 Mac 上使用 postfix,但出现了可怕的错误,我似乎无法修复它。

I believe the problem is related to starting Postfix.

我相信问题与启动 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.

基本上,Mac 似乎拒绝更改其主机名。在 bash 中,用户显示为“admin@(null)”,如果我输入“hostname”,我也会得到“(null)”。

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.

在“系统偏好设置”中更改“共享”中的主机名会导致第二个示例发生更改(例如,“其他用户可以访问此计算机上的共享文件夹,管理员可以访问所有卷,位于 afp://null/ 或“lion2”。 ") 但第一个保持为空。

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

我什至尝试过 /etc/hostconfig 手动设置主机名,但没有任何效果。

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 无法工作并报告:

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

后缀:警告:valid_hostname:无效字符 40(十进制):(空)后缀:致命:无法使用我自己的主机名

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

拜托,我真的希望有人能帮我解决这个问题。我已经尝试了几个小时了。

Cheers,

干杯,

Scott

斯科特

采纳答案by twistedpixel

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.

非常感谢所有其他答案和帮助,但是经过大量调查,问题似乎出在我的路由器和 iMac 上:路由器不允许 iMac 在客户端更改其主机名或可能向 iMac 发送一个奇怪的主机名以供它使用.

回答by SSteve

Have you tried scutil?

你试过scutil吗?

sudo scutil --get prefwill show the current value and sudo scutil --set pref namewill set the value to name. prefcan be one of these:

sudo scutil --get pref将显示当前值并将sudo scutil --set pref name值设置为namepref可以是其中之一:

           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

回答by valexa

You should have tried running /bin/hostnamedirectly 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.

您应该尝试直接运行/bin/hostname来设置 unix/bsd 层上的主机名,scutil 中的值是 SystemConfiguration 设置,这是 unix 没有注意到的更高层。

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

即使运行/bin/hostname向您显示主机名,sudo scutil --get 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)

设置主机名运行sudo hostname Foo.bar(这与另一个答案给出的代码中的 sethostname() BSD 调用基本相同)

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

或者,您可以运行sudo scutil --set HostName Foo.bar以保持 SystemConfiguration 设置同步

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

注意: SystemConfiguration 中的 HostName 可以与 LocalHostName 和 ComputerName 不同,它也可以与/bin/hostname返回的不同,但最好它们都是同步的,所以你也可以这样做:

sudo scutil --set LocalHostName Foo 
sudo scutil --set ComputerName Foo

回答by sarnold

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

如果你在启动 postfix 之前运行这个小程序,它会起作用吗?

#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

详情见: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.cwill do) then run:

要编译并运行这个小程序,请将其内容保存到一个文件中(/tmp/newhostname.c可以),然后运行:

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.

至少我假设您make(1)知道如何使用默认规则从 C 源编译为可运行的二进制文件。

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

如果尚未安装编译器,也许其他人会有更好的主意。