How to on Linux: Change Hostname

If you’re using a Linux distro, you’ll often see the hostname (name of the machine) throughout the system. In this simple and easy tutorial, we’re going to show you how to change the hostname on a Linux distro.

This tutorial will work on all Linux distros, including Ubuntu, CentOS, Fedora, Linux Mint, etc.

All you need is a terminal (the CLI) and a sudo user/root.

To change the hostname via the GUI on Ubuntu, just go to Settings -> About -> Device Name.

You can use different commands and methods to do this, but we’ll be using the hostnamectl command, which is the easiest and works on all systemd distros.

How to change the hostname on Linux

To change the hostname, just run this command:

hostnamectl set-hostname linuxstans-new

Replace “linuxstans-new” with your desired new hostname. Allowed characters for the hostname are letters from A to Z, digits from 0 to 9, and the hyphen (-).

That’s it! It was that easy.

How to get the current hostname on Linux

To get the current hostname and other info about your system, run this command:


Which should give you an output with info like your static hostname (the first line), the OS you’re using, the kernel you’re using, etc.

You can verify if you changed the hostname with the previous command.

Static hostname: linuxstans-new
Icon name: computer-vm
Chassis: vm
Machine ID: d2112e9bc476412b9e86f82930e22961
Boot ID: f66f723d4880432e921ebe7fed61e07d
Virtualization: kvm
Operating System: Ubuntu 20.04 LTS
Kernel: Linux 5.4.0-110-generic
Architecture: x86-64

This is a sample output of a KVM Linux VPS.

If you want to get the hostname only, without all the other data, just run this command:


And it will give you an output of your hostname only. In this case:


Leave a comment

Your email address will not be published. Required fields are marked *