17.6 C
Wednesday, December 1, 2021

How to Set or Change Hostname in Amazon Linux EC2 Instance

Must read

I am a new Amazon Linux 2 EC2 instance user. My EC2 name was set to ‘ip-172-26-7-134’ during instance creation. But now, I would like to change the EC2 hostname to ‘stats1’. Can you tell me how do I remove ip-172-26-7-134 and set it to stats1 on Amazon Linux 2? How do I change the Amazon Linux Lightsail or EC2 cloud server instance?

You can use the hostname command or hostnamectl command to see or set the Amazon Linux EC2 instance’s host name. Open the terminal application and type the following commands to set or change hostname or computer name on Amazon Linux EC2 Instance.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Amazon Linux 2
Est. reading time 3 minutes

How To Change the hostname of your Amazon Linux Instance

The procedure to change the computer name on Ubuntu Linux without public DNS name:

  1. Use the hostnamectl command to set your hostname to stats1:
    sudo hostnamectl set-hostname stats1.localdomain
  2. Edit the /etc/hosts file:
    sudo vim /etc/hosts
  3. Find entry and append your hostname at the end:
  4. Save and close the file
  5. Reboot the instance:
    sudo reboot
How to Set or Change Hostname in Amazon Linux EC2 Instance

Click to enlarge

Please note that the reboot step is optional and is not needed.

Display the current Amazon Linux hostname

Type the hostname or hostnamectl (or use the cat command as follows:
# or #
# another option #
cat /etc/hostname

Patreon supporters only guides 🤓

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare
  • Join my Patreon to support independent content creators and start reading latest guides:

Join Patreon

Amazon Linux Change Hostname Using hostnamectl with public DNS

Say you have public DNS name such as stats1.opensourceflare.com, then you would change the system hostname to a public DNS name as follows:

  1. Open the terminal application.
  2. Log in into the remote EC2 instance using the ssh command.
  3. Then set hostname:
    sudo hostnamectl set-hostname stats1.opensourceflare.com
  4. Reboot the EC2 or Lightsail instance:
    sudo reboot
  5. After rebooting system, verify new hostname:
  6. Send ICMP ECHO_REQUEST to network using the ping command:
    ping -c4 stats1.opensourceflare.com

Why updates the /etc/hosts file?

The /etc/hosts file acts as a static table lookup for hostnames when DNS is not in place. Many services on Linux and Unix-like systems use it for resolving hostnames. Before the arrival of DNS, the host table was the only way of resolving hostnames on the fledgling Internet. Hence, we need to edit this file when setting hostname without DNS. This file is a text file that associates IP addresses with hostnames, one line per IP address. For each host, a single line should be present with the following information:
IP_address canonical_hostname [aliases...]


Here is my home /etc/hosts file:	localhost

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters  router  wifi  printer hp0   freebsd git dell6700   rhel8   nixcraft-macbookpro mac office

Summing up

In this tutorial, you learned how to change hostname on Amazon Linux EC2 Instance using the hostnamectl command. For more information see the following man pages using the man command or read it online:
man hostnamectl
man hostname
man 5 hosts


Source link

- Advertisement -

More articles


Please enter your comment!
Please enter your name here

- Advertisement -

Latest article