Home » Shell Scripting » BASH » Linux Change Hostname Example

About Toni

Toni

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.

(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a sysadmin Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Introduction to NGINX
2. Apache HTTP Server Cookbook
3. VirtualBox Essentials
4. Nagios Monitoring Cookbook
5. Linux BASH Programming Cookbook
6. Postgresql Database Tutorial
and many more ....
I agree to the Terms and Privacy Policy
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
4 years ago

hostnamectl set-hostname geeklab