1. Current Location: Home >  Router Encyclopedia >  Changing a line in the hosts file can block a website; trojans love to secretly modify it

Changing a line in the hosts file can block a website; trojans love to secretly modify it

What is the hosts file: parsing sequence flowchart, first query hosts, then DNS query

My

former colleague's office computer had a strange incident: the Taobao homepage could open, but as soon as I logged in, it spun in circles, and the browser even popped up a big red cross saying there was a problem with the certificate. The antivirus software was thoroughly searched, and it was glowing green. I went over remotely to check for a long time. The router's DNS was normal, and other computers were fine, but it lagged for over an hour. Strangely, I remembered there was still one place I hadn't logged — the hosts file. When I opened it, I saw three lines below: a certain shopping domain name was all pointed to an unknown IP. Delete those three lines, and it'll be ready immediately.

Few people know about this file, but it's the first site for domain name resolution, even earlier than DNS. Today, we'll explain it thoroughly: what it is, where it is, how to change it, what it can do, and who cares about it.

hosts is the note in your computer pocket

Usually, when typing a domain name, everyone defaults to asking the DNS server for an IP, < a href="https://www.shunot.com/en/lybk/1069.html"> I have broken down that route process. But there's a prerequisite action in the complete sequence: the system first reaches into its own pocket and flips through a small notebook called hosts. If you have this domain on your laptop, just use the IP address directly; the DNS server won't be asked about it at all; No, I went out to ask.

This address book is not a new invention; on the contrary, it is the ancestor of DNS. In the earliest days, the entire internet relied on a hosts file to synchronize domain names manually, but later DNS was invented when there were so many websites that they could barely remember them. Old, the rules haven't changed to this day: if you have something in hosts, use it directly from the system.

I did a thorough verification on my own computer. I put a line of 127.0.0.1 www.shunot.com into the hosts—pointing my own blog to 127.0.0.1, the 'home' address. Then I asked for a parsing in Python:

www.shunot.com -> 127.0.0.1

The real IP is 49.51.202.150, and that's what the DNS records. But the system didn't even ask about DNS, just replied 127.0.0.1 because the note had written it. Delete this line and ask again, immediately revert to 49.51.202.150. No need to restart the browser or computer throughout the process—just add and delete line by line, and the effect is immediate.

This "priority" is the source of hosts' full power: changing a line is like changing the address of a website on this computer.

Where it is, what it looks like, why you can't change

it

In Windows, its location is C:\Windows\System32\drivers\etc\hosts, and it does not have an extension; Mac and Linux are in /etc/hosts. You can open it with a notebook, and it looks especially plain

127.0.0.1 localhost
::1 localhost
172.18.0.2 DESKTOP-XX

One line per line, with the IP in front and the domain name after, spaces or tabs in the middle, and hash numbers starting with comments. By default, these are the few lines. localhost refers to itself, and the IPv6 lines are reserved by the system.

Ninety percent of the first-time users will get stuck in the same place: the Notepad opens fine, but the save bomb says "Access Denial." Because the etc directory is managed by the system, Notepad with normal permissions can only be read but not written. The main way is to search for Notepad in the Start menu, right-click 'Run as administrator', then open the file → from Notepad and manually paste the path into it. Only then will the save be saved.

There's an even darker trap: File Explorer hides the extension by default. You "save" as a hosts, but the actual disk is hosts.txt, and the system doesn't recognize it. After all this change, it's basically not changed. After saving after making changes, be sure to check if there are any hidden .txt after the file name.

I'll add a line and show it: blocking takes effect immediately

Once you know the priority, the most practical trick works: point the domain you don't want the computer to access to 127.0.0.1.

127.0.0.1 is the "own" house number. If a website is directed to your home, it's like the computer is knocking on your door—no one answers, and it fails instantly. I tried using a test domain, added a line of 127.0.0.1 test.example.com, but the browser couldn't open the domain immediately, as fast as the website disappeared. If someone is willing to nitpick details, writing 0.0.0.0 fails a bit faster; 127.0.0.1 still has to wait for their home port to reply with "No one," then 0.0.0.0 is immediately declared invalid. In home scenarios, you can't really tell the difference between the two.

The key is that it only manages this one computer, without touching the router or affecting the phone's tablet at home. The advantage is precision, but the downside is that—only one device is guaranteed, and the other equipment is useless. So it's the 'fence of the computer layer,' which doesn't conflict with the router layer

the firewall? Adjust who you want to control. . Handle in
Where do you want to block
Only manage this computer's hosts file, don't affect other devices
all devicesthe household, router blocking/DNS layerPhones and flat-screen TVs are all included

If you really want to manage FamilyMart, I've written an article about the router layer five roadblocks to sort out different layers. NAS users can also set up their own AdGuard Home to have their family's DNS run on it. The time my family handled 60,000 cases in a month is recorded here. hosts has the lowest threshold among them: no need to log into any backend, just know how to open Notepad.

Of the three legitimate uses, the second one is least known

First, add a handmade fence for your child's computer. My child started searching on the old computer in the study room in first grade. For several game websites, I directly entered the hosts and set it to 127.0.0.1. Compared to router parental control, it was a bit rough, but it worked on this computer forever. You can't bypass browser changes or cache clearing—parsing is a dead end.

Second, record the address numbers for the intranet devices. The static IP I have on my NAS at home is 192.168.31.10. When installing apps on my phone, you need to remember this string of numbers ( I wrote about the three routes for my mom to access the NAS via phone). A more convenient way on your computer is to add a line of 192.168.31.10 nas.home to hosts. From now on, you can just type nas.home directly in your browser or resource manager, without needing to remember the IP. Some people say NAS supports direct name connection, and that automatic discovery mechanism is called mDNS. casting and searching rely entirely on it, but it has a flaw: it works intermittently and doesn't even recognize old Windows versions. hosts is the manual version of name resolution, written in a dead end and never fails.

Third, development testing. A colleague who works on a website temporarily points a domain to run the local version on their own computer, then changes hosts and refreshes to test it as if visiting a real site, then deletes it after testing. You won't encounter it in home scenarios, just know that it's important.

Trojans love it the most: that's how those three lines on my colleague's computer came from

Back to the computer at the beginning. Why do trojans prefer to change hosts? If you think from the attacker's perspective, you'll understand: changing the router's DNS causes a lot of noise and harms the whole family, making it easy to be discovered; Stuffing three lines into a computer's hosts without anyone noticing, only one person is tricked; after modifying files, the trojan deletes itself, and the antivirus software is left searching for nothing.

Those three lines point the login page of the shopping website to a fake IP, and the fake site makes the page look exactly the same, waiting to collect account passwords. But one detail saved my colleague: the login page starts with https, the real website comes with a real certificate, but the server on the fake IP couldn't show the corresponding certificate, so the browser only showed a big red X. I dismantled the HTTPS certificate mechanism once, and it played a big role here—if the certificate doesn't match the number, it goes on strike. This barrier is the barrier that keeps the fishing station out. On the other hand, if one day you encounter "website won't open but still pops up certificate warnings," don't just try again—the hosts are worth a quick look.

Self-check is very simple, just one red line: in hosts, besides localhost and machine name, every extra line must be able to provide history. Some cracking software and green versions also like to write a blocking line to activate the domain name. It's not a trojan, but you still need to know what it says. For self-checking on router-level hacking, I have a more comprehensive list: five locations in DNS settings to port mapping. The hosts section is on the computer side, and the two layers complement each other.

Changes won't work? Let's count the three situations one by one

First, the cache is holding up. The browser caches DNS parse results, and Chrome enters the address bar chrome://net-internals/#dns and clicks Clear host cache; At the system layer, there's another step: clear ipconfig /flushdns from the administrator command line. I cleared the four layers of web caching. The DNS two layers are neighbors, and the troubleshooting approach is the same: if you change it and it doesn't work, first suspect the cache is blocking the old answer.

The second is storing a fake document. This is the hosts.txt mentioned earlier, or simply not saved successfully (not enough permissions, Notepad silent failure). Right-click the file to check the modification time; if the time hasn't changed, it means it hasn't been saved at all.

The third type is that the enterprise computer is controlled. Some companies lock hosts directly in their computer group policies, and personal antivirus software will block hosts changes as suspicious behavior and pop-up ads. Don't force your business environment—contact IT.

And to clarify the boundary: there's no such thing on your phone. On Android, you need to root to access hosts, while iOS doesn't even provide an entry point. So if you want to block domain names on your phone, you can only go to the router layer—choose one of the router blockers, DNS filtering, or AdGuard Home.

To wrap up, just three sentences about hosts are enough: the first stop of the analysis, if it's on the note, don't ask for directions; Changing it requires administrator privileges, then after saving it you can check the extension; If the website won't open on your computer and still shows a certificate warning, try it first. A minute's worth of things saves me from being stuck for over an hour like I did last time.

What if you want to do can be done? domain . : . If block , you
something with hosts
Blocking a certain website (only this computer) can, but the easiestis 127.0.0.1
Blocking the whole family's devices won't workUse Router Blocking or AdGuard Home
Give the NAS a memorable nameIP and a custom name
you can'ton your phonewon't get hosts, so go to the router layer and figure out a solution
Read More


Copyright Notice Scan to read on mobile
All Rights Reserved: 《SHUNOT》 => 《Changing a line in the hosts file can block a website; trojans love to secretly modify it》
Article URL: https://www.shunot.com/en/lybk/1260.html
Unless otherwise stated, all articles are original by 《SHUNOT》. Reposting is welcome! Please indicate the original URL when reposting, thank you.

Contact Us

Online Consultation: Click here to send me a message

WeChat ID: master_135

Scan to follow