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 startThis 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?
72 Answers
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_HEREAlso you should recreate your ssh keys on your machine:
sudo rm /etc/ssh/ssh_host_*
sudo dpkg-reconfigure openssh-serverJust in case, check references of your old hostname on /etc and fix it:
sudo grep -R OLD_NAME /etc/ 2>/dev/null 1 Edit the file /etc/hostname.
From man 5 hostname:
6It should contain a single newline-terminated hostname string.