How to change the hostname on Arch Linux

Admittedly, I rarely ever need to change the hostname of a system. I have my
own standard naming convention that I use, and will set things up at the time of
installation, never to be touched again.

Recently with a new work machine, I deviated from said convention, and after a
few weeks, I had to get in there to change things back to my usual model.josh
naming.

Back in the day, to change the hostname of a system, I would take the long
road, manually editing both /etc/hostname and /etc/hosts to reflect the new
name, and then rebooting the system to get the new hostname to take effect.

Ain’t nobody got time for unnecessary reboots!

Since systemd has consumed nearly as much of the world as JavaScript has, of
course there’s a hostname service that makes changing the hostname quick and
easy.

First, let’s check “status” of the hostname service, which will tell us the
current hostname as well as some other fun details:

% hostnamectl status
 Static hostname: crappy-hostname
       Icon name: computer-laptop
         Chassis: laptop  
      Machine ID: 99cf0606b06d847e24288fc4241d2c61
         Boot ID: 25f442dbd5dc430c91dee89ef5df8ed8
Operating System: Arch Linux
          Kernel: Linux 5.16.14-arch1-1
    Architecture: x86-64
 Hardware Vendor: Dell Inc.
  Hardware Model: XPS 15 7590

Then to actually set the hostname we run the following:

% hostnamectl set-hostname better-hostname

Which doesn’t give us any feedback, so we can check the status again:

% hostnamectl status
 Static hostname: better-hostname
       Icon name: computer-laptop
         Chassis: laptop  
      Machine ID: 99cf0606b06d847e24288fc4241d2c61
         Boot ID: 25f442dbd5dc430c91dee89ef5df8ed8
Operating System: Arch Linux
          Kernel: Linux 5.16.14-arch1-1
    Architecture: x86-64
 Hardware Vendor: Dell Inc.
  Hardware Model: XPS 15 7590

Nothing to it, the hostname has been updated and is immediately available
without a reboot!

Keep in mind that it doesn’t appear that /etc/hosts gets updated, and when I
checked, it still had the original hostname in there. Fortunately, there is a
module (nss-myhostname), that is provided by systemd that handles local
hostname resolution, making the /etc/hosts file unnecessary for this
scenario.

Also, if you’ve happened upon this post and you’re not running Arch Linux, keep
in mind that systemd is available on just about every Linux distro these days
(Debian, Ubuntu, et al) so this method should work just fine on other systems.

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.