When you SSH into an AWS EC2 instance, you'll typically see a prompt like this:
[ec2-user@ip-10-128-80-10 ~]$
The ip-10-128-80-10
portion is the internal hostname automatically assigned by AWS. For developers managing multiple instances, this isn't the most user-friendly or memorable format.
The most robust solution is to change the system hostname itself:
sudo hostnamectl set-hostname us1
Verify the change took effect:
hostnamectl
For temporary changes or more control over the prompt format, modify the PS1
variable in ~/.bashrc
:
# Edit ~/.bashrc
nano ~/.bashrc
# Add or modify this line
export PS1="[\\u@us1 \\W]\\$ "
# Apply changes
source ~/.bashrc
For multiple instances, you might want to set hostnames based on instance tags:
#!/bin/bash
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/region)
HOSTNAME=$(aws ec2 describe-tags \
--filters "Name=resource-id,Values=$INSTANCE_ID" "Name=key,Values=Name" \
--region $REGION --output text | cut -f5)
sudo hostnamectl set-hostname "$HOSTNAME"
On older systems, you may need to edit additional files:
# Edit /etc/hosts
127.0.0.1 localhost us1
# Edit /etc/sysconfig/network (on RHEL/CentOS)
HOSTNAME=us1
After making any changes, verify with:
hostname
echo $PS1
You should now see your custom hostname in the prompt:
[ec2-user@us1 ~]$
When you SSH into an Amazon EC2 instance, you'll typically see a prompt like this:
[ec2-user@ip-10-128-80-10 ~]$
This format follows the standard Linux convention of [username@hostname working-directory]
, where the hostname defaults to the internal IP address in EC2 instances.
Customizing the hostname offers several advantages:
- Easier identification when managing multiple instances
- Better organization in team environments
- More professional appearance in screenshots/demos
For a session-specific change (won't persist after reboot):
sudo hostname us1
exec bash
The exec bash
command refreshes your shell to display the new hostname.
For a persistent change across reboots, edit these files:
On Modern Linux (systemd)
sudo hostnamectl set-hostname us1
On Older Systems
Edit /etc/hostname
:
sudo nano /etc/hostname
# Replace contents with just your desired hostname:
us1
Then modify /etc/hosts
:
127.0.0.1 localhost localhost.localdomain us1
::1 localhost localhost.localdomain us1
Check the current hostname with:
hostname
hostnamectl status
For advanced prompt customization, edit ~/.bashrc
:
PS1='[\u@\h \W]\$ '
# Or for color:
PS1='$$\e[32m$$[\u@\h \W]\$$$\e[0m$$ '
- Avoid using dots in hostnames (us1 instead of us1.example.com)
- Changes might affect some AWS services that rely on the original hostname
- Always test in a staging environment first
For new instances, set the hostname in user-data:
#cloud-config
hostname: us1
fqdn: us1.local