Setting the Hostname of Linux Workstation or Server

Photo by Ainur Iman on Unsplash

Setting the Hostname of Linux Workstation or Server

A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices.

Type:

hostname

To see where it is stored, type:

cat /etc/hostname

To get the hostname and additional information about your machine:

hostnamectl

To change the hostname, login as root user or use the sudo command:

sudo hostnamectl set-hostname my-private-workstation
💡
Restart your terminal window since the new settings will not show up.

If you do not want to restart your terminal, you can force the current terminal to be changed by typing (assuming you use bash):

exec bash

Verify the hostname was changed:

cat /etc/hostname

Check the content of the /cat/hosts file

cat /etc/hosts

Even though the hostnamectl command allows us to set the new hostname, it does not update the /etc/hosts file. This is optional, but highly recommended.

To update the /etc/hosts file, use the vim command:

sudo vim /etc/hosts/

To ping this instance, run:

ping my-private-laptop

References:

  1. Linux Crash Course - Setting the Hostname of your Linux Workstation or Server

  2. How to Change Hostname on Ubuntu

  3. How to configure a hostname on a Linux system