Contents[Show]

1. Intro

Changing a computer's name is a common thing to do.

This is how.

 

2. Guide

To change the hostname permanently, follow these steps.

 

2.1. For CentOS

  1. Edit /etc/sysconfig/network.
    # nano /etc/sysconfig/network
  2. Change the HOSTNAME.
    HOSTNAME=newHostname
  3. Save and exit.

  4. Edit /etc/hosts
    # nano /etc/hosts
  5. Add a new line.
    127.0.0.1 newHostname
  6. Save and exit.

  7. 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
  8. If restarting the network didn't or won't change the hostname for some reason, try restarting the computer.
    # shutdown -r now
  9. Confirm the name change.
    # hostname
    newHostname

 

 

2.2. For Ubuntu

  1. Edit /etc/hosts.
    # nano /etc/hosts
  2. Add a new line.
    127.0.0.1 newHostname
  3. Save and exit.

  4. 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
  5. Nope, not yet set. So we run hostnamectl again, but this time with a couple of parameters.
    # hostnamectl set-hostname newHostname
  6. 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

 

 

 

3. 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/