How to change hostname in Linux

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:

cat /etc/hostname

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:

nano /etc/hostname

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:

hostname NEW_HOSTNAME

Or so:

sysctl kernel.hostname=NEW_HOSTNAME

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.

Подписаться на IT Blog (RU) по Email
Subscribe to IT Blog (EN) by Email

Did my article help you? How about buying me a cup of coffee as an encouragement? Buy me a coffe.

Leave a Reply