How to change a hostname in Linux

Use the hostnamectl command to change your hostname on a Linux computer.
26 readers like this.
Person using a laptop

To change your hostname on a Linux computer, use the hostnamectl command.

For example, to change your computer's hostname to myhostname:

$ sudo hostnamectl --static \
--transient \
--pretty \
myhostname

What is a hostname? 

Computers have network addresses, but they're usually difficult for humans to remember. Hostnames are labels intended to help humans refer to a specific computer. Instead of navigating to 93.184.216.34, for instance, you navigate to www . example . com.

Read Alan Formy-Duval's article What's in a hostname? for more information about hostnames.

What to read next
Seth Kenlon
Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time.

2 Comments

I suppose this is a useful command, but most of the time, if you intend to change a device's hostname, you would like it to persist across reboots.

To do this, edit the file /etc/hostname. The SystemD hostname service reads this file and will use its content to set the hostname at system startup.

Thanks for the additional context!

The `hostnamectl` command with the options I specify causes the hostname change to persist across reboots, and edits `/etc/hostname`. The command is part of the systemd project.

I find the advantage to this command is that it changes all permutations of the hostname in one quick command.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.