Editing the Hosts File to Redirect a Domain to a Different IP Address Print

  • hosts, file, domain, redirect, ip address, mac, linux, windows
  • 0

The hosts file is a system file that maps domain names to IP addresses, overriding DNS resolution. Editing the hosts file allows you to redirect a domain to a specific IP address on your local machine. This is useful for testing websites on a different server (e.g., a staging server) before updating public DNS records, troubleshooting network issues, or blocking malicious domains.
Why Edit the Hosts File?
  • Website Testing: Test a website on a new server without changing public DNS, ensuring the site works correctly before going live.
  • Development: Access a development or staging environment using a production domain name.
  • Troubleshooting: Diagnose DNS-related issues by manually specifying an IP address.
  • Blocking Domains: Redirect unwanted domains (e.g., ad servers) to a non-existent or safe IP address like 127.0.0.1.
This article provides step-by-step instructions for editing the hosts file on Windows, macOS, and Linux.

Prerequisites
  • Administrator/root access to your system.
  • The IP address you want to redirect the domain to.
  • A text editor (e.g., Notepad on Windows, TextEdit on macOS, or nano/vim on Linux).
  • The domain name you want to redirect (e.g., example.com).

Editing the Hosts File
 
1. Windows
The hosts file on Windows is located at C:\Windows\System32\drivers\etc\hosts.
Steps:
  1. Open Notepad as Administrator:
    • Press Win + S, type Notepad, right-click, and select Run as administrator.
    • This ensures you have permission to save changes to the hosts file.
  2. Open the Hosts File:
    • In Notepad, click File > Open.
    • Navigate to C:\Windows\System32\drivers\etc.
    • Change the file filter to All Files (.) to see the hosts file.
    • Select hosts and click Open.
  3. Edit the Hosts File:
    • Add a new line at the end of the file in the format:
       
       
      <IP_ADDRESS> <DOMAIN_NAME>
      Example:
      192.168.1.100 example.com www.example.com
    • This redirects example.com and www.example.com to 192.168.1.100.
  4. Save the File:
    • Click File > Save.
    • If prompted, confirm you want to overwrite the existing file.
  5. Flush DNS Cache:
    • Open Command Prompt as Administrator (Win + S, type cmd, right-click, select Run as administrator).
    • Run the command:
       
       
      ipconfig /flushdns
    • This ensures the system uses the updated hosts file.
  6. Verify the Change:
    • Open a browser and navigate to the domain (e.g., example.com).
    • Alternatively, use the command ping example.com in Command Prompt to confirm it resolves to _IP_ADDRESS_.
Notes:
  • If the file is locked or you cannot save it, ensure Notepad is running as Administrator.
  • Avoid adding unnecessary spaces or tabs in the hosts file to prevent errors.

2. macOS
The hosts file on macOS is located at /etc/hosts.
Steps:
  1. Open Terminal:
    • Press Cmd + Space, type Terminal, and press Enter.
  2. Edit the Hosts File:
    • Run the following command to open the hosts file in a text editor (e.g., nano):
       
       
      sudo nano /etc/hosts
    • Enter your admin password when prompted.
  3. Add the Redirect Entry:
    • Scroll to the bottom of the file using arrow keys.
    • Add a new line in the format:
       
       
      <IP_ADDRESS> <DOMAIN_NAME>
      Example:
       
       
      192.168.1.100 example.com www.example.com
  4. Save and Exit:
    • Press Ctrl + O, then Enter to save.
    • Press Ctrl + X to exit nano.
  5. Flush DNS Cache:
    • Run the following command to clear the DNS cache:
       
       
      sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
  6. Verify the Change:
    • Open a browser and navigate to the domain.
    • Alternatively, use ping example.com in Terminal to confirm it resolves to the specified IP address.
Notes:
  • macOS requires sudo for editing system files like /etc/hosts.
  • If you prefer a graphical editor, use sudo open -e /etc/hosts to open the file in TextEdit, but ensure you save it as plain text.

3. Linux
The hosts file on Linux is located at /etc/hosts.
Steps:
  1. Open a Terminal:
    • Open your terminal application (e.g., via Ctrl + Alt + T on Ubuntu).
  2. Edit the Hosts File:
    • Use a text editor like nano or vim with sudo privileges:
       
       
      sudo nano /etc/hosts
    • Enter your password when prompted.
  3. Add the Redirect Entry:
    • Move to the end of the file.
    • Add a new line in the format:
       
       
      <IP_ADDRESS> <DOMAIN_NAME>
      Example:
       
       
      192.168.1.100 example.com www.example.com
  4. Save and Exit:
    • For nano: Press Ctrl + O, then Enter to save, and Ctrl + X to exit.
    • For vim: Press Esc, type :wq, and press Enter.
  5. Flush DNS Cache (if applicable):
    • Some Linux distributions use a local DNS cache. Clear it with:
       
      sudo systemd-resolve --flush-caches
      or, for older systems:
       
      sudo /etc/init.d/nscd restart
    • If no caching service is used, the hosts file change takes effect immediately.
  6. Verify the Change:
    • Open a browser and navigate to the domain.
    • Alternatively, use ping example.com to confirm it resolves to the specified IP address.
Notes:
  • The exact DNS cache flush command depends on your Linux distribution (e.g., Ubuntu, CentOS).
  • Ensure you have root or sudo privileges to edit /etc/hosts.

Best Practices
  • Backup the Hosts File: Before editing, copy the hosts file to a safe location to restore it if needed.
  • Use Comments: Add comments in the hosts file (lines starting with #) to describe your changes, e.g.:
     
     
    # Redirect example.com for testing
    192.168.1.100 example.com
  • Test After Changes: Always verify the redirect works by pinging the domain or accessing it in a browser.
  • Revert Changes: Remove or comment out entries when testing is complete to avoid unintended redirects.
  • Avoid Overloading: Too many entries in the hosts file can slow down DNS resolution.

 


Was this answer helpful?

« Back

Powered by WHMCompleteSolution