Ubuntu Hosts File: Find & Edit /etc/hosts

Ubuntu Hosts File: Find & Edit /etc/hosts

Updated 7 min read

/etc/hosts on Ubuntu 24.04 and 22.04 — edit with nano, flush systemd-resolved, and fix ignored entries.

Hosts file location on Ubuntu:

/etc/hosts
How many times will you retype this command this month?Automate it with ToggleHosts

Same path as every Linux distribution. Owned by root, mode 644. Ubuntu’s default DNS stack, systemd-resolved, can make edits look ignored until you flush the cache. This guide covers Ubuntu 22.04 and 24.04 LTS from edit to verification.

Next: flush DNS on Linux · /etc/hosts not working

How to edit the hosts file on Ubuntu

To edit the hosts file on Ubuntu, open /etc/hosts as root and flush systemd-resolved afterwards:

  1. Open a terminal (Ctrl + Alt + T).
  2. Run sudo nano /etc/hosts and enter your password.
  3. Add a new line with the IP address, a space, then the domain, for example 127.0.0.1 mysite.test.
  4. Save with Ctrl+O then Enter, and exit with Ctrl+X.
  5. Flush the DNS cache with sudo resolvectl flush-caches.

Verification and systemd-resolved details are below.

See also: Main guide

Ubuntu hosts file location

/etc/hosts

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

Default Ubuntu /etc/hosts

A typical Ubuntu 24.04 installation:

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

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

2. Edit with nano

sudo nano /etc/hosts

Add your entries at the bottom:

# 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:

sudo resolvectl flush-caches

On Ubuntu 20.04 or systems with older systemd:

sudo systemd-resolve --flush-caches

4. Verify with resolvectl

resolvectl query phoenix.test

Expected output:

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:

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:

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

resolvectl status

Shows active DNS servers, domains, and cache state.

View what systemd-resolved knows about a domain

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:

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:

grep phoenix.test /etc/hosts

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

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:

# 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:

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

Docker on Ubuntu

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

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):

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

Block social media:

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:

# 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.

# 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:

# /etc/nginx/sites-available/myapp.test
server {
 listen 80;
 server_name myapp.test www.myapp.test;
 root /var/www/myapp/public;
}
# /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:

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:

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:

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.*

Sources and further reading

Frequently Asked Questions

/etc/hosts, same location as all Linux distributions. Writable only with sudo.

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.

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

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

Related Articles