Hostnamectlコマンドはどういう意味ですか?

Hostnamectlコマンドはどういう意味ですか?

/etc/hostname または関連するコンテンツを編集するのとは逆ですか?

そこには妥当な理由があるでしょう(私は希望します)。通常、私はすべてがテキストファイルである「古い」方法を好みます。私は議論をするつもりはありません。それが妥当な理由なのか、自分で知って決めたいのです。ありがとうございます。

ベストアンサー1

背景

hostnamectlsystemdの一部であり、標準化された方法でサーバーのホスト名を設定するのに適したAPIを提供します。

$ rpm -qf $(type -P hostnamectl)
systemd-219-57.el7.x86_64

以前は、systemdを使用していなかったすべてのディストリビューションにはこれを行う独自の方法があり、不必要な複雑さがたくさんありました。

DESCRIPTION
  hostnamectl may be used to query and change the system hostname and
  related settings.

  This tool distinguishes three different hostnames: the high-level 
  "pretty" hostname which might include all kinds of special characters 
  (e.g. "Lennart's Laptop"), the static hostname which is used to
  initialize the kernel hostname at boot (e.g. "lennarts-laptop"), and the 
  transient hostname which is a default received from network 
  configuration. If a static hostname is set, and is valid (something
   other than localhost), then the transient hostname is not used.

   Note that the pretty hostname has little restrictions on the characters 
   used, while the static and transient hostnames are limited to the 
   usually accepted characters of Internet domain names.

   The static hostname is stored in /etc/hostname, see hostname(5) for 
   more information. The pretty hostname, chassis type, and icon name are 
   stored in /etc/machine-info, see machine-info(5).

   Use systemd-firstboot(1) to initialize the system host name for mounted 
   (but not booted) system images.

hostnamectlまた、ブートできるようにさまざまなデータを1か所にインポートします。

$ hostnamectl
   Static hostname: centos7
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 1ec1e304541e429e8876ba9b8942a14a
           Boot ID: 37c39a452464482da8d261f0ee46dfa5
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-693.21.1.el7.x86_64
      Architecture: x86-64

ここにある情報は、サーバーのホスト名を含むなどで提供され/etc/*releaseますuname -a

ファイルはどこにありますか?

ところで、すべてがまだファイルにあるので、hostnamectlそのファイルと対話するか、各ファイルの場所を知る方法が簡素化されました。

strace -s 2000 hostnamectl証拠として、どのファイルをインポートするかを確認して使用できます。

$ strace -s 2000 hostnamectl |& grep ^open | tail -5
open("/lib64/libattr.so.1", O_RDONLY|O_CLOEXEC) = 3
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
open("/proc/self/stat", O_RDONLY|O_CLOEXEC) = 3
open("/etc/machine-id", O_RDONLY|O_NOCTTY|O_CLOEXEC) = 4
open("/proc/sys/kernel/random/boot_id", O_RDONLY|O_NOCTTY|O_CLOEXEC) = 4

systemd-hostname.service?

鋭い観察者であれば、上記straceのすべてのファイルが存在するわけではないことを覚えておく必要があります。実際には、ほとんどの管理者に馴染みのあるほとんどのファイルと実際に「相互作用」するhostnamectlサービスとの相互作用に関するものです。systemd-hostnamectl.service/etc/hostname

したがって、実行すると、hostnamectlサービスから詳細を取得できます。これはオンデマンドサービスなので、常に実行されていることを確認することはできません。走るときだけhostnamectlwatchコマンドの実行後にhostnamectl何度も実行を開始すると、それを確認できます。

$ watch "ps -eaf|grep [h]ostname"
root      3162     1  0 10:35 ?        00:00:00 /usr/lib/systemd/systemd-hostnamed

ソースは次のとおりです。https://github.com/systemd/systemd/blob/master/src/hostname/hostnamed.c/etc/hostname詳しく見ると、同等の参照が表示されます。

引用する

おすすめ記事