Solved: How to change hostname in AWS EC2 instance of RHEL 7

In our last post we have seen how to change hostname of an RHEL server.

But if you are using the RHEL 7 AMI provided on AWS marketplace the steps will be slightly different.

First login to your EC2 instance. (Check this post to know How to login to AWS EC2 Linux instance.)

Once you login to your EC2 instance execute below command.

 sudo hostnamectl set-hostname --static cloudvedas

(Here “cloudvedas” is the new hostname.)

If you want to make it persistent across reboot follow further.

Save on Flights to Hot Beaches!Use code BT18 & get up to $18 off on our fees on flights.Use the Code & Save!

Now using vi or vim editor edit the file /etc/cloud/cloud.cfg

sudo vi /etc/cloud/cloud.cfg

At the end of file add the following line and save the file

preserve_hostname: true

Finally reboot the server

sudo reboot

Once the server is up, check the hostname.

ec2-user# hostname
cloudvedas
ec2-user#

It should now show you the new hostname.

2 Replies to “Solved: How to change hostname in AWS EC2 instance of RHEL 7”

  1. Thanks, this has worked to change EC2 host name, I have another requirement, if I wanted to access EC2 by host name from another EC2 instance, how do I access?
    Ec2 one: webserver1(hostname)
    ec2 two: appserver1(hostname)

    I would like to ping ‘appserver1’ from ‘webserver1’, I’ve modified the host name as per your instrucitons, however when I try to ping ‘appserver1’ from ‘webserver1’, getting error.

    ]$ ping appserver1
    ping: appserver1: Name or service not known

Leave a Reply

Your email address will not be published. Required fields are marked *