Intro
Changing a computer's name is a common thing to do.
This is how.
Guide
To change the hostname permanently, follow these steps.
For CentOS
- Edit /etc/sysconfig/network.
# nano /etc/sysconfig/network
- Change the HOSTNAME.
HOSTNAME=newHostname
- Save and exit.
- Edit /etc/hosts
# nano /etc/hosts
- Add a new line.
127.0.0.1 newHostname
- Save and exit.
- Restart the network daemon.
For CentOS 6 and older:# service network restart
For CentOS 7 and newer, as well as for most Ubuntu-distros:# systemctl restart NetworkManager
- If restarting the network didn't or won't change the hostname for some reason, try restarting the computer.
# shutdown -r now
- Confirm the name change.
# hostname newHostname
For Ubuntu
- Edit /etc/hosts.
# nano /etc/hosts
- Add a new line.
127.0.0.1 newHostname
- Save and exit.
- Run hostnamectl to check the name.
# hostnamectl
Static hostname: oldHostname
Icon name: computer-desktop
Chassis: desktop
Operating System: Ubuntu 18.04.1 LTS
Kernel: Linux 4.15.0-43-generic
Architecture: x86-64 - Nope, not yet set. So we run hostnamectl again, but this time with a couple of parameters.
# hostnamectl set-hostname newHostname
- And a confirmation.
# hostnamectl
Static hostname: newHostname
Icon name: computer-desktop
Chassis: desktop
Operating System: Ubuntu 18.04.1 LTS
Kernel: Linux 4.15.0-43-generic
Architecture: x86-64
Sources
https://unix.stackexchange.com/questions/145019/how-to-change-hostname-on-centos-6-5
https://support.rackspace.com/how-to/centos-hostname-change/
https://linuxize.com/post/how-to-change-hostname-on-ubuntu-18-04/