Change a Linux server's hostname

By default, your server is started with the server's given name as the hostname.
Some software such as cPanel® requires a valid fully qualified domain name (FQDN) for the hostname
to be used during their licensing verification system. This article describes how to change a server hostname
in Linux®.

Change a server's hostname

  1. Check existing hostname

    $ hostname
  2. Modify the value to match your FQDN hostname,
    as shown in the following example:

    $  hostnamectl set-hostname NEW-HOST
    $  hostname
  3. Open the file at /etc/hosts. To update the information for internal
    networking, change the host that is associated with the main IP address
    for your server, as shown in the following example:

    $  vim /etc/hosts      localhost localhost.localdomain   hostname
  4. Change the domain name (where required)

    $  vim /etc/resolv.conf
    domain            <--- This would be the domain.
  5. Change networking configuration /etc/sysconfig/network

    $  vim /etc/sysconfig/network
    FORWARD_IPV4=false   <---- change this bit (FQDN)          <---- change this bit if the domain name has changed
  6. Change networking configuration /etc/sysconfig/network

    $  vim /etc/hostname
  7. Update the Mail Transfer Agent (MTA) configuration.

    $  vim /etc/postfix/mydomains
    mydomains - included a list of aliases for your machine here, formatted:                                   OK
    After change, run 'postmap /etc/postfix/mydomains'
    localhost                                     OK                       OK <------ commented out                           OK <----- added in

    Update the hostname in Postfix if it is hadcoded in and restart the service

    $  vim /etc/postfix/
    $  postmap /etc/postfix/
    $  systemctl restart postfix

    NOTE: This file can have quite a bit of information. You can grep for 'hostname' for faster results.

    $  grep 'hostname' /etc/postfix/main.cnf
    $  cat /etc/postfix/main.cnf | grep hostname
  8. Update MySQL, MariaDB or Percona - Check the user table for the old hostname.

    $  mysql -e "SELECT DISTINCT host FROM mysql.user;"

    If binary logging is enabled and the log files named using the original hostname then you must update /etc/my.cnf or binary logging will break and possibly have replication. You must specify the filename of the existing files, i.e. the old hostname

    $  grep  -E 'log-bin|relay-log' /etc/my.cnf /etc/mysql/my.cnf
  9. Restart syslog

    $  service rsyslog restart    
    $  systemctl restart rsyslog

Related Articles

Use the Feedback tab to make any comments or ask questions. You can also start a conversation with us.