10

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?

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Any chance there is an error in hosts file on Windows 7 so that the entry is just ignored (like some control char that displays like regular space)? – UserControl Jun 05 '15 at 07:12
  • Well, if I switch the user to an actual user instead of LocalSystem, the same hosts file and same service binary work fine, so I guess it must be correct. – nvoigt Jun 05 '15 at 07:39
  • Have you checked the permissions on the hosts file to ensure they are correct? – Xefan Jun 05 '15 at 08:28
  • Also, does the machine have any proxy configurations setup such that one user is being forced through a proxy, and another not? Could this be set by group policy? I believe hosts files are ignored when proxy settings are setup in internet configuration. – Xefan Jun 05 '15 at 08:32
  • 2
    This question doesn't belong here. The canonical Q+A [is here](http://serverfault.com/questions/452268/hosts-file-ignored-how-to-troubleshoot). – Hans Passant Jun 05 '15 at 10:45

2 Answers2

9

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

Community
  • 1
  • 1
Codeguard
  • 7,787
  • 2
  • 38
  • 41
  • This was our issue when we first started working with Windows 7; our HOSTS entries were being redirected and weren't taking effect. Use Run As administrator with Notepad and open the file directly, or look in the VirtualStore to see if you have a copy of the file stored there. – Skerkles Jun 11 '15 at 16:49
3
  • First close other runnings applications and have a look at your system logs
  • 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

intika
  • 8,448
  • 5
  • 36
  • 55