Páginas

Sunday, 10 March 2019

Changing system hostname on AWS

When you log into an instance for the firs time, you'll see that it is automatically  created with a horrendous hostname based on your ip and so on.

I consider it handy to set a new name for your computer. You can follow this easy steps if you don't have a public DNS domain name.

Check your OS with this command

cat /etc/system-release
Amazon Linux release 2 (Karoo)

Step 1

For Amazon Linux 2: Use the hostnamectl command
sudo hostnamectl set-hostname "servername.localdomain"

For Amazon Linux AMI: Open /etc/sysconfig/network and set
HOSTNAME=servername.localdomain


Step 2
Open the /etc/hosts file and change the entry beginning with 127.0.0.1 to look like this
127.0.0.1 servername.localdomain servername localhost4 localhost4.localdomain4

Step 3
Reboot the instance


If you do not want to modify the hostname for your instance, but you would like to have a more useful system name when you log into your instance follow the next steps

Step 1
Create a file in /etc/profile.d that sets the environment variable called NICKNAME

sudo sh -c 'echo "export NICKNAME=servername" > /etc/profile.d/prompt.sh'

Step 2
Open /etc/bashrc (RedHat/Amazon) or /etc/bash.bashrc (Debian/Ubuntu)
and edit the file with sudo 
sudo vi /etc/bashrc

and look for the line that starts with [ "$PS1")

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "

replace it with

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@$NICKNAME \W]\\$ "

Step 3
Log out and then log back in

No comments:

Post a Comment