Locahl
Get Locahl
hosts fileUbuntusystemd-resolvednanotutorial

How to Edit the Hosts File on Ubuntu (2026)

Edit /etc/hosts on Ubuntu 24.04 with nano, understand systemd-resolved interaction, flush DNS cache, and fix entries that systemd-resolved ignores.

L

Locahl Team

Β·Updated Β·7 min read

Ubuntu uses the standard Linux hosts file at /etc/hosts, but its default DNS stack β€” systemd-resolved β€” adds a layer that confuses many developers. Entries appear to save correctly, yet browsers and some tools still resolve the old IP. This guide covers Ubuntu-specific steps from edit to verification on Ubuntu 22.04 and 24.04 LTS.

See also: Main guide Β· General Linux guide

Ubuntu hosts file location

TEXT
/etc/hosts

Same path as every Linux distribution. Owned by root, mode 644.

Default Ubuntu /etc/hosts

A typical Ubuntu 24.04 installation:

TEXT
127.0.0.1 localhost
127.0.1.1 ubuntu-desktop

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Why 127.0.1.1 instead of 127.0.0.1 for hostname?

Ubuntu deliberately maps $(hostname) to 127.0.1.1 rather than 127.0.0.1. This prevents some applications from confusing the machine hostname with the localhost service address. Do not change or remove this line unless you understand the implications.

Step-by-step edit on Ubuntu

1. Back up

BASH
sudo cp /etc/hosts /etc/hosts.backup

2. Edit with nano

BASH
sudo nano /etc/hosts

Add your entries at the bottom:

TEXT
# Local dev β€” Project Phoenix
127.0.0.1    phoenix.test
127.0.0.1    api.phoenix.test
::1          phoenix.test

Save: Ctrl+O, Enter, Ctrl+X

3. Flush systemd-resolved cache

This is the step most Ubuntu users skip:

BASH
sudo resolvectl flush-caches

On Ubuntu 20.04 or systems with older systemd:

BASH
sudo systemd-resolve --flush-caches

4. Verify with resolvectl

BASH
resolvectl query phoenix.test

Expected output:

TEXT
phoenix.test: 127.0.0.1
             ::1

-- Information acquired via protocol DNS in 2.3ms.
-- Data is authenticated: no

Look for the IP you configured. If it shows the wrong address, the cache was not flushed or the entry has a syntax error.

Alternative verification:

BASH
getent hosts phoenix.test
ping -c 1 phoenix.test

Understanding systemd-resolved on Ubuntu

Ubuntu desktop and server editions use systemd-resolved as the local DNS stub resolver. It:

1. Reads /etc/hosts for static mappings 2. Caches DNS query results 3. Forwards external queries to upstream DNS (from DHCP or /etc/systemd/resolved.conf)

How resolution order works

/etc/nsswitch.conf on Ubuntu typically contains:

TEXT
hosts: files mdns4_minimal [NOTFOUND=return] dns myhostname

This means:

1. files β€” check /etc/hosts first 2. mdns4_minimal β€” mDNS for .local domains 3. dns β€” query systemd-resolved β†’ upstream DNS 4. myhostname β€” resolve local hostname

Your hosts file entries at step 1 should take priority β€” unless cached stale data interferes.

Check systemd-resolved status

BASH
resolvectl status

Shows active DNS servers, domains, and cache state.

View what systemd-resolved knows about a domain

BASH
resolvectl query example.com

For hosts file entries, data source shows as local/unauthenticated.

Ubuntu-specific troubleshooting

Entry works in ping but not in browser

Cause: Browser DNS cache or DNS-over-HTTPS.

Fixes: 1. sudo resolvectl flush-caches 2. Chrome: chrome://net-internals/#dns β†’ Clear host cache 3. Disable "Use secure DNS" in Chrome/Firefox settings temporarily

Entry ignored entirely

Check syntax:

BASH
cat /etc/hosts | grep -v '^#' | grep -v '^$'

Each line must have IP followed by at least one space/tab and a hostname.

Check for duplicate entries β€” first match wins:

BASH
grep phoenix.test /etc/hosts

