BASH

Linux Change Hostname Example

The hostname are aliases or labels used to identify easily each device within a network. In Linux desktop versions, is the “computer name” that we are asked to specify during the installation. In this example, we can see how that hostname can be changed.

For this example, Linux Mint 18 has been used.
 
 
 
 
 
 

1. What is a hostname?

In a network, each device is identified with a unique IP address. When we have to deal with a mid-size network, we may have difficulties to identify each host’s IP address by head, since the IP addresses do not tell us anything about the host.

To make the identification of the hosts of a network easier, we can use “aliases”, known as hostnames. The hostnames can be compounded by a set of alphanumeric, and others, characters. So, for example, if we had a printer with a hypothetical 192.168.1.35 IP address, we could set, e.g., printer hostname for it, being able to access the device by that name (and, of course, with the IP too).

As said before, in this example, we will see how to change the hostname of our Linux devices. We will see two ways, each one with its own implications.

2. Changing the hostname with “hostname” command

The hostname command is the command used for hostname related configurations. If we execute it without any parameter, it will return the hostname of the computer. So:

hostname

Will return something like, e.g., the following:

julen-SVE1511V1EW

There are more options for the hostname for showing information, like DNS domain name or FQDN (Fully Qualified Domain Name), but which that not apply for this example.

2.1. Changing the hostname

Note: this method applies the changes immediately, but they’ll be lost after system restart.

For changing the hostname with hostname command, we just have to execute it, just passing the wanted new hostname as parameter (with root permissions).

sudo hostname <new-hostname>

For example:

sudo hostname julen-VAIO

3. Changing the hostname manually

To modify the host name manually, we have to modify the file /etc/hostname. If we look at its content, we will just see a single line, with the name of the hostname:

cat /etc/hostname # Will return the hostname

So, to change the hostname, just open the file with your favorite editor, and overwrite it with the new hostname:

sudo vim /etc/hostname

Note: after editing the /etc/hostname, we have to restart the system to make the changes work.

After restarting the system, we can use the hostname command to check that effectively the hostname has been changed successfully.

4. Summary

This example has shown how to change the hostname for our system. We have seen two different ways: using the hostname command, which applies the changes instantly, but that won’t be saved if we restart the system; or modifying the configuration file for the hostname, which requires a system restart, but keeping the changes applied permanently.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mikhail
Mikhail
7 years ago

hostnamectl set-hostname geeklab

Back to top button