LoginSignup
3
5

More than 3 years have passed since last update.

Amazon Linux 2のhostname変更方法

Last updated at Posted at 2021-01-23
はじめに

AmazonLinux2でEC2を立ち上げ、 hostsファイルnetworkファイルを編集してhostnameを変更後にLinuxを再起動後に再度SSH接続してもhostnameが変更されていないという場面で遭遇しました。
そのため、AmazonLinux2でのhostname変更方法の忘備録を残しておきたいと思います。

変更方法

hostnamectlコマンドでhostnameを変更する

# hostnamectlコマンドで変更前のhostnameを確認する
[ec2-user@ip-10-0-11-229 ~]$ hostnamectl 
   Static hostname: ip-10-0-11-229.ap-northeast-1.compute.internal ←変更前のhostname
         Icon name: computer-vm
           Chassis: vm
        Machine ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
           Boot ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    Virtualization: xen
  Operating System: Amazon Linux 2
       CPE OS Name: cpe:2.3:o:amazon:amazon_linux:2
            Kernel: Linux 4.14.209-160.339.amzn2.x86_64
      Architecture: x86-64

# hostnameコマンドでhostnameを変更
[ec2-user@ip-10-0-11-229 ~]$ sudo hostnamectl set-hostname hogehoge

# hostsファイルの変更
[ec2-user@ip-10-0-11-229 ~]$ cat /etc/hosts
127.0.0.1  hogehoge localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost6 localhost6.localdomain6

# インスタンスの再起動
[ec2-user@ip-10-0-11-229 ~]$ sudo reboot

# SSH接続
$ ssh -i hogehoge.pem ec2-user@XX.XXX.XXX.XXX

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

https://aws.amazon.com/amazon-linux-2/
[ec2-user@hogehoge ~]$
まとめ

hostnameを変更するにはhostnamectlを使って変更しないと反映されないみたいなんですね。

3
5
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
3
5