How do you permanently change an Linux server's hostname on EC2 so that it doesn't get reset by package updates?

When I install Ubuntu 16 package updates on an EC2 server with:

sudo apt-get upgrade

I'll notice that often, after I reboot, my hostname will reset to something like:


How do I prevent this? I manually set my custom hostname by doing:

sudo hostname myname
sudo bash -c "echo myname > /etc/hostname"
sudo bash -c "echo myname >> /etc/hosts"

and this sticks across reboots, but not across updates.


Try using hostnamectl instead:

sudo hostnamectl set-hostname myname

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.