• Get application security done the right way! Detect, Protect, Monitor, Accelerate, and more…
  • One of the essential tasks for a system administrator is to know how to change the hostname.

    If you have entered the wrong hostname during installation or requirement to change it, you can change it as below. The following is tested on Ubuntu and CentOS, both.

    Changing hostname involves three steps.

    • Change HOSTNAME value in /etc/sysconfig/network
    • Change hostname in /etc/hosts
    • Updating hostname on the terminal

    1. Display Hostname

    Let’s find out the configured name by executing hostname on the terminal.

    [[email protected] ~]# hostname
    centos-s-1vcpu-2gb-lon1-01
    [[email protected] ~]#

    Let’s change to geekflare

    2. Change Hostname

    Edit /etc/sysconfig/network file using vi, look for HOSTNAME= and update that to the desired name.

    Note: I’ve noticed on some Cloud VM that HOSTNAME entry doesn’t exist. If you don’t find this on your server then you may ignore this step.

    Before change:

    # cat network
    NETWORKING=yes
    NETWORKING_IPV6=no
    HOSTNAME=centos-s-1vcpu-2gb-lon1-01

    After change:

    # cat network
    NETWORKING=yes
    NETWORKING_IPV6=no
    HOSTNAME=geekflare
    [[email protected] sysconfig]#

    The next step would change the hostname in a hosts file.

    Edit /etc/hosts file, look for current hostname in 127.0.0.1 and server IPs (if any) line and update that to the desired one which you configured above.

    Before change:

    [[email protected] ~]# cat /etc/hosts | grep -v '^#'
    127.0.0.1 centos-s-1vcpu-2gb-lon1-01 centos-s-1vcpu-2gb-lon1-01
    127.0.0.1 localhost.localdomain localhost
    127.0.0.1 localhost4.localdomain4 localhost4
    
    ::1 centos-s-1vcpu-2gb-lon1-01 centos-s-1vcpu-2gb-lon1-01
    ::1 localhost.localdomain localhost
    ::1 localhost6.localdomain6 localhost6
    
    [[email protected] ~]#

    After change:

    [[email protected] ~]# cat /etc/hosts | grep -v '^#'
    127.0.0.1 geekflare
    127.0.0.1 localhost.localdomain localhost
    127.0.0.1 localhost4.localdomain4 localhost4
    
    ::1 centos-s-1vcpu-2gb-lon1-01 centos-s-1vcpu-2gb-lon1-01
    ::1 localhost.localdomain localhost
    ::1 localhost6.localdomain6 localhost6
    
    [[email protected] ~]#

    3. Change hostname on terminal

    And, finally, execute the hostname command with the desired value.

    [[email protected] ~]# hostname geekflare

    Try to re-login to your server and you’ll notice the new hostname.

    Using DigitalOcean Server?

    If you would like to change the hostname on the DigitalOcean VM, then you also have to update the hostname in /etc/hostname file.

    Here is a quick video demonstration.

    That was easy. Isn’t it? Interested in learning more about Linux? Check out this administration course.