Migrate to RunxBuild and earn up to $50 in hosting credit on your first deposit.

Calculate your savings
unxBuild

Set the Hostname in Linux: hostnamectl and the /etc/hosts Two-Step

Sean

Platform Writer

Jul 06, 2026
5 min read

Set the hostname in Linux with sudo hostnamectl set-hostname newname, then update /etc/hosts to map 127.0.1.1 to the same name. Skip the second step and sudo breaks with a ‘unable to resolve host’ warning on every invocation. The two commands together are the right pattern. The team that runs both has the right setup.

Set the Hostname in Linux: hostnamectl and the /etc/hosts Two-Step

Table of contents

The two-step procedure

The right procedure is the two commands together. First, set the hostname:

sudo hostnamectl set-hostname newname

Second, edit /etc/hosts:

sudo nano /etc/hosts

Find the line:

127.0.1.1   oldname

Change oldname to newname. Do not touch 127.0.0.1 localhost.

The right verification:

hostname
hostname -f

If hostname -f errors, /etc/hosts is wrong.

What hostnamectl does

hostnamectl updates three things:

  • The kernel hostname (read by uname -n)
  • The static hostname file at /etc/hostname
  • The systemd-hostnamed daemon’s internal state

The change is immediate. The right answer is that no reboot or service restart is needed.

The wrong answer is to expect hostnamectl to update /etc/hosts. The systemd team has explicitly chosen not to do this. The right answer is to update /etc/hosts by hand.

The three hostname types

Systemd tracks three hostname types. static is the persistent one in /etc/hostname. pretty is the human-readable form (can include spaces). transient is set by DHCP or mDNS.

The right answer for a server is to set the static hostname. The right answer for a workstation is to set both static and pretty. The right verification is hostnamectl status, which shows all three plus the chassis type and machine ID.

The /etc/hosts trap

/etc/hosts is the local name-to-IP map that predates DNS. Many services (sudo, apt, postfix) consult it before hitting DNS. If the kernel hostname is newname but /etc/hosts still says 127.0.1.1 oldname, sudo logs:

sudo: unable to resolve host newname: Name or service not known

The fix is the /etc/hosts edit. Sudo still works, but the warning appears on every invocation.

Cloud-init override

On cloud servers, cloud-init resets the hostname on every boot. The right answer is to either set the hostname through the cloud API or to disable cloud-init’s hostname management by setting preserve_hostname: true in /etc/cloud/cloud.cfg.

Verifying the change

Three commands:

hostname
hostname -f
uname -n

hostname returns the short hostname. hostname -f returns the FQDN. uname -n is the same as hostname.

The right answer is to also check getent hosts newname, which queries the NSS like most applications do. The expected result is 127.0.1.1.

FAQ

Does the change survive a reboot?

Yes, if you updated /etc/hostname and /etc/hosts. Cloud-init can override it on cloud boxes. The right fix for a cloud box is preserve_hostname: true in /etc/cloud/cloud.cfg.

Can I use a hostname with dots?

Yes. hostnamectl set-hostname newname.example.com sets the FQDN. The short hostname is the part before the first dot. The right answer for tools that expect a short hostname is hostname -s.

What is the difference between hostname and uname -n?

Nothing. Both query the kernel hostname. Use either.

Why does the prompt not update?

The prompt is set by the shell’s PS1, read once at startup. Open a new terminal to see the change.

Can I have a hostname with uppercase letters?

Technically yes. The right answer is lowercase only, because DNS is case-insensitive in lookup but case-preserving in records.

How do I disable password authentication too?

Edit /etc/ssh/sshd_config and set PasswordAuthentication no, then sudo systemctl reload sshd. The right answer is to do this after key-based auth is working, to avoid locking yourself out.

What is a chassis type?

The chassis type describes the form factor: laptop, desktop, server, vm, container, etc. The right answer for a server is server or vm. The right answer is to set it correctly in /etc/machine-info if the auto-detection is wrong.

How do I see all the hostnames on a system?

hostnamectl status shows all three: static, pretty, transient. The right answer for a script is to use hostname for the static and hostname -f for the FQDN.

If you are sizing the infrastructure for the kind of project this post covers, the RunxBuild hosting calculator is the right place to model the line items. The compute, the memory, the storage, the bandwidth, the database - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers. The RunxBuild dashboard is where the team sees the actual usage in one place.

Useful related references:

#linux#guide#dev-infra#tutorial