hostnamectlを使います
デフォルトのhostnameである「localhost.localdomain」から、「local.domain-server.com(任意)」に変更するため、hostnamectlコマンドを使います。
変更前
$ hostname
localhost.localdomain
hostnamectlで変更する
$ hostnamectl set-hostname local.domain-server.com
※CentOS7インストール時にサーバソフトウェアを入れていると、以下のメッセージが出るので、sudoで実行します。
Could not set property: Interactive authentication required.
→権限がないので、sudoで実行。
$ sudo hostnamectl set-hostname local.domain-server.com
※ CentOS 7がminimalだと、以下のようなメッセージが出ます。
==== AUTHENTICATING FOR org.freedesktop.hostname1.set-static-hostname ===
Authentication is required to set the statically configured local host name, as well as the pretty host name.
Authenticating as: user01
Password:
==== AUTHENTICATION COMPLETE ===
→Passwordプロンプトでパスワードを入力しEnterを押します。
変更後
$ hostname
local.domain-server.com
簡単ですね。
他にも、nmtuiや、nmcliで変更できますが、全部、/etc/hostnameを変更しているので、直接ファイルを編集しても同じことができます。
※ログイン中のプロンプトのホスト名は、一旦ログアウトしてログインし直さないと反映されません。