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
Table of Contents
- What is /etc/hosts?
- Before you edit: back up
- Method 1: Edit with nano (recommended for beginners)
- Open the file
- Add entries
- Save and exit
- Method 2: Edit with vim
- Useful vim commands for hosts file management
- Hosts file syntax on Linux
- Flush DNS cache on Linux
- systemd-resolved (Ubuntu, Fedora, Debian, Arch with systemd)
- nscd (Name Service Cache Daemon)
- dnsmasq
- No cache daemon running
- Verify your entries
- ping
- getent (more reliable than ping)
- dig (shows full resolution path)
- Common problems on Linux
- Permission denied
- Changes not taking effect
- File is immutable (cannot save even with sudo)
- Docker containers ignore host /etc/hosts
- SELinux blocking edits (RHEL, Fedora)
- Distribution notes
- Automation and scripting
- NetworkManager and desktop Linux
- Container and Kubernetes development
- Flatpak and Snap considerations
- Headless servers and SSH-only access
- Best practices for Linux developers
- Related guides
On every Linux distribution, the hosts file lives at /etc/hosts. It is one of the oldest configuration files on Unix systems β present since the early days of networked computing. Editing it requires root privileges and a text editor; this guide covers nano (beginner-friendly) and vim (power user), plus DNS cache flush across different Linux setups.
For the cross-platform overview see How to edit the host file on all platforms.
What is /etc/hosts?
/etc/hosts maps hostnames to IP addresses locally, before your system queries external DNS servers. It is checked first in the name resolution order defined in /etc/nsswitch.conf.
Typical default content:
127.0.0.1 localhost
127.0.1.1 myhostname
::1 localhost ip6-localhost ip6-loopbackThe 127.0.1.1 entry (hostname mapping) is common on Debian-based systems β do not remove it without understanding your distro's conventions.
Before you edit: back up
Always create a backup before modifying system files:
sudo cp /etc/hosts /etc/hosts.backupRestore if something goes wrong:
sudo cp /etc/hosts.backup /etc/hostsMethod 1: Edit with nano (recommended for beginners)
nano is pre-installed on most Linux distributions and shows keyboard shortcuts at the bottom of the screen.
Open the file
sudo nano /etc/hostsEnter your password when prompted.
Add entries
Navigate to the bottom with arrow keys and add lines:
# Local development
127.0.0.1 app.myproject.test
127.0.0.1 api.myproject.test
# IPv6
::1 app.myproject.testSave and exit
- Ctrl+O β Write Out (save)
- Enter β Confirm filename
- Ctrl+X β Exit nano
Method 2: Edit with vim
vim is available on virtually every Linux system and preferred by developers who live in the terminal.
sudo vim /etc/hostsBasic workflow:
1. Press G then o β jump to end and open new line below 2. Press i β enter insert mode 3. Type your entries 4. Press Esc β exit insert mode 5. Type :wq and press Enter β write and quit
To exit without saving: Esc, then :q!
Useful vim commands for hosts file management
/searchterm " search for a domain
dd " delete current line
u " undo
:%s/old/new/g " replace all occurrencesHosts file syntax on Linux
IP_address hostname [aliases...]Rules:
- At least one space or tab between IP and hostname
- Lines starting with
#are comments - No wildcards
- First match wins on duplicate entries
- IPv4 and IPv6 on separate lines
Full reference: Hosts file syntax guide.
Example well-organized file:
127.0.0.1 localhost
::1 localhost
# === Dev environment ===
127.0.0.1 app.test
127.0.0.1 api.test
::1 app.test
# === Temporary staging (remove 2026-07-01) ===
203.0.113.50 client.comFlush DNS cache on Linux
After editing /etc/hosts, flush the resolver cache so changes take effect immediately.
systemd-resolved (Ubuntu, Fedora, Debian, Arch with systemd)
sudo systemd-resolve --flush-caches
# Or on newer systemd versions:
sudo resolvectl flush-cachesCheck status:
resolvectl statusnscd (Name Service Cache Daemon)
sudo systemctl restart nscd
# Or legacy:
sudo /etc/init.d/nscd restartdnsmasq
sudo systemctl restart dnsmasqNo cache daemon running
On minimal systems without a cache daemon, changes to /etc/hosts take effect immediately β no flush needed. Verify with getent hosts.
For Ubuntu-specific systemd-resolved details see Edit hosts file on Ubuntu.
Verify your entries
ping
ping -c 3 app.testExpected:
PING app.test (127.0.0.1) 56(84) bytes of data.getent (more reliable than ping)
getent hosts app.testExpected:
127.0.0.1 app.testdig (shows full resolution path)
dig app.testLook for the ANSWER section showing your configured IP.
Common problems on Linux
Permission denied
[ Error writing /etc/hosts: Permission denied ]Fix: Use sudo. See Edit as administrator.
Changes not taking effect
1. Flush DNS cache (see above) 2. Check /etc/nsswitch.conf β hosts: files dns means hosts file is checked first 3. If using systemd-resolved, verify with resolvectl query domain.test 4. Clear browser DNS cache separately
File is immutable (cannot save even with sudo)
Some hardened systems set the immutable flag:
lsattr /etc/hosts
# If you see ----i--------:
sudo chattr -i /etc/hosts
# Edit, then optionally restore:
sudo chattr +i /etc/hostsDocker containers ignore host /etc/hosts
Docker containers have their own /etc/hosts. Use --add-host when running containers:
docker run --add-host app.test:127.0.0.1 myimageSELinux blocking edits (RHEL, Fedora)
sudo ausearch -m avc -ts recent
# If SELinux denied the write, check audit logs and adjust policy if neededDistribution notes
| Distro | Default hostname line | DNS cache tool |
|---|---|---|
| Ubuntu | 127.0.1.1 hostname | systemd-resolved |
| Debian | 127.0.1.1 hostname | systemd-resolved (if installed) |
| Fedora | 127.0.0.1 localhost | systemd-resolved |
| Arch | 127.0.0.1 localhost | varies |
| RHEL/CentOS | 127.0.0.1 localhost | nscd or none |
Automation and scripting
Append an entry from a script:
echo "127.0.0.1 newsite.test" | sudo tee -a /etc/hostsRemove an entry:
sudo sed -i '/newsite.test/d' /etc/hostsIdempotent add (only if not present):
grep -q 'newsite.test' /etc/hosts || echo "127.0.0.1 newsite.test" | sudo tee -a /etc/hostsNetworkManager and desktop Linux
On desktop distributions using NetworkManager (Ubuntu GNOME, Fedora Workstation), DNS settings in the GUI do not replace /etc/hosts β they configure upstream resolvers for domains not listed locally. Your hosts entries remain authoritative for matching hostnames.
If you use nmcli to manage connections, hosts file behavior is unchanged:
nmcli connection show
# hosts file entries still take priority for listed domainsContainer and Kubernetes development
Linux developers running minikube, kind, or k3s often combine hosts file entries with cluster ingress:
127.0.0.1 app.local
127.0.0.1 api.localPoint these to localhost where your ingress controller listens (often port 80/443 on the host). The hosts file bridges friendly domain names to the local cluster entry point without requiring a real DNS server in your homelab.
Flatpak and Snap considerations
Some sandboxed applications on Linux use confined network namespaces. Most respect the system /etc/hosts, but if a Snap app ignores your entry, check whether it uses network: host mode or an internal DNS configuration. Testing with getent hosts from the same user session as the app helps isolate sandbox vs resolver issues.
Headless servers and SSH-only access
On Linux servers without a desktop environment, editing hosts is the same sudo nano /etc/hosts workflow over SSH. There is no DNS cache daemon on many minimal server installs β changes often take effect immediately. Verify with getent hosts rather than ping if ICMP is blocked by firewall rules (common on cloud VPS instances).
Best practices for Linux developers
- Use
.testTLD for local domains (RFC 2606 reserved) - Comment and date temporary entries
- Keep
/etc/hosts.backupin version control for team projects (not the live file) - Prefer
getent hostsoverpingfor verification (ping may be blocked by firewall) - Flush cache after every edit β add to your dev startup scripts
Related guides
- Main guide β all platforms
- Ubuntu-specific guide
- Administrator rights
- Block a website
- Complete reference
- Safety guide
---
*Last tested: Ubuntu 24.04 LTS, Fedora 40 β 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
"Clear nano and vim instructions. The systemd-resolved flush command was exactly what I needed on Fedora."
June 2, 2026
"Best Linux hosts guide I found. Covers both beginner nano workflow and vim for power users."
June 5, 2026
"Solid reference. Would love a section on Docker networking interaction but otherwise excellent."
June 9, 2026
Frequently Asked Questions
Where is the hosts file on Linux?
/etc/hosts β a system-wide plain text file readable by all users but writable only by root.
Why do I need sudo to edit /etc/hosts?
The file is owned by root. Without sudo, you get "Permission denied". See [[edit-hosts-file-as-administrator|administrator guide]].
How do I flush DNS cache on Linux?
On systemd systems: sudo systemd-resolve --flush-caches. On older systems: sudo /etc/init.d/nscd restart or sudo service nscd restart.
Can I use vim instead of nano?
Yes. Run sudo vim /etc/hosts, press i to insert, add entries, press Esc, type :wq and Enter to save and quit.
Does the hosts file work the same on all Linux distros?
Yes β location (/etc/hosts) and syntax are standardized. DNS cache flush commands vary by init system and resolver.
How do I test a hosts file entry on Linux?
Run ping domain.test or getent hosts domain.test β both should return the IP you configured.
Related Articles
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 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
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