0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AMIから複製サーバーを建てた際にhostnameを変える方法

Posted at

AMIから複製サーバーを建てた際に勝手にhostnameが変わってた経験皆さんありませんか?
私はあります。
ということで、その際の対処法を記録しときます。

1./etc/cloud/cloud.cfgの編集

preserve_hostname: true
の記述を追加します。
hostnamectlで変えただけでは、サーバーを再起動した際に元に戻ってしまいます。
これがAMIから複製サーバーを建てた際に元に戻っている原因です。
しっかりこの記述をconfファイルに記述しときましょう。

[root@ip-172-31-34-110 ~]# cp /etc/cloud/cloud.cfg /etc/cloud/cloud.cfg.20250701
[root@ip-172-31-34-110 ~]# vim /etc/cloud/cloud.cfg
[root@ip-172-31-34-110 ~]# diff -u /etc/cloud/cloud.cfg.20250701 /etc/cloud/cloud.cfg
--- /etc/cloud/cloud.cfg.20250701	2025-07-01 13:26:23.237980466 +0900
+++ /etc/cloud/cloud.cfg	2025-07-01 13:26:47.774079913 +0900
@@ -4,6 +4,8 @@
 users:
  - default
 
+preserve_hostname: true
+
 disable_root: true
 ssh_pwauth:   false

その後hostnamectlで変える
例でwww.example.co.jpにしてます。

[root@ip-172-31-34-110 ~]# hostnamectl set-hostname www.example.co.jp
[root@ip-172-31-34-110 ~]# hostname
www.example.co.jp
[root@ip-172-31-34-110 ~]# hostname -f
www.example.co.jp

ログアウトしてもっかいsshしてみます

[ec2-user@ip-172-31-34-110 ~]$ ログアウト
Connection to XXX.XXX.XXX.XXX closed.
hoge@iMac-PC-0079 ~ % ssh -i ~/ssh/ssh_key/hogehoge.pem ec2-user@XXX.XXX.XXX.XXX
Last login: Tue Jul  1 13:17:26 2025 from hogehoge

      __|  __|_  )
      _|  (     /   Amazon Linux 2 AMI
     ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
[ec2-user@www ~]$ sudo su -
最終ログイン: 2025/07/01 (火) 13:17:31 JST日時 pts/0
[root@www ~]# hostname
www.example.co.jp

無事変わりましたね🙌

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?