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.
Locahl Team
Table of Contents
- Ubuntu hosts file location
- Default Ubuntu /etc/hosts
- Why 127.0.1.1 instead of 127.0.0.1 for hostname?
- Step-by-step edit on Ubuntu
- 1. Back up
- 2. Edit with nano
- 3. Flush systemd-resolved cache
- 4. Verify with resolvectl
- Understanding systemd-resolved on Ubuntu
- How resolution order works
- Check systemd-resolved status
- View what systemd-resolved knows about a domain
- Ubuntu-specific troubleshooting
- Entry works in ping but not in browser
- Entry ignored entirely
- /etc/resolv.conf is a symlink
- Netplan and custom DNS
- Snap applications and hosts file
- Changes lost after reboot
- Cloud Ubuntu (AWS, GCP, Azure)
- Docker on Ubuntu
- Example configurations
- LXD and Multipass VMs
- PHP, Nginx, and Apache on Ubuntu
- Journalctl and debugging resolution
- Ubuntu Server vs Desktop
- Pairing with /etc/hostname
- Best practices on Ubuntu
- Related guides
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
/etc/hostsSame 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-allroutersWhy 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.backup2. Edit with nano
sudo nano /etc/hostsAdd your entries at the bottom:
# Local dev β Project Phoenix
127.0.0.1 phoenix.test
127.0.0.1 api.phoenix.test
::1 phoenix.testSave: Ctrl+O, Enter, Ctrl+X
3. Flush systemd-resolved cache
This is the step most Ubuntu users skip:
sudo resolvectl flush-cachesOn Ubuntu 20.04 or systems with older systemd:
sudo systemd-resolve --flush-caches4. Verify with resolvectl
resolvectl query phoenix.testExpected output:
phoenix.test: 127.0.0.1
::1
-- Information acquired via protocol DNS in 2.3ms.
-- Data is authenticated: noLook 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.testUnderstanding 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 myhostnameThis 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 statusShows active DNS servers, domains, and cache state.
View what systemd-resolved knows about a domain
resolvectl query example.comFor 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/etc/resolv.conf is a symlink
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.confThis 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/hostson 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.cfgAdd persistent entries via cloud-init user-data or modify the template:
sudo nano /etc/cloud/templates/hosts.debian.tmplDocker on Ubuntu
Docker containers have isolated /etc/hosts. Pass host mappings at runtime:
docker run --add-host phoenix.test:host-gateway myimagehost-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.testBlock 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.comSee 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.comLXD 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/hostsDocument 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.testEnable 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.testLook 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-resolvedIf 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 matchMismatch 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.backupbefore bulk edits - Run
sudo resolvectl flush-cachesafter every change β add to shell aliases - Use
resolvectl queryinstead ofpingfor authoritative verification - Comment entries with dates for temporary overrides
- Use
.testnot.localβ.localtriggers mDNS on Ubuntu - Keep a project README section with required hosts entries for your team
Related guides
- Edit host file β all platforms
- Linux guide (nano/vim)
- Administrator rights
- Syntax reference
- Changes not working (Mac, similar DNS issues)
- Complete reference
---
*Last tested: Ubuntu 24.04 LTS β June 2026.*
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
Reader Reviews
"The systemd-resolved section explained why my entries worked in ping but not in Chrome. resolvectl query was the key."
June 3, 2026
"Perfect for Ubuntu 24.04. Clear steps from backup to verification."
June 6, 2026
"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
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.
Locahl Team
How to Edit the Host File on Windows, Mac and Linux (2026)
Edit the host file on Windows, macOS and Linux: file location, admin rights, syntax, DNS flush, and troubleshooting when changes do not apply.
Locahl Team
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.
Locahl Team
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.
Locahl Team
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.
Locahl Team