Linux hostname

设置方式

linux下设置hostname有以下两个命令

  • hostnamectl
  • hostname

hostnamectl是 systemd (https://github.com/systemd/systemd) 的工具。

hostname设置/proc/sys/kernel/hostname中的值。

hostnamectl中的 hostnamectl set-hostname 则提供了三个选项

  • static: 永久修改
  • transient:临时主机
  • pretty: 仅查看,不提供网络功能

看下hostnamectl 文档中的说明

Effectively, the static hostname has higher priority than a transient hostname, which has higher
priority than the fallback hostname. Transient hostnames are equivalent, so setting a new transient
hostname causes the previous transient hostname to be forgotten. The hostname specified on the kernel
command line is like a transient hostname, with the exception that it has higher priority when the
machine boots. Also note that those are the semantics implemented by systemd tools, but other programs
may also set the hostname
typedef enum HostnameSource {
        HOSTNAME_STATIC,     /* from /etc/hostname */
        HOSTNAME_TRANSIENT,  /* a transient hostname set through systemd, hostnamed, the container manager, or otherwis…
        HOSTNAME_DEFAULT,    /* the os-release default or the compiled-in fallback were used */
        _HOSTNAME_INVALID = -EINVAL,
} HostnameSource;

static 是修改/etc/hostname中的内容, transient修改的是内核中的内容, 可以有多种方式修改hostname也是修改内核的hostname /proc/sys/kernel/hostname, pretty就是为了设置标签方便查找。systemd中还会携带一个hostnamed的服务, 下面是 Debian环境中的

$ sudo systemctl status systemd-hostnamed.service
● systemd-hostnamed.service - Hostname Service
   Loaded: loaded (/lib/systemd/system/systemd-hostnamed.service; static; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:systemd-hostnamed.service(8)
           man:hostname(5)
           man:machine-info(5)
           https://www.freedesktop.org/wiki/Software/systemd/hostnamed
Dec 18 21:03:34 debian systemd[1]: Started Hostname Service.
Dec 18 21:03:34 debian systemd-hostnamed[1409]: Changed pretty host name to 'My computer'
Dec 18 21:04:05 debian systemd[1]: systemd-hostnamed.service: Succeeded.
Dec 18 21:09:58 debian systemd[1]: Starting Hostname Service...
Dec 18 21:09:58 debian systemd[1]: Started Hostname Service.
Dec 18 21:09:58 debian systemd-hostnamed[1566]: Changed icon name to 'linux-vm'
Dec 18 21:10:14 debian systemd-hostnamed[1566]: Changed chassis to 'vm'
Dec 18 21:10:19 debian systemd-hostnamed[1566]: Changed deployment to 'development'
Dec 18 21:10:26 debian systemd-hostnamed[1566]: Changed location to 'Berlin, Germany'
Dec 18 21:10:56 debian systemd[1]: systemd-hostnamed.service: Succeeded.

 

命名格式:

gethostbyname函数实现分析

可以看到再进行域名解析的时候, 通过 /etc/nsswitch.conf 配置文件可以确定先通过DNS方式获取还是先通过文件方式获取,为了格式的统一,一些RFC规范中对hostname进行比较严格的显示, 数字, 字符, “.”, “-“,  但也有例外在windows系统, NetBIOS是可以支持下划线的。

在systemd早期版本中也是支持下划线的, 后续已经做为bug进行修复,

garlic@garlic9999:~$ sudo hostnamectl set-hostname --static garlic_test
Could not set static hostname: Invalid static hostname 'garlic_test'
garlic@garlic9999:~$ sudo hostnamectl set-hostname --transient garlic_test
Hint: static hostname is already set, so the specified transient hostname will not be used.
Could not set transient hostname: Invalid hostname 'garlic_test'

版本

garlic@garlic9999:~$ hostnamectl --version
systemd 249 (249.11-0ubuntu3.7)
+PAM +AUDIT +SELINUX +APPARMOR +IMA +SMACK +SECCOMP +GCRYPT +GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN +IPTC +KMOD +LIBCRYPTSETUP +LIBFDISK +PCRE2 -PWQUALITY -P11KIT -QRENCODE +BZIP2 +LZ4 +XZ +ZLIB +ZSTD -XKBCOMMON +UTMP +SYSVINIT default-hierarchy=unified

 

还找到其他一些原因, 因为早期的键盘上没有“_”,  http://www.pdp8online.com/asr33/pics/kbd_top.shtml?large

总之为了避免麻烦, 在设置hostname尽量不要使用下划线避免兼容性问题的产生。 如果想要设置特殊字符可以是使用pretty:选项。 自己看看就行了。

 

参考链接:

https://access.redhat.com/discussions/6975200

https://github.com/systemd/systemd/issues/11500#issuecomment-455815146

https://domainkeys.sourceforge.net/underscore.html

https://sleeplessbeastie.eu/2020/06/22/how-to-change-system-hostname/

https://www.entrust.com/blog/2019/01/removal-of-underscores-from-domain-names/

https://etherealmind.com/why-i-do-not-use-underscores-in-dns-a-war-story/

https://www.quora.com/Why-are-underscores-not-allowed-in-DNS-host-names

Comments are closed.