Something Different In Linux

Home » aws » script to change hostname for AWS instance

script to change hostname for AWS instance

Posted by Rishikesh Vispute Categories: aws

If you want to set the public DNS as a hostname to the AWS linux  server then you can set it by using following scripts.

The default public DNs for the AWs instance in the format “” and when we create the instance from AWs then it set internal hostname for the server. We can set the correct hostname for AWS instance.

1) Create file vi /home/ and add the following code.
#!/usr/bin/env bash
OLD_HOSTNAME="$( hostname )"
# Auto Detect the Hostname of the server
NEW_HOSTNAME="$( curl )"

if [ -z "$NEW_HOSTNAME" ]; then
#echo -n "Please enter new hostname: "
read NEW_HOSTNAME < /dev/tty
if [ -z "$NEW_HOSTNAME" ]; then
echo "Error: no hostname entered. Exiting."
exit 1
echo "Changing hostname from $OLD_HOSTNAME to $NEW_HOSTNAME..."
hostname "$NEW_HOSTNAME"
sed -i "s/HOSTNAME=.*/HOSTNAME=$NEW_HOSTNAME/g" /etc/sysconfig/network
if [ -n "$( grep "$OLD_HOSTNAME" /etc/hosts )" ]; then
sed -i "s/$OLD_HOSTNAME/$NEW_HOSTNAME/g" /etc/hosts
echo -e "$( curl )\t$NEW_HOSTNAME" >> /etc/hosts
echo "Done."

2) chmod 755 /home/
3) sh /home/
4) check hostname is changed or not by using the command
[root@] hostname
[root@] hostname -i
4) Done

Popular Posts

remove index.php fro

How to remove index.php from url in magento Ans : If you ...

PHP-Java bridge on p

  What is php-java bridge? The php-java bridge is the php ...

Set expiration for y

If you want to set the expiration to your site ...

Warning: Parameter 1

Error: Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a ...

domain pointing to e

If you domain uses external MX record and it is ...