Ubuntu – Permanent hostname change on Ubuntu

16.04hostnamestartup

I have successfully changed the hostname of my Ubuntu machine, though whenever I restart it, I need to run this command to access it using host name

sudo /etc/init.d/hostname.sh start

This is what I used to do the change:

# take care of "hostname" command and editing "/etc/hostname"
hostnamectl set-hostname <new.hostname>
# Edit "/etc/hosts"
127.0.1.1     <new.hostname>

How can I make this permanent?

Best Answer

I followed [these instructions] and it worked even after restart the machine.

Summary:

sudo hostname NEW_NAME_HERE
sudo nano /etc/hostname # change OLD_NAME by NEW_NAME
sudo nano /etc/hosts # change OLD_NAME by NEW_NAME
sudo systemctl restart systemd-logind.service
hostnamectl set-hostname NEW_NAME_HERE

Also you should recreate your ssh keys on your machine:

sudo rm /etc/ssh/ssh_host_*
sudo dpkg-reconfigure openssh-server

Just in case, check references of your old hostname on /etc and fix it:

sudo grep -R OLD_NAME /etc/ 2>/dev/null
Related Question