cPanel servers
In the event that you are wanting to change the hostname for a cPanel server, you should instead follow our cPanel guide: Change the Hostname of a cPanel Server
Requirements:
The new hostname for the server:
Must be 1-63 characters long for each segment (separated by periods), up to a total length of 253 characters.
Be composed of ASCII(7) characters, which includes alphabetic characters (a-z), numerals (0-9), periods (.), and the hyphen (-).
A hostname may not start or end with a hyphen (-).
Periods should only be used to separate domain labels.
Temporary Changes:
Temporary changes of the current active hostname can be done through the hostname command within Linux.
To see your current active hostname type hostname:
[test@testvm ~]# hostname
examplevm.servercontrol.com.auTo change your current active hostname to something else, enter it as hostname <domain> for example:
[test@testvm ~]# hostname examplevm.servercontrol.com.au
[test@testvm ~]# hostname
examplevm.servercontrol.com.au
Persistence
Whilst hostname works for a temporary change for testing purposes, this is not persistent across reboots, as the configurations files are not changed.
To make it persistent, you will need to use hostnamectl, or edit the configuration files directly. On the example system being used, if I check /etc/hostname we can see that it is still set as testvm.servercontrol.com.au instead of what I set with hostname:[test@testvm ~]# cat /etc/hostname
testvm.servercontrol.com.au
If I wanted to make this persistent on this AlmaLinux test server, I would use hostnamectl.
Persistent Changes:
Method 1: hostnamectl
Prerequisite
hostnamectl interacts with /etc/hostname and is reliant on your Linux distribution being systemd based.
You can check if your system has hostnamectl via which:
[test@testvm]# which hostnamectl
/usr/bin/hostnamectl
hostnamectl will change your hostname immediately, as well as changing the recorded hostname within /etc/hostname.
Check your current set hostname with hostname:
[test@testvm ~]# hostnameexamplevm.servercontrol.com.auSet a new hostname via hostnamectl:
[test@testvm ~]# hostnamectl set-hostname examplevm.servercontrol.com.au
Using hostname we can make sure the change is currently active.
[test@testvm ~]# hostname
examplevm.servercontrol.com.auBy checking our configuration file at /etc/hostname we can make sure that the change will be persistent across reboots.
[test@testvm ~]# cat /etc/hostname
examplevm.servercontrol.com.au
Method 2: Text editor
On non-systemd distributions you will need to edit the text file directly. There are a number of locations that it could be stored. For Red-hat systems using sysv, this historically was /etc/sysconfig/network, but has since changed to be /etc/hostname to align with systemd.
Distributions
In the event that your hostname isn’t located in either /etc/hostname or /etc/sysconfig/network, you will need to look up the location of your hostname configuration file for your OS distribution. I have put some examples below of distributions and their typical hostname file:
Debian: /etc/hostname
Ubuntu: /etc/hostname
AlmaLinux: /etc/hostname
Rocky Linux: /etc/hostname
CentOS 7/8/9: /etc/hostname
Legacy CentOS 6 and prior: /etc/sysconfig/network
Legacy RHEL 6 and prior: /etc/sysconfig/network
OpenSUSE: /etc/hostname
Fedora Server: /etc/hostname
Alpine Linux: /etc/hostname
/etc/hostname only contains the hostname, which is easy to edit:
[test@testvm ~]# cat /etc/hostname
examplevm.servercontrol.com.auBut within /etc/sysconfig/network, it is stored as as a variable, and not the only one present:
[test@testvm ~]# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=yes
IPV6_AUTOCONF=no
HOSTNAME=examplevm.servercontrol.com.au
DOMAINNAME=servercontrol.com.auAfter this has been changed and saved within the relevant file for your distribution, you will need to use hostname as described in the Temporary Changes section to set it within the current active config, as this was set during the last system boot before the change was made.
Once this has been completed, congratulations! You have changed the hostname for a Linux Server!