How to Edit Ubuntu's etc Hosts File by Example |Video upload date:  · Duration: PT1M13S  · Language: EN

Step by step guide to safely edit Ubuntu hosts file for local DNS mapping using terminal editors sudo and simple testing commands

Why edit the hosts file on Ubuntu

Want to force a domain to point to your machine for debugging, block a site without spiritual counseling, or override DNS for a staging server That is what the hosts file does and it is stubbornly simple and brutally effective

Back up the hosts file before you adventure

Mistakes here lead to sad networking and mild regret. Make a safe backup so you can undo your ambitions in one command

sudo cp /etc/hosts /etc/hosts.bak

Edit the file with elevated privileges

Pick your weapon nano or vim. The file lives in a protected path so use sudo

sudo nano /etc/hosts

In nano press Control O to write and Control X to exit. If you prefer vim then use standard vim save and quit commands

How lines should look

  • One mapping per line
  • Format example 127.0.0.1 example.local
  • Use the loopback address for localhost testing and avoid duplicate hostnames on different lines

Make changes that do not hurt the rest of the system

Do not overwrite the real localhost entry unless you enjoy subtle breakage. Add your custom names on new lines and use clearly unique domains for testing like app.localtest or mysite.devtest

Reload name resolution if needed

Some Ubuntu setups cache DNS or use local resolvers. If your change does not appear try flushing caches or restarting network services

sudo systemd-resolve --flush-caches
sudo systemctl restart NetworkManager

Verify the mapping actually works

Ping is fast and truthful when it comes to name to IP checks

ping -c 1 example.local

You can also test with a browser or curl for HTTP checks

Troubleshooting when the hosts file seems shy

  • Check for typos and duplicate entries in /etc/hosts
  • Confirm the hosts lookup order in /etc/nsswitch.conf so hosts is consulted before DNS
  • Look for local DNS services like systemd resolved dnsmasq or a proxy that might override the file
  • Browsers and proxies cache aggressively. Try a hard refresh or a private window

Quick summary and a pro tip

Backing up the file editing with sudo adding your hostname to IP mappings saving the file and flushing caches will get you where you want to be most of the time. The hosts file is small but mighty for local development and lightweight domain rerouting

Pro tip use test only domains that cannot collide with real public names to avoid accidental chaos

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.