Query and change the system hostname.

Display operating system.

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 10 (buster)
Release:        10
Codename:       buster

Display hostname settings.

$ hostnamectl
   Static hostname: debian
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 5b1a78753cf747d9b9250ba8ba0d737d
           Boot ID: c1989633a9a34eaf8a9d725b8ac0cf6e
    Virtualization: oracle
  Operating System: Debian GNU/Linux 10 (buster)
            Kernel: Linux 4.19.0-6-amd64
      Architecture: x86-64

Persistent hostname

Set static (persistent) hostname that will be preserved across a reboot.

$ sudo hostnamectl --static set-hostname debian.localhost.localdomain

Display current static hostname.

$ hostnamectl --static

hostname file will be updated accordingly.

$ cat /etc/hostname

Remember to update /etc/hosts static table lookup for hostnames.

Transient hostname

Transient hostname can be used only in a specific situation where persistent hostname is set to localhost, localhost.localdomain or ends with these names (like debian.localhost.localdomain). See additional notes at the end.

Set transient (not persistent) hostname that will be overwritten after system reboot.

$ sudo hostnamectl --transient set-hostname debian

Current hostname will be updated immediately.

$ hostname
$ sudo sysctl kernel.hostname
kernel.hostname = debian

Static hostname will be not altered, so this change will be not preserved across a reboot.

$ hostnamectl --static
$ cat /etc/hostname

Display current transient hostname.

$ hostnamectl --transient

Display hostname settings to notice additional line with transient hostname.

$ hostnamectl
   Static hostname: debian.localhost.localdomain
Transient hostname: debian
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 5b1a78753cf747d9b9250ba8b  a0d737d
           Boot ID: 29280923e1624358b2b485af48d4c5e7
    Virtualization: oracle
  Operating System: Debian GNU/Linux 10 (buster)
            Kernel: Linux 4.19.0-6-amd64
      Architecture: x86-64

Remember to update /etc/hosts static table lookup for hostnames.

Supplementary settings

Set pretty hostname.

$ sudo hostnamectl --pretty set-hostname "My computer"

Set host icon.

$ sudo hostnamectl set-icon-name linux-vm

Set host chassis.

$ sudo hostnamectl set-chassis vm

Set host deployment.

$ sudo hostnamectl set-deployment development

Set host location.

$ sudo hostnamectl set-location "Berlin, Germany"

These settings are stored in /etc/machine-info file.

$ cat /etc/machine-info
LOCATION="Berlin, Germany"

Use these settings at your discretion.

System service

Functionality described here is provided by the Hostname Service.

$ sudo systemctl status systemd-hostnamed.service
● systemd-hostnamed.service - Hostname Service
   Loaded: loaded (/lib/systemd/system/systemd-hostnamed.service; static; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:systemd-hostnamed.service(8)
Dec 18 21:03:34 debian systemd[1]: Started Hostname Service.
Dec 18 21:03:34 debian systemd-hostnamed[1409]: Changed pretty host name to 'My computer'
Dec 18 21:04:05 debian systemd[1]: systemd-hostnamed.service: Succeeded.
Dec 18 21:09:58 debian systemd[1]: Starting Hostname Service...
Dec 18 21:09:58 debian systemd[1]: Started Hostname Service.
Dec 18 21:09:58 debian systemd-hostnamed[1566]: Changed icon name to 'linux-vm'
Dec 18 21:10:14 debian systemd-hostnamed[1566]: Changed chassis to 'vm'
Dec 18 21:10:19 debian systemd-hostnamed[1566]: Changed deployment to 'development'
Dec 18 21:10:26 debian systemd-hostnamed[1566]: Changed location to 'Berlin, Germany'
Dec 18 21:10:56 debian systemd[1]: systemd-hostnamed.service: Succeeded.

Additional notes

Inspect following code snippets to understand in detail how it works.

Source file src/hostname/hostnamed.c

static int context_update_kernel_hostname(Context *c) {
        const char *static_hn;
        const char *hn;
        static_hn = c->data[PROP_STATIC_HOSTNAME];
        /* /etc/hostname with something other than "localhost"
         * has the highest preference ... */
        if (hostname_is_useful(static_hn))
                hn = static_hn;
        /* ... the transient host name, (ie: DHCP) comes next ... */
        else if (!isempty(c->data[PROP_HOSTNAME]))
                hn = c->data[PROP_HOSTNAME];
        /* ... fallback to static "localhost.*" ignored above ... */
        else if (!isempty(static_hn))
                hn = static_hn;
        /* ... and the ultimate fallback */
                hn = FALLBACK_HOSTNAME;
        if (sethostname_idempotent(hn) < 0)
                return -errno;
        (void) nscd_flush_cache(STRV_MAKE("hosts"));
        return 0;
static bool hostname_is_useful(const char *hn) {
        return !isempty(hn) && !is_localhost(hn);

Source file src/basic/hostname-util.c

bool is_localhost(const char *hostname) {
        /* This tries to identify local host and domain names
         * described in RFC6761 plus the redhatism of localdomain */
        return strcaseeq(hostname, "localhost") ||
               strcaseeq(hostname, "localhost.") ||
               strcaseeq(hostname, "localhost.localdomain") ||
               strcaseeq(hostname, "localhost.localdomain.") ||
               endswith_no_case(hostname, ".localhost") ||
               endswith_no_case(hostname, ".localhost.") ||
               endswith_no_case(hostname, ".localhost.localdomain") ||
               endswith_no_case(hostname, ".localhost.localdomain.");

Source file meson.build

conf.set_quoted('FALLBACK_HOSTNAME', get_option('fallback-hostname'))

Source file meson_options.txt

option('fallback-hostname', type : 'string', value : 'localhost',
       description : 'the hostname used if none configured')

Source file src/hostname/hostnamed.c

r = parse_env_file(NULL, "/etc/machine-info",
                           "PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME],
                           "ICON_NAME", &c->data[PROP_ICON_NAME],
                           "CHASSIS", &c->data[PROP_CHASSIS],
                           "DEPLOYMENT", &c->data[PROP_DEPLOYMENT],
                           "LOCATION", &c->data[PROP_LOCATION]);