Block a website using the /etc/hosts file

Redirect a domain to 127.0.0.1 so no browser on this machine can open it.

LinuxnetworkprivacyhostsconfigMedium risk
Add lines like 127.0.0.1 example.com to /etc/hosts with sudo — the site stops resolving for every app on this computer.

01Command

Command
$ echo -e '127.0.0.1\texample.com\n127.0.0.1\twww.example.com' | sudo tee -a /etc/hosts

02What it does

The system checks /etc/hosts before DNS, so mapping a domain to 127.0.0.1 makes the browser fail to connect immediately — no extensions or filtering software needed. Add both the bare domain and the www. variant, since browsers request them separately. The block applies system-wide and survives reboots until you delete the line.

03Risk

RiskMedium risk
!Warning: This only blocks resolution on this machine and is easy to undo by anyone with sudo; apps with their own DNS (some VPNs, DoH browsers) can bypass it. Never delete existing lines from /etc/hosts.

04Undo

Undo
$ sudo sed -i '/example.com/d' /etc/hosts

05Sources

  • hosts(5) — Linux manual page
    man7.org · community · 2026-09-17
    Evidence: /etc/hosts maps hostnames to IP addresses and is consulted before DNS, so pointing a domain at 127.0.0.1 blocks access to it.

Last verified: 2026-09-18 · Category: Network

06Related solutions