Here is an example of changing the hostname in Linux.
First, switch to the root user or add “sudo” before the commands.
Let’s see the current hostname:
hostname uname -n sysctl kernel.hostname
Let’s see what hostname is specified in the configuration file:
When the system is started, the hostname is set from the /etc/hostname file, so change it by opening the file in any text editor, for example nano:
Or replace it with a new one:
echo NEW_HOSTNAME > /etc/hostname
In order for the new hostname to take effect, you can reboot the server or change it immediately:
Both previous commands set the hostname before the system is restarted, and after the startup the system takes the hostname from the file /etc/hostname.