これは、なにをしたくて書いたもの?
Linuxのホスト名の変え方を、ちゃんと見てきていなかったので、この機会にちょっと見ておきたいな、と。
hostnamectl
かnmcli general hostname
のどちらかを使って変更するのが良いみたいですね。
環境
環境は、Ubuntu Linux 20.04 LTSを使って行います。Vagrantの以下のBoxを使用しました。Libvirt Providerです。
確認時点での情報。
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.4 LTS Release: 20.04 Codename: focal $ uname -srvmpio Linux 5.4.0-110-generic #124-Ubuntu SMP Thu Apr 14 19:46:19 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
hostnameコマンドや直接/etc/hostnameファイルを修正して変更した場合
まずは、hostname
コマンド。
hostname(1) - Linux manual page
現在のホスト名。
$ hostname ubuntu2004.localdomain
変更してみます。
$ sudo hostname myhost
変わりました。
$ hostname myhost
ただ、この方法で変更してもOSを再起動すると
$ sudo reboot
元に戻ってしまいます。
$ hostname ubuntu2004.localdomain
次に、/etc/hostname
ファイルを変更してみます。
$ sudo vim /etc/hostname
このように。
/etc/hostname
myhost
この変更は、すぐには反映されません。
$ hostname ubuntu2004.localdomain
OSを再起動すると
$ sudo reboot
反映されます。
$ hostname myhost
hostname
の説明を見るとわかるのですが、起動時に1度読むだけだからですね。
The host name is usually set once at system startup by reading the contents of a file which contains the host name, e.g. /etc/hostname).
hostname(1) - Linux manual page
/etc/hostname
ファイルは元に戻して(再起動もして)おきます。
/etc/hostname
ubuntu2004.localdomain
hostnamectlコマンドで変更する
次に、hostnamectl
コマンドを見てみましょう。
hostnamectl(1) - Linux manual page
Ubuntu Manpage: hostnamectl - Control the system hostname
現時点でのホスト名。
$ hostname ubuntu2004.localdomain
hostnamectl
コマンドで見ると、もっと情報が表示されます。
$ hostnamectl Static hostname: ubuntu2004.localdomain Icon name: computer-vm Chassis: vm Machine ID: e7950de6a4f749c3a0427899e320e4c0 Boot ID: 0da5d3ff14ef4bf487b70c800a73eb8b Virtualization: kvm Operating System: Ubuntu 20.04.4 LTS Kernel: Linux 5.4.0-110-generic Architecture: x86-64
ホスト名の変更は、root
権限でhostnamectl set-hostname
を実行。
$ sudo hostnamectl set-hostname myhost
こちらの場合、即座に反映されます。
$ hostname myhost
/etc/hostname
ファイルも変更されます。
/etc/hostname
myhost
OSを再起動しても
$ sudo reboot
変更は反映されたままです。
$ hostname myhost
ところで、hostnamectl set-hostname
でオプションを指定した場合は、指定したホスト名だけが変更されることが書かれています。
however, if one or more of --static, --transient, --pretty are used, only the selected hostnames are changed.
Ubuntu Manpage: hostnamectl - Control the system hostname
3つありますね。違いは?
This tool distinguishes three different hostnames
static
は起動時にカーネルが初期化しているもの。
the static hostname which is used to initialize the kernel hostname at boot (e.g. "lennarts-laptop"),
transient
はネットワーク設定のフォールバック値で、static
にあたるホスト名が設定され、それが妥当であればそちらが使用されtransient
な
ホスト名は使われないみたいです。
transient hostname which is a fallback value received from network configuration. If a static hostname is set, and is valid (something other than localhost), then the transient hostname is not used.
pretty
はハイレベルなホスト名だということですが…今回は気にしないことにします…。
the high-level "pretty" hostname which might include all kinds of special characters (e.g. "Lennart's Laptop")
それぞれの情報を見てみましょう。
$ hostnamectl Static hostname: myhost Icon name: computer-vm Chassis: vm Machine ID: e7950de6a4f749c3a0427899e320e4c0 Boot ID: 769e58a6b4d941459ccfae7795de713f Virtualization: kvm Operating System: Ubuntu 20.04.4 LTS Kernel: Linux 5.4.0-110-generic Architecture: x86-64 $ hostnamectl --static myhost $ hostnamectl --transient myhost $ hostnamectl --pretty $ hostname myhost
pretty
は設定されていなさそうです。
試しに、--transient
を指定してみます。
$ sudo hostnamectl set-hostname --transient myhost2
結果。Transient hostname
が増えたりしていますが、hostname
は変わりませんね。
$ hostnamectl Static hostname: myhost Transient hostname: myhost2 Icon name: computer-vm Chassis: vm Machine ID: e7950de6a4f749c3a0427899e320e4c0 Boot ID: 769e58a6b4d941459ccfae7795de713f Virtualization: kvm Operating System: Ubuntu 20.04.4 LTS Kernel: Linux 5.4.0-110-generic Architecture: x86-64 $ hostnamectl --static myhost $ hostnamectl --transient myhost2 $ hostnamectl --pretty $ hostname myhost
再起動すると
$ sudo reboot
Transient hostname
はいなくなりました。
$ hostnamectl Static hostname: myhost Icon name: computer-vm Chassis: vm Machine ID: e7950de6a4f749c3a0427899e320e4c0 Boot ID: f54e564b8fe94ddc9e1e5d30eeaa1a65 Virtualization: kvm Operating System: Ubuntu 20.04.4 LTS Kernel: Linux 5.4.0-110-generic Architecture: x86-64
nmcli general hostnameを使う
最後はNetworkManagerのnmcli
を使います。
この環境ではNetworkManagerがインストールされていなかったので、まずはインストールから…。
$ sudo apt install network-manager
使用するのは、nmcli general
コマンドです。
説明。
$ nmcli general hostname -h 使い方: nmcli general hostname { 引数 | help } 引数 := [<ホスト名>] システムの永続的なホスト名を取得または変更します。 引数を付けずに実行すると、現在設定されているホスト名を出力します。ホスト名を 渡すと、そのホスト名をシステムの永続的なホスト名として新たに設定します。
永続的なホスト名の表示または設定を行うようです。
現在の値を表示。
$ nmcli general hostname ubuntu2004.localdomain
変更。root
権限が必要です。
$ sudo nmcli general hostname myhost
確認。
各種コマンドの結果。
$ hostnamectl Static hostname: myhost Icon name: computer-vm Chassis: vm Machine ID: e7950de6a4f749c3a0427899e320e4c0 Boot ID: bc2fc058a7b6409e91ff3f13be0b553d Virtualization: kvm Operating System: Ubuntu 20.04.4 LTS Kernel: Linux 5.4.0-110-generic Architecture: x86-64 $ hostname myhost
/etc/hostname
ファイル。
/etc/hostname
myhost
OS再起動後の確認は省略します。
また、general
とhostname
の部分はそれぞれg
とh
に省略することができます。
$ nmcli g h ubuntu2004.localdomain
変更。
$ sudo nmcli g h myhost
確認。
$ nmcli g h myhost $ hostname myhost $ hostnamectl Static hostname: myhost Icon name: computer-vm Chassis: vm Machine ID: e7950de6a4f749c3a0427899e320e4c0 Boot ID: 7ff8e9b79d854d17b67e470fb8d37e69 Virtualization: kvm Operating System: Ubuntu 20.04.4 LTS Kernel: Linux 5.4.0-110-generic Architecture: x86-64
まとめ
Ubuntu Linux 20.04 LTSを使ってですが、ホスト名の変更方法を見てみました。
hostnamectl
またはNetworkManagerを使えば良さそうですが、使える環境ならNetworkManagerを利用すればいいのではないでしょうか。