If you’ve worked as a system admin for any length of time, you’ve probably run into a situation where you have to change the hostnames on your server to match some corporate naming convention, but you can’t have downtime either. So the question is how do you change the hostname without rebooting the server?
Luckily, for us changing the hostname of a Linux system is easy. It can be changed by using “hostname” command.
Changing Hostname Using hostnamectl command in Ubuntu
Current Linux distros use systemd an init system used in Linux distributions to manage system processes and the user space.
Display the current hostname
Simply use this command :
hostnamectl
Sample outputs:
Static hostname: host_name
Icon name: computer
Machine ID: bd8978fcd141f40f56c5759700000d24
Boot ID: 4ce02f818334497a85bc81c962d6bf4b
Operating System: Ubuntu 16.04.4 LTS
Kernel: Linux 4.4.0-124-generic
Architecture: x86-64
Setting or Changing the hostname
You can change or set hostname of your Linux system by running:
hostnamectl set-hostname 'new_host_name'
To verify it simply type:
hostnamectl
You have just changed your hostname using hostnamectl
command.
Setting or Changing Hostname in Linux Using hostname Command
Running hostname command without any parameters will display the current hostname of your Linux system like this:
hostname
host_name
You can change or set hostname of your Linux system, by running the command below:
hostname new_hostname
Replace “new_hostname” with the hostname you want to set. The problem with this approach is the system hostname will change immediately, but after restarting, the old hostname will be restored.
There is a solution to that effect, thus changing the hostname permanently.
Change or Set Permanent Hostname on Linux Distributions
The current versions of Linux distributions like lastest Ubuntu, Debian, CentOS, Fedora, RedHat …, comes with systemd which is a service manager that gives hostnamectl command to manage hostnames on the various Linux distributions.
Setting hostname on systemd based distributions, use hostnamectl command as shown:
$ sudo hostnamectl set-hostname new_hostname
Setting hostname on earlier Linux distributions, which uses SysVinit in short init, you can edit the hostname file located in:
# nano /etc/hostname
Then, add the record for the hostname in:
# nano /etc/hosts
For example:
127.0.0.1 new_hostname
Then, restart the hostname by running the command below:
# /etc/init.d/hostname restart
For RHEL/CentOS Linux distributions that use init, the hostname can be changed by editing:
# nano /etc/sysconfig/network
Here is a sample of that file:
/etc/sysconfig/network
NETWORKING=yes
HOSTNAME="new_hostname"
GATEWAY="192.168.0.214"
GATEWAYDEV="eth0"
FORWARD_IPV4="yes"
Changing the value next to "HOSTNAME" will make the hostname permanent.
Conclusion
I hope this article helped you in changing your hostname and you probably have learned something new to add to your knowledge.
Top comments (1)
Hi Clem
Would you like to touch base with me @agakpe@hotmail.com. Could use your services.
Thanks