The hostname of a system is usually set during installation. However as time goes on, it may be determined that the hostname should be changed to something more suitable.
Outlined below is the procedure for changing the systems hostname without having to reboot.
CentOS 5 and 6
First, check to see what the existing hostname is on the server:
[[email protected] ~]# hostname
Then change the current hostname. In this example, we’re going to change the hostname to web04.mydomain.com:
[[email protected] ~]# hostname web04.mydomain.com
Now update /etc/hosts with the new hostname:
[[email protected] ~]# vim /etc/hosts
127.0.0.1 localhost.localdomain.com localhost
192.168.1.5 web04.mydomain.com web04
If you have ‘domain’ specified in /etc/resolv.conf, update that accordingly. There is not a need to add ‘domain’ if it is not already defined:
[[email protected] ~]# vim /etc/resolv.conf
domain web04.mydomain.com
nameserver 8.8.8.8
nameserver 8.8.4.4
Next update your network configuration:
[[email protected] ~]# vim /etc/sysconfig/network
...
HOSTNAME=web04.mydomain.com
...
Restart syslog so the new changes go into effect:
# CentOS 5
[[email protected] ~]# service syslog restart
# CentOS 6
[[email protected] ~]# service rsyslog restart
Finally, log out and back into the server via SSH and you should see the new hostname in effect. Also check /var/log/secure and /var/log/messages to ensure the new hostname is being used.
CentOS 7
In this example, we’re going to change the hostname to web04.mydomain.com. First run:
[[email protected] ~]# hostname web04.mydomain.com
Now update /etc/hosts with the new hostname:
[[email protected] ~]# vim /etc/hosts
127.0.0.1 localhost.localdomain.com localhost
192.168.1.5 web04.mydomain.com web04
Now update the hostname using the systemd command:
[[email protected] ~]# hostnamectl set-hostname web04.mydomain.com
If you have ‘domain’ specified in /etc/resolv.conf, update that accordingly. There is not a need to add ‘domain’ if it is not already defined:
[[email protected] ~]# vim /etc/resolv.conf
domain web04.mydomain.com
nameserver 8.8.8.8
nameserver 8.8.4.4
Now restart syslog so the new changes go into effect:
[[email protected] ~]# systemctl restart rsyslog
Finally, log out and back into the server via SSH and you should see the new hostname in effect. Also check /var/log/auth.log and /var/log/syslog to ensure the new hostname is being used.
Ubuntu 12.04 and Ubuntu 14.04
First, check to see what the existing hostname is on the server:
[[email protected] ~]# hostname
In this example, we’re going to change the hostname to web04.mydomain.com. So run:
[[email protected] ~]# hostname web04.mydomain.com
Now update /etc/hosts with the new hostname:
[[email protected] ~]# vim /etc/hosts
127.0.0.1 localhost.localdomain.com localhost
192.168.1.5 web04.mydomain.com web04
If you have ‘domain’ specified in /etc/resolv.conf, update that accordingly. There is not a need to add ‘domain’ if it is not already defined:
[[email protected] ~]# vim /etc/resolv.conf
domain web04.mydomain.com
nameserver 8.8.8.8
nameserver 8.8.4.4
Then update /etc/hostname accordingly:
[[email protected] ~]# vim /etc/hostname
web04.mydomain.com
Now restart syslog so the new changes go into effect:
[[email protected] ~]# service rsyslog restart
Finally, log out and back into the server via SSH and you should see the new hostname in effect. Also check /var/log/auth.log and /var/log/syslog to ensure the new hostname is being used.
Ubuntu 16.04 and Ubuntu 18.04
First, check to see what the existing hostname is on the server:
[[email protected] ~]# hostname
In this example, we’re going to change the hostname to web04.mydomain.com. So run:
[[email protected] ~]# hostname web04.mydomain.com
Now update /etc/hosts with the new hostname:
[[email protected] ~]# vim /etc/hosts
127.0.0.1 localhost.localdomain.com localhost
192.168.1.5 web04.mydomain.com web04
Now update the hostname using the systemd command:
[[email protected] ~]# hostnamectl set-hostname web04.mydomain.com
If you have ‘domain’ specified in /etc/resolv.conf, update that accordingly. There is not a need to add ‘domain’ if it is not already defined:
[[email protected] ~]# vim /etc/resolv.conf
domain web04.mydomain.com
nameserver 8.8.8.8
nameserver 8.8.4.4
Now restart syslog so the new changes go into effect:
[[email protected] ~]# systemctl restart rsyslog
On Ubuntu 18.04 only, check to see if /etc/cloud/cloud.cfg exists. If it does, confirm the preserve hostname is set to true as shown below:
[[email protected] ~]# vim /etc/cloud/cloud.cfg
...
preserve_hostname: true
...
Finally, log out and back into the server via SSH and you should see the new hostname in effect. Also check /var/log/auth.log and /var/log/syslog to ensure the new hostname is being used.