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
cat /etc/hosts

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
nano /etc/hosts

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.

Leave a comment

Leave a Reply

Discover more from IT Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading