fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← IT & networking

IT & networking / 5 MIN READ

DNS Hosts File

Changing the DNS Hosts File to prepare for a new domain

From the original Fervor library. Examples may use older package versions.

Editing your DNS hosts file is like setting up a personalized phonebook for your computer. When you type in a website like “www.example.com,” your system checks the hosts file to see if you have a specific phone number (or IP address) listed for that name before it goes asking around the internet. This can be super useful for testing websites locally before they go live or blocking access to certain sites. Let’s dive into how you can edit your DNS hosts file on different operating systems!

What You’ll Need:

  • Administrator Access: You’ll need admin privileges on your computer to make these changes.
  • A Text Editor: Windows users might use Notepad, while macOS and Linux folks might opt for nano or vim in the terminal.

Editing the Hosts File on Different Operating Systems:

Windows:

  1. Open Start: Click the Windows logo in the bottom-left corner of the screen.
  2. Search for Notepad: Type in “notepad”.
  3. Run as Administrator: Right-click on Notepad and choose “Run as administrator”. This step is crucial as you’ll need admin rights to edit the hosts file.
  4. Open the Hosts File: In Notepad, go to File > Open, navigate to C:\Windows\System32\drivers\etc, and select “All Files” or “*.txt” from the dropdown menu to see the hosts file. Open the hosts file.
  5. Make Your Changes: Add entries by typing the IP address, followed by at least one space (or a tab), then the domain name. For example:
    102.54.94.97     www.example.com
    
  6. Save Your Changes: Go to File > Save to apply the changes.
  7. Flush the DNS Cache: Open Command Prompt as an administrator and type ipconfig /flushdns to ensure your system starts using the updated hosts file right away.

macOS:

  1. Open Terminal: You can find this in your Applications folder under Utilities, or search for it using Spotlight.
  2. Open the Hosts File in a Text Editor: Type sudo nano /etc/hosts and press Enter. You’ll be prompted to enter your password.
  3. Make Your Changes: Just like in Windows, add entries in the form of IP address, space/tab, domain name.
  4. Save and Exit: Press Ctrl-O to save the file, then Ctrl-X to exit nano.
  5. Flush the DNS Cache: Type sudo killall -HUP mDNSResponder and press Enter to make sure your system recognizes the changes.

Linux:

  1. Open Terminal: This can typically be found in your system’s application launcher.
  2. Edit the Hosts File: Use a text editor with root privileges by typing sudo nano /etc/hosts.
  3. Make Your Changes: Add lines for each IP address and domain you want to reroute, similar to the other operating systems.
  4. Save and Exit: Use Ctrl-O and Ctrl-X in nano to save changes and exit.
  5. Flush the DNS Cache: Depending on your distribution, this might vary. A common method is sudo systemd-resolve --flush-caches.

Tips for Success:

  • Backup First: Before you make changes, it’s a good idea to make a backup of your hosts file. Just copy it to another location or rename it in the same directory.
  • Check for Typos: A single typo can prevent your changes from working as expected.
  • Test Immediately: After editing and flushing your system’s DNS cache, try accessing the domains you’ve added to see if they point to the new IP addresses correctly.

Editing your DNS hosts file is a powerful tool when you need to override the DNS for a domain on a local machine. Whether it’s for testing or blocking purposes, it puts you in control of domain resolving. Happy editing!

BONUS cool thing and Things to Avoid!!

Sure! Tweaking your DNS hosts file can feel like a hidden superpower, but with great power comes great responsibility. Here are some cool things to know and a few important “what not to do” tips when it comes to editing your hosts file:

Cool Things to Know:

  1. Local Testing: Developers often use the hosts file to test websites locally before making them live. This is incredibly useful for making sure a site functions correctly on the actual domain before the rest of the world sees it.
  2. Blocking Unwanted Sites: You can block websites on your computer by redirecting their domain names to a non-routable IP or your local machine. For instance, adding 127.0.0.1 www.example.com will block www.example.com by directing it to your local host.
  3. Speed Up Browsing: Bypassing DNS lookups by using a hosts file can speed up your access to frequently visited sites. If the IP addresses are static and you trust they won’t change, this can shave off milliseconds of lookup time, making browsing feel snappier.
  4. Educational Tool: It’s a fantastic way to learn more about networking basics, DNS, and how the internet works under the hood. Experimenting with your hosts file can provide hands-on understanding of these concepts.

What Not to Do:

  1. Don’t Use It for Large Scale DNS Management: While it’s tempting to start using the hosts file for extensive DNS management, this is not scalable or manageable, especially as the number of entries grows. It can become a nightmare to maintain.
  2. Avoid Outdated Entries: Always make sure that the entries in your hosts file are up-to-date. Pointing to an old or incorrect IP can lead to confusing debugging sessions or security issues.
  3. Don’t Ignore Security Risks: Since entries in the hosts file override DNS lookups, they can be a security risk if misused by malware. Malicious software can modify the hosts file to redirect you to phishing or malware-laden websites without your knowledge.
  4. Avoid Skipping Backups: Always back up your original hosts file before making any changes. This provides a quick rollback option if something goes wrong.
  5. Don’t Forget to Flush the DNS Cache: After editing, always flush your system’s DNS cache to make sure the changes are applied immediately. Failing to do this might make troubleshooting difficult because your system could still be using old DNS information.

Using the hosts file wisely allows you to harness a potent tool that affects how your computer navigates the internet. Whether you’re testing, learning, or customizing your browsing experience, these insights and precautions can help you do so effectively and safely. Happy editing!

Keep your curiosity going.Explore more IT & networking →
287 TUTORIALS · 22 TOPICSREADY