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
    NEW-HOST
    
  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    
    127.0.0.1      localhost localhost.localdomain
    123.45.67.89   hostname.domain.com   hostname
    
  4. Change the domain name (where required)

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

    $  vim /etc/sysconfig/network
    NETWORKING=yes
    FORWARD_IPV4=false
    HOSTNAME=hostname.domainname.com   <---- change this bit (FQDN)
    DOMAINNAME=domainname.com          <---- change this bit if the domain name has changed
    GATEWAY=172.0.0.1
    GATEWAYDEV=eth0
    
  6. Change networking configuration /etc/sysconfig/network

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

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

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

    $  vim /etc/postfix/main.cf
    $  postmap /etc/postfix/main.cf
    $  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
    log-bin=/var/lib/mysqllogs/oldlogfilename
    relay-log=/var/lib/mysqllogs/oldrelaylogfilename
    
  9. Restart syslog

    $  service rsyslog restart    
    or
    $  systemctl restart rsyslog
    

Related Articles


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