Menu
Amazon Elastic Compute Cloud
User Guide for Linux Instances

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 IPv4 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 IPv4 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 IPv4 address changes, and so does your public DNS name, system hostname, and shell prompt. Instances launched into EC2-Classic also receive a new private IPv4 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.

  1. On your instance, open the /etc/sysconfig/network configuration file in your favorite text editor and change the HOSTNAME entry to reflect the fully qualified domain name (such as webserver.mydomain.com).

    HOSTNAME=webserver.mydomain.com
  2. Reboot the instance to pick up the new hostname.

    [ec2-user ~]$ sudo reboot
  3. Log 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 ~]$ hostname
    webserver.mydomain.com

To change the system hostname without a public DNS name

  1. Open the /etc/sysconfig/network configuration file in your favorite text editor and change the HOSTNAME entry to reflect the desired system hostname (such as webserver).

    HOSTNAME=webserver.localdomain
  2. Open the /etc/hosts file in your favorite text editor and change the entry beginning with 127.0.0.1 to match the example below, substituting your own hostname.

    127.0.0.1 webserver.localdomain webserver localhost4 localhost4.localdomain4
  3. Reboot the instance to pick up the new hostname.

    [ec2-user ~]$ sudo reboot
  4. Log 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 ~]$ hostname
    webserver.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

  1. Create a file in /etc/profile.d that sets the environment variable called NICKNAME to the value you want in the shell prompt. For example, to set the system nickname to webserver, execute the following command.

    [ec2-user ~]$ sudo sh -c 'echo "export NICKNAME=webserver" > /etc/profile.d/prompt.sh'
  2. Open the /etc/bashrc file in your favorite text editor (such as vim or nano). You need to use sudo with the editor command because /etc/bashrc is owned by root.

  3. 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 windows

    And change the \h (the symbol for hostname) in that line to the value of the NICKNAME variable.

      # 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
  4. (Optional) To set the title on shell windows to the new nickname, complete the following steps.

    1. Create a file called /etc/sysconfig/bash-prompt-xterm.

      [ec2-user ~]$ sudo touch /etc/sysconfig/bash-prompt-xterm
    2. Make the file executable with the following command.

      [ec2-user ~]$ sudo chmod +x /etc/sysconfig/bash-prompt-xterm
    3. Open the /etc/sysconfig/bash-prompt-xterm file in your favorite text editor (such as vim or nano). You need to use sudo with the editor command because /etc/sysconfig/bash-prompt-xterm is owned by root.

    4. Add the following line to the file.

      echo -ne "\033]0;${USER}@${NICKNAME}:${PWD/#$HOME/~}\007"

  5. 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: