Technology

How to Set or Change Hostname in Amazon Linux EC2 Instance

There is no greater hope in human life than survival. At the same time, it is important to live well. With that in mind, every content on the website is written so that a person can get all the information from here to start his life to make beautifully.

According to that, Technology is one of the topics. It is also a part of life. Read carefully Details of Technology related article

How to Set or Change Hostname in Amazon Linux EC2 Instance

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 127.0.0.1 entry and append your hostname at the end:
    stats1.localdomain
  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:
hostname
# or #
hostnamectl
# another option #
cat /etc/hostname

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:

 

Patreon supporters only guides 🤓

    • No ads and tracking

 

 

    • Join my Patreon to support independent content creators and start reading latest guides:

 

Join Patreon
  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:
    hostnamectl
  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...]

Examples

Here is my home /etc/hosts file:

127.0.0.1	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

192.168.1.254  router
192.168.1.253  wifi
192.168.1.252  printer hp0
192.168.1.17   freebsd git dell6700
192.168.1.18   rhel8
192.168.1.19   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

 


ADVERTISEMENT
 

 

Did you like this article?
Share it on any of the following social media channels below to give us your vote. Your feedback helps us improve.

Other related Technologies ideas you might enjoy

Related Articles

Leave a Reply

Your email address will not be published.