On Ubuntu, /etc/resolv.conf usually symlinks to systemd-resolved stub:

BASH
ls -la /etc/resolv.conf
# lrwxrwxrwx ... /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

This is normal. Do not replace it manually unless you disable systemd-resolved.

Netplan and custom DNS

If you configured custom DNS via Netplan (/etc/netplan/*.yaml), upstream DNS changes do not affect hosts file entries β€” but they affect domains not in /etc/hosts.

Snap applications and hosts file

Some Snap apps run in confinement and may not read the host /etc/hosts the same way. Test with a non-Snap browser first.

Changes lost after reboot

Unusual on Ubuntu unless:

  • Cloud-init regenerates /etc/hosts on cloud instances
  • Configuration management (Ansible, Puppet) overwrites the file

For cloud-init managed systems, add entries to /etc/cloud/templates/hosts.debian.tmpl or use cloud-init manage_etc_hosts configuration.

Cloud Ubuntu (AWS, GCP, Azure)

Default cloud images may regenerate /etc/hosts on reboot via cloud-init. To persist custom entries:

BASH
# Check if cloud-init manages hosts
grep manage_etc_hosts /etc/cloud/cloud.cfg

Add persistent entries via cloud-init user-data or modify the template:

BASH
sudo nano /etc/cloud/templates/hosts.debian.tmpl

Docker on Ubuntu

Docker containers have isolated /etc/hosts. Pass host mappings at runtime:

BASH
docker run --add-host phoenix.test:host-gateway myimage

host-gateway resolves to the host machine IP from inside the container.

Example configurations

Local web stack (Nginx + PHP):

TEXT
127.0.0.1    myapp.test
127.0.0.1    www.myapp.test
127.0.0.1    api.myapp.test

Block social media:

TEXT
0.0.0.0    facebook.com
0.0.0.0    www.facebook.com
0.0.0.0    twitter.com
0.0.0.0    www.twitter.com

See Block a website with the hosts file.

Point production domain to staging server:

TEXT
# REMOVE AFTER LAUNCH β€” added 2026-06-11
203.0.113.50    client.com
203.0.113.50    www.client.com

LXD and Multipass VMs

Ubuntu developers using LXD or Multipass for isolated environments should know that each VM has its own /etc/hosts. The host Ubuntu system's entries do not propagate automatically.

BASH
# On the host β€” for host browser access
sudo nano /etc/hosts

# Inside the VM β€” for services running in the VM
lxc exec mycontainer -- nano /etc/hosts
# or
multipass shell myvm
sudo nano /etc/hosts

Document which domains resolve where in your project README to avoid the common mistake of editing only the host file while the service runs inside a VM.

PHP, Nginx, and Apache on Ubuntu

Local LAMP/LEMP stacks on Ubuntu pair hosts entries with virtual host configuration:

NGINX
# /etc/nginx/sites-available/myapp.test
server {
    listen 80;
    server_name myapp.test www.myapp.test;
    root /var/www/myapp/public;
}
TEXT
# /etc/hosts
127.0.0.1    myapp.test
127.0.0.1    www.myapp.test

Enable the site (sudo ln -s), reload Nginx (sudo systemctl reload nginx), flush DNS, and browse to http://myapp.test. The hosts file provides name resolution; the web server provides routing.

Journalctl and debugging resolution

When entries seem ignored, inspect systemd-resolved logs:

BASH
journalctl -u systemd-resolved -f
# In another terminal:
resolvectl query myapp.test

Look for cache hits, DNSSEC validation skips, and "Using degraded feature set" warnings that indicate fallback behavior.

Ubuntu Server vs Desktop

Ubuntu Server installations may not include systemd-resolved by default on older releases. Check which resolver is active:

BASH
systemctl is-active systemd-resolved

If inactive, /etc/hosts changes apply immediately with no flush step β€” but also verify /etc/nsswitch.conf still lists files before dns.

Pairing with /etc/hostname

Ubuntu sets the machine name in /etc/hostname and maps it in /etc/hosts via the 127.0.1.1 line. If you rename your machine, update both files consistently:

BASH
sudo hostnamectl set-hostname new-name
sudo nano /etc/hosts   # update 127.0.1.1 line to match

Mismatch between hostname and hosts mapping can cause sudo warnings and slow SSH connections β€” unrelated to dev entries but worth fixing during the same editing session.

Best practices on Ubuntu

  • Always sudo cp /etc/hosts /etc/hosts.backup before bulk edits
  • Run sudo resolvectl flush-caches after every change β€” add to shell aliases
  • Use resolvectl query instead of ping for authoritative verification
  • Comment entries with dates for temporary overrides
  • Use .test not .local β€” .local triggers mDNS on Ubuntu
  • Keep a project README section with required hosts entries for your team

---

*Last tested: Ubuntu 24.04 LTS β€” June 2026.*

Share this article
Available on Windows, macOS & Linux

Ready to simplify your workflow?

Stop wasting time with the terminal. Locahl lets you manage your hosts file in a few clicks, with automatic validation and no risk of errors.

  • Intuitive visual interface
  • Automatic DNS flush
  • Multi-environment management
  • Automatic backups
  • JSON Import/Export
Get Locahl β€” $5.99One-time payment, no subscription

Reader Reviews

4.7β˜…(3 reviews)
Samuel O.
β˜…β˜…β˜…β˜…β˜…

"The systemd-resolved section explained why my entries worked in ping but not in Chrome. resolvectl query was the key."

June 3, 2026

Priya N.
β˜…β˜…β˜…β˜…β˜…

"Perfect for Ubuntu 24.04. Clear steps from backup to verification."

June 6, 2026

Lucas F.
β˜…β˜…β˜…β˜…β˜…

"Great Ubuntu-specific guide. The 127.0.1.1 hostname line explanation was helpful."

June 8, 2026

Frequently Asked Questions

Where is the hosts file on Ubuntu?

/etc/hosts β€” same location as all Linux distributions. Writable only with sudo.

Why does Ubuntu have a 127.0.1.1 line in /etc/hosts?

Ubuntu maps your machine hostname to 127.0.1.1 (not 127.0.0.1) to avoid conflicts with applications that use 127.0.0.1 for localhost services. Do not remove this line.

How do I flush DNS on Ubuntu 24.04?

Run: sudo resolvectl flush-caches. On older Ubuntu with systemd-resolve: sudo systemd-resolve --flush-caches.

Does systemd-resolved override /etc/hosts?

No β€” systemd-resolved reads /etc/hosts and uses it for local resolution. However, its cache may serve stale results until flushed.

How do I check if systemd-resolved is using my hosts entry?

Run: resolvectl query yourdomain.test β€” look for "Data from: local" in the output.

Can I disable systemd-resolved on Ubuntu?

You can, but it is not recommended on desktop Ubuntu. If you disable it, configure /etc/resolv.conf manually and use nscd or no cache daemon.

Related Articles

7 min read
hosts fileLinux/etc/hosts

How to Edit the Hosts File on Linux (2026)

Edit /etc/hosts on Linux with nano or vim: permissions, syntax, DNS flush on systemd and legacy systems, and troubleshooting for developers.

L

Locahl Team

7 min read
hosts fileWindows 10tutorial

How to Edit the Hosts File on Windows 10 (2026)

Edit the Windows 10 hosts file step by step: Notepad as administrator, file location, UAC, save errors, DNS flush, and troubleshooting access denied.

L

Locahl Team

7 min read
hosts fileWindows 11PowerShell

How to Edit the Hosts File on Windows 11 (2026)

Three methods to edit the Windows 11 hosts file: Notepad as admin, PowerShell, and VS Code. Includes UAC tips, DNS flush, and troubleshooting for 24H2.

L

Locahl Team

7 min read
hosts fileblock websitead blocking

How to Block a Website with the Hosts File (2026)

Block any website system-wide using the hosts file. Redirect domains to 0.0.0.0 or 127.0.0.1 on Windows, Mac and Linux. Unblock mistakes and avoid pitfalls.

L

Locahl Team