I have programmed a Windows Service that is calling another service over the network.
The other service's IP is defined in the hosts file.
The Windows Service is running as local system.
On Windows Server 2008 this works fine. The IP from the hosts file is used.
On Windows 7, the IP from the hosts file is not used. Instead it uses normal DNS.
If I use a normal user instead of local system, the behaviour is correct. The same as on Windows Server 2008. The host file is used.
I can reliably switch between local system and a normal user for the same service binary without touching the hosts file, the error is reproducable. So it's not about caching anything anywhere or having a wrong hosts file.
Is there anything in Windows 7 I missed? Why would a service running as local system not use the hosts file?
Windows 7 and earlier versions of Windows Select Start > Run, type %WinDir%\System32\Drivers\Etc, and then select OK. Select the Hosts file, and rename it "Hosts. old." Copy or move the Hosts file that you created in step 3 to the %WinDir%\System32\Drivers\Etc folder.
If the ping does not work, then it is likely that the permissions on the hostfile are incorrect. Incorrect permissions on the hostfile may cause an arbitrary lock on the hostsfile which prevents windows from properly read it and populating the dns cache.
Testing the hosts file Pinging the domain name is the easiest way to test if the newly configured IP is working. Tools like dig, nslookup, and host use the DNS data to retrieve information - because of this you don't see the changes in effect.
The reason why it works as normal user could be this: you could have a duplicate hosts
file in %LOCALAPPDATA%\VirtualStore
, which is a redirection folder for programs running as non-admin and trying to write to system locations. If this is the case, then you actually see wrong hosts
file when you think you edit the system's one!
The other reason could be wrong permissions set to file. Please try point 5 from https://serverfault.com/a/452269
You can run anything as system to debug like explorer.exe or cmd.exe with the folowing command using PSTools you could then browser your hosts and check it
psexec -i -s cmd.exe
psexec -i -s ping your.host
Avoid multiple entry for the same host on hosts file
Even if you talked about it i post some debug commands
ipconfig /flushdns
nbtstat -R
(reset netbios)
You can check DNS Cache
ipconfig /displaydns | more
Windows set the hosts file location with this registry key
\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DataBasePath
Check proxy config it could bypass hosts
Check "etc" directory permission and "hosts" file permission
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With