Changing the Hostname of Your Linux Instance
When you launch an instance, it is assigned a hostname that is a form of the private,
internal IP address. A typical Amazon EC2 private DNS name looks something like this:
ip-12-34-56-78.us-west-2.compute.internal, where the name
consists of the internal domain, the service (in this case, compute),
the region, and a form of the private IP address. Part of this hostname is displayed at
the shell prompt when you log into your instance (for example,
ip-12-34-56-78). Each time you stop and restart your Amazon EC2
instance (unless you are using an Elastic IP address), the public IP address changes,
and so does your public DNS name, system hostname, and shell prompt. Instances launched
into EC2-Classic also receive a new private IP address, private DNS hostname, and
system hostname when they're stopped and restarted; instances launched into a VPC
don't.
Important
These procedures are intended for use with Amazon Linux. For more information about other distributions, see their specific documentation.
Changing the System Hostname
If you have a public DNS name registered for the IP address of your instance (such
as webserver.mydomain.com), you can set the system hostname so
your instance identifies itself as a part of that domain. This also changes the
shell prompt so that it displays the first portion of this name instead of the
hostname supplied by AWS (for example, ip-12-34-56-78). If you
do not have a public DNS name registered, you can still change the hostname, but the
process is a little different.
To change the system hostname to a public DNS name
Follow this procedure if you already have a public DNS name registered.
On your instance, open the
/etc/sysconfig/networkconfiguration file in your favorite text editor and change theHOSTNAMEentry to reflect the fully qualified domain name (such aswebserver.mydomain.com).HOSTNAME=webserver.mydomain.comReboot the instance to pick up the new hostname.
[ec2-user ~]$sudo rebootLog into your instance and verify that the hostname has been updated. Your prompt should show the new hostname (up to the first ".") and the hostname command should show the fully qualified domain name.
[ec2-user@webserver ~]$hostnamewebserver.mydomain.com
To change the system hostname without a public DNS name
Open the
/etc/sysconfig/networkconfiguration file in your favorite text editor and change theHOSTNAMEentry to reflect the desired system hostname (such aswebserver).HOSTNAME=webserver.localdomainOpen the
/etc/hostsfile in your favorite text editor and change the entry beginning with127.0.0.1to match the example below, substituting your own hostname.127.0.0.1webserver.localdomainwebserverlocalhost localhost.localdomainReboot the instance to pick up the new hostname.
[ec2-user ~]$sudo rebootLog into your instance and verify that the hostname has been updated. Your prompt should show the new hostname (up to the first ".") and the hostname command should show the fully qualified domain name.
[ec2-user@webserver ~]$hostnamewebserver.localdomain
Changing the Shell Prompt Without Affecting the Hostname
If you do not want to modify the hostname for your instance, but you would like to
have a more useful system name (such as webserver) displayed than
the private name supplied by AWS (for example, ip-12-34-56-78),
you can edit the shell prompt configuration files to display your system nickname
instead of the hostname.
To change the shell prompt to a host nickname
Create a file in
/etc/profile.dthat sets the environment variable calledNICKNAMEto the value you want in the shell prompt. For example, to set the system nickname towebserver, execute the following command.[ec2-user ~]$sudo sh -c 'echo "export NICKNAME=webserver" > /etc/profile.d/prompt.sh'Open the
/etc/bashrcfile in your favorite text editor (such as vim or nano). You need to use sudo with the editor command because/etc/bashrcis owned byroot.Edit the file and change the shell prompt variable (
PS1) to display your nickname instead of the hostname. Find the following line that sets the shell prompt in/etc/bashrc(several surrounding lines are shown below for context; look for the line that starts with[ "$PS1"):# Turn on checkwinsize shopt -s checkwinsize [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h\W]\\$ " # You might want to have e.g. tty in prompt (e.g. more virtual machines) # and console windowsAnd change the
\h(the symbol forhostname) in that line to the value of theNICKNAMEvariable.# Turn on checkwinsize shopt -s checkwinsize [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@$NICKNAME\W]\\$ " # You might want to have e.g. tty in prompt (e.g. more virtual machines) # and console windows(Optional) To set the title on shell windows to the new nickname, complete the following steps.
Create a file called
/etc/sysconfig/bash-prompt-xterm.[ec2-user ~]$sudo touch /etc/sysconfig/bash-prompt-xtermMake the file executable with the following command.
[ec2-user ~]$sudo chmod +x /etc/sysconfig/bash-prompt-xtermOpen the
/etc/sysconfig/bash-prompt-xtermfile in your favorite text editor (such as vim or nano). You need to use sudo with the editor command because/etc/sysconfig/bash-prompt-xtermis owned byroot.Add the following line to the file.
echo -ne "\033]0;${USER}@${NICKNAME}:${PWD/#$HOME/~}\007"
Log out and then log back in to pick up the new nickname value.
Changing the Hostname on Other Linux Distributions
The above procedures are intended for use with Amazon Linux only. For more information about other Linux distributions, see their specific documentation and the following articles:

