Wednesday 31 March 2010

samba jcifs single server/host multiple computer or host names

I was using jcifs in a java application to access files on a remote file server.

Samba and hence jcifs do not like it when you use a path which contains an alias rather than the actual computer or host name.

So for example if the hostname of your remote file server is fileserver1, but for some reason you want to access some files on this server using JCIFS or samba but want to refer to it as fileserver2 and in your local hosts file you have fileserver2 pointing to the same IP address as fileserver1, then when you try to access \\fileserver2\.... you get the following error:

jcifs.smb.SmbException: Failed to connect to server
java.net.UnknownHostException: fileserver2
at jcifs.UniAddress.getAllByName(UniAddress.java:315)
at jcifs.smb.SmbFile.getFirstAddress(SmbFile.java:852)
at jcifs.smb.SmbFile.connect(SmbFile.java:939)
at jcifs.smb.SmbFile.connect0(SmbFile.java:868)
at jcifs.smb.SmbFile.queryPath(SmbFile.java:1321)

The solution is the following:
  1. open registry editor on the fileserver. 
  2. go to HKEY_Local_Machine \ System \ CurrentControlSet \ Services \ LanmanServer \ Parameters 
  3. create a new multi-string value parameter with the name of "OptionalNames". 
  4. edit the data of this newly created field and add as many aliases as you want. One alias on a new line. So in the above case, you would just add fileserver2. 
  5. Now on both the fileserver and your local client add fileserver2 to your hosts file (C:\WINDOWS\system32\drivers\etc\hosts) with the same IP address of fileserver1. 
  6. reboot the fileserver. 

I recently found that an easier way is just to create a new value under HKEY_Local_Machine \ System \ CurrentControlSet \ Services \ LanmanServer \ Parameters on the fileserver and then reboot the fileserver:

Value name: DisableStrictNameChecking
Data type: REG_DWORD
Radix: Decimal
Value: 1

No comments:

Post a Comment