How to Configure Multiple IP Addresses for a Single Hostname in Windows Using Hosts File


2 views

While the Windows hosts file (C:\WINDOWS\system32\drivers\etc\hosts) is great for simple hostname-to-IP mappings, it has a critical limitation when you need to point multiple IP addresses to a single hostname. If you try adding multiple entries like this:


192.168.244.128   gateway.net
192.168.226.129   gateway.net

The system will only use the last entry, ignoring all previous ones. Windows doesn't support round-robin DNS or failover functionality in the hosts file.

Since you mentioned this is for a local network deployment, here are some better approaches:

1. Set Up a Local DNS Server

For true multi-IP resolution, consider running a local DNS server like BIND or dnsmasq:


# Example dnsmasq configuration
address=/gateway.net/192.168.244.128
address=/gateway.net/192.168.226.129

2. Use Windows DNS Round Robin

If you have a Windows Server available, configure DNS round robin:


# PowerShell commands to create multiple A records
Add-DnsServerResourceRecordA -Name "gateway" -ZoneName "net" -IPv4Address "192.168.244.128"
Add-DnsServerResourceRecordA -Name "gateway" -ZoneName "net" -IPv4Address "192.168.226.129"

3. Client-Side Load Balancing Script

For development environments, you could create a simple script that modifies the hosts file dynamically:


@echo off
set ips=192.168.244.128,192.168.226.129
for /f "tokens=1,2 delims=," %%a in ("%ips%") do (
    echo %%a gateway.net > %SystemRoot%\System32\drivers\etc\hosts
    ping -n 1 gateway.net
    if not errorlevel 1 (
        echo Successfully connected to %%a
        exit /b
    )
    echo %%b gateway.net > %SystemRoot%\System32\drivers\etc\hosts
    ping -n 1 gateway.net
)

If you're developing an application that needs to handle multiple backend IPs, consider implementing these patterns directly in your code:


// C# example for IP failover
List gatewayIPs = new List {
    "192.168.244.128", 
    "192.168.226.129"
};

foreach (var ip in gatewayIPs) {
    try {
        var uri = new Uri($"http://{ip}");
        // Attempt connection
        break; // If successful
    } catch {
        continue; // Try next IP
    }
}
  • Hosts file modification: Only for temporary testing with single IP
  • Local DNS server: Best for permanent multi-machine environments
  • Application-level handling: Most flexible for distributed systems

The Windows hosts file (C:\WINDOWS\system32\drivers\etc\hosts) traditionally supports only one IP address per hostname. When you add multiple entries for the same hostname:

192.168.244.128   gateway.net
192.168.226.129   gateway.net

The system will typically use the last entry that appears in the file, ignoring previous ones. This behavior is by design in the name resolution process.

For local network applications requiring failover or load balancing between multiple IPs, consider these methods:

1. DNS Round Robin (Local DNS Server)

If you control a local DNS server, configure multiple A records:

gateway.net.    IN  A  192.168.244.128
gateway.net.    IN  A  192.168.226.129

Clients will receive both IPs in random order for each query.

2. PowerShell Script for Dynamic Resolution

Create a script that tests multiple IPs and modifies the hosts file:

$ips = @("192.168.244.128","192.168.226.129")
$target = "gateway.net"

foreach ($ip in $ips) {
    if (Test-Connection $ip -Count 1 -Quiet) {
        $hostsContent = Get-Content "$env:windir\System32\drivers\etc\hosts" -Raw
        $newEntry = "$ipt$target"
        
        # Remove existing entries
        $hostsContent = $hostsContent -replace "$ip\s+$target", ""
        $hostsContent = $hostsContent -replace "$target\s+$ip", ""
        
        # Add working entry
        $hostsContent += "n$newEntry"
        Set-Content "$env:windir\System32\drivers\etc\hosts" $hostsContent
        break
    }
}

3. Custom Name Resolution with C#

For applications you control, implement custom resolution:

using System.Net;

public class MultiIPResolver
{
    private static readonly string[] gatewayIPs = 
    {
        "192.168.244.128",
        "192.168.226.129"
    };

    public static IPAddress Resolve(string hostname)
    {
        if (hostname == "gateway.net")
        {
            foreach (var ip in gatewayIPs)
            {
                try
                {
                    var ping = new System.Net.NetworkInformation.Ping();
                    var reply = ping.Send(ip, 1000);
                    if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
                    {
                        return IPAddress.Parse(ip);
                    }
                }
                catch { }
            }
        }
        return Dns.GetHostAddresses(hostname).FirstOrDefault();
    }
}

For manual testing, you can use this batch script to check reachability:

@echo off
set hostname=gateway.net
set ip1=192.168.244.128
set ip2=192.168.226.129

ping -n 1 %ip1% >nul
if %errorlevel%==0 (
    echo %ip1% is reachable
    echo %ip1% %hostname% >> %windir%\System32\drivers\etc\hosts
) else (
    ping -n 1 %ip2% >nul
    if %errorlevel%==0 (
        echo %ip2% is reachable
        echo %ip2% %hostname% >> %windir%\System32\drivers\etc\hosts
    ) else (
        echo No endpoints reachable for %hostname%
    )
)