Locahl
Get Locahl
hosts fileLinux/etc/hostsnanovimtutorial

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

Β·Updated Β·7 min read

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:

TEXT
127.0.0.1   localhost
127.0.1.1   myhostname
::1         localhost ip6-localhost ip6-loopback

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

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

Restore if something goes wrong:

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

nano is pre-installed on most Linux distributions and shows keyboard shortcuts at the bottom of the screen.

Open the file

BASH
sudo nano /etc/hosts

Enter your password when prompted.

Add entries

Navigate to the bottom with arrow keys and add lines:

TEXT
# Local development
127.0.0.1    app.myproject.test
127.0.0.1    api.myproject.test

# IPv6
::1          app.myproject.test

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

BASH
sudo vim /etc/hosts

Basic 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

VIM
/searchterm    " search for a domain
dd             " delete current line
u              " undo
:%s/old/new/g  " replace all occurrences

Hosts file syntax on Linux

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

TEXT
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.com

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

BASH
sudo systemd-resolve --flush-caches
# Or on newer systemd versions:
sudo resolvectl flush-caches

Check status:

BASH
resolvectl status

nscd (Name Service Cache Daemon)

BASH
sudo systemctl restart nscd
# Or legacy:
sudo /etc/init.d/nscd restart

dnsmasq

BASH
sudo systemctl restart dnsmasq

No 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

BASH
ping -c 3 app.test

Expected:

TEXT
PING app.test (127.0.0.1) 56(84) bytes of data.

getent (more reliable than ping)

BASH
getent hosts app.test

Expected:

TEXT
127.0.0.1       app.test

dig (shows full resolution path)

BASH
dig app.test

Look for the ANSWER section showing your configured IP.

Common problems on Linux

Permission denied

TEXT
[ 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:

BASH
lsattr /etc/hosts
# If you see ----i--------:
sudo chattr -i /etc/hosts
# Edit, then optionally restore:
sudo chattr +i /etc/hosts

Docker containers ignore host /etc/hosts

Docker containers have their own /etc/hosts. Use --add-host when running containers:

BASH
docker run --add-host app.test:127.0.0.1 myimage

SELinux blocking edits (RHEL, Fedora)

BASH
sudo ausearch -m avc -ts recent
# If SELinux denied the write, check audit logs and adjust policy if needed

Distribution notes

DistroDefault hostname lineDNS cache tool
Ubuntu127.0.1.1 hostnamesystemd-resolved
Debian127.0.1.1 hostnamesystemd-resolved (if installed)
Fedora127.0.0.1 localhostsystemd-resolved
Arch127.0.0.1 localhostvaries
RHEL/CentOS127.0.0.1 localhostnscd or none

Automation and scripting

Append an entry from a script:

BASH
echo "127.0.0.1    newsite.test" | sudo tee -a /etc/hosts

Remove an entry:

BASH
sudo sed -i '/newsite.test/d' /etc/hosts

Idempotent add (only if not present):

BASH
grep -q 'newsite.test' /etc/hosts || echo "127.0.0.1    newsite.test" | sudo tee -a /etc/hosts

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

BASH
nmcli connection show
# hosts file entries still take priority for listed domains

Container and Kubernetes development

Linux developers running minikube, kind, or k3s often combine hosts file entries with cluster ingress:

TEXT
127.0.0.1    app.local
127.0.0.1    api.local

Point 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 .test TLD for local domains (RFC 2606 reserved)
  • Comment and date temporary entries
  • Keep /etc/hosts.backup in version control for team projects (not the live file)
  • Prefer getent hosts over ping for verification (ping may be blocked by firewall)
  • Flush cache after every edit β€” add to your dev startup scripts

---

*Last tested: Ubuntu 24.04 LTS, Fedora 40 β€” 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)
Pedro L.
β˜…β˜…β˜…β˜…β˜…

"Clear nano and vim instructions. The systemd-resolved flush command was exactly what I needed on Fedora."

June 2, 2026

Anna B.
β˜…β˜…β˜…β˜…β˜…

"Best Linux hosts guide I found. Covers both beginner nano workflow and vim for power users."

June 5, 2026

Wei C.
β˜…β˜…β˜…β˜…β˜…

"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

7 min read
hosts fileUbuntusystemd-resolved

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

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