What is Lmhosts? What is it for?
LMHOSTS is an ASCII file used to enable Domain Name Resolution under Windows Operating Systems when other methods, such as WINS, fail.
Lmhosts stands for LAN (Local Area Network) Manager Hosts.
After installing Windows, a file called Lmhosts.sam is created in C:\Windows\system32\drivers\etc. This file is just a sample which also contains a set of instruction on how to use this resolution method. The extension .sam means sample.
When using this file for name resolution, you should edit open the Lmhosts.sam with Notepad (or another text editor) and save it without the .sam extension (keep the same name and the exact same location).
In this article
- Lmhosts file location
- Creating and Editing an Lmhosts file
- Lmhosts sample file transcription
- Difference between Lmhosts and host files
- Troubleshooting: Can’t save the Lmhosts file
Note that Windows Server automatically resolves NetBIOS names for computers in a TCP/IP local network, so you don’t really need LMHOSTS. However, you can use an LMHOSTS file to resolve IP addresses of computers on other networks to which your network is connected by a gateway when a WINS server isn’t available.
Lmhosts file location
The file is located in %windir%\system32\drivers\etc\, and the sample file (lmhosts.sam) is located here. The %windir% is an environment variable pointing to the Windows installation directory, typically C:\Windows.
Using Lmhosts for name resolution
Remember that Lmhosts function is to resolve manually NetBIOS names, not Host names. For resolving manually Host Names you should use the Hosts file. Read the section: Difference between Lmhosts and host files in this article.
Be sure that the “EnableLMHOSTS” registry key is set to “1”. It normally is. You can find it under:
Creating and Editing an Lmhosts file
How to edit the Lmhosts file?
Any Windows operating system, by default, makes the LMHOSTS.SAM file available, usually in the C:\Windows\System32\drivers\etc folder. The first thing to do is to copy this file keeping the same name and removing the .SAM extension. Don’t delete the LMHOSTS.SAM. It may be useful in the future.
You should use a simple text editor, like Notepad, to edit the file you created and start making the changes you need.
For example, you need your computer to be aware that the server SERVER_NY has the IP address 126.96.36.199. Just add this line:
# Only a comment. Nothing to compute
188.8.131.52 SERVER_NY #This is the New York web server
Save the file (LMHOSTS without the .SAM extension, but in the same folder)
At the command prompt, you can use NET VIEW to check your recent entry.
C:\net view \\SERVER_NY
You can also use the PING command.
Some important rules:
- Never add a space at the beginning of the line.
- Use # to comment or to temporarily exclude a line.
- NetBIOS names can have a maximum of 15 characters defined by the Administrator, the last character is set by the system (total max of 16 characters). Names larger than that will simply not be read.
- Read the LMHOSTS.SAM file for more instructions. We have the full transcription in the next section of the article.
Lmhosts sample file transcription
Transcription of the instruction included in Lmhosts.sam file:
Copyright (c) 1993-1999 Microsoft Corp.
This is a sample LMHOSTS file used by the Microsoft TCP/IP for Windows.
This file contains the mappings of IP addresses to computernames (NetBIOS) names. Each entry should be kept on an individual line.
The IP address should be placed in the first column followed by the corresponding computername. The address and the computername should be separated by at least one space or tab. The “#” character is generally used to denote the start of a comment (see the exceptions below).
This file is compatible with Microsoft LAN Manager 2.x TCP/IP lmhosts files and offers the following extensions:
\0xnn (non-printing character support)
Following any entry in the file with the characters “#PRE” will cause the entry to be preloaded into the name cache. By default, entries are not preloaded, but are parsed only after dynamic name resolution fails.
Following an entry with the “#DOM:<domain>” tag will associate the entry with the domain specified by <domain>. This affects how the browser and logon services behave in TCP/IP environments. To preload the host name associated with #DOM entry, it is necessary to also add a #PRE to the line. The <domain> is always preloaded although it will not be shown when the name cache is viewed.
Specifying “#INCLUDE <filename>” will force the RFC NetBIOS (NBT) software to seek the specified <filename> and parse it as if it were local. <filename> is generally a UNC-based name, allowing a centralized lmhosts file to be maintained on a server.
It is ALWAYS necessary to provide a mapping for the IP address of the server prior to the #INCLUDE. This mapping must use the #PRE directive.
In addtion the share “public” in the example below must be in the LanManServer list of “NullSessionShares” in order for client machines to be able to read the lmhosts file successfully. This key is under \machine\system\currentcontrolset\services\lanmanserver\parameters\nullsessionshares
in the registry. Simply add “public” to the list found there.
The #BEGIN_ and #END_ALTERNATE keywords allow multiple #INCLUDE statements to be grouped together. Any single successful include will cause the group to succeed.
Finally, non-printing characters can be embedded in mappings by first surrounding the NetBIOS name in quotations, then using the \0xnn notation to specify a hex value for a non-printing character.
The following example illustrates all of these extensions:
184.108.40.206 rhino #PRE #DOM:networking #net group’s DC
220.127.116.11 “appname \0x14” #special app server
18.104.22.168 popular #PRE #source server
22.214.171.124 localsrv #PRE #needed for the include
In the above example, the “appname” server contains a special character in its name, the “popular” and “localsrv” server names are preloaded, and the “rhino” server name is specified so it can be used to later #INCLUDE a centrally maintained lmhosts file if the “localsrv” system is unavailable.
Note that the whole file is parsed including comments on each lookup, so keeping the number of comments to a minimum will improve performance.
Therefore it is not advisable to simply add lmhosts file entries onto the end of this file.
Difference between Lmhosts and host files
An LMHOSTS file specifies the NetBIOS computer name and IP address mappings. A HOSTS file specifies the DNS name and IP address.
On a local computer, the HOSTS file (used by Windows Sockets applications to find TCP/IP host names) and LMHOSTS file (used by NetBIOS over TCP/IP to find NetBIOS computer names) can both be used to list known IP addresses, mapped with corresponding computer names.
LMHOSTS is used for name resolution in Windows for internetworks where WINS is not available.
The HOSTS file is used as a local DNS equivalent to resolve host names to IP addresses.
The LMHOSTS file is used as a local WINS equivalent to resolve NetBIOS computer names to IP addresses.
Each of these files is also known as a host table. Sample versions of LMHOSTS (called LMHOSTS.SAM) and HOSTS files are added to the Windows directory when you install Windows 95 with TCP/IP support. These files can be edited using any ASCII editor, such as WordPad or Edit. To take advantage of HOSTS or LMHOSTS, DNS must be enabled on the computer. For information about setting up and using HOSTS and LMHOSTS files, see Appendix G, “HOSTS and LMHOSTS Files for Windows 95.”
Troubleshooting: Can’t save the Lmhosts file
If you’re trying to save changes made to a Lmhosts file you may receive an error message like this:
Access to C:\Windows\System32\drivers\etc\ hosts was denied
Cannot create the C:\Windows\System32\drivers\etc\hosts file.
Make sure that the path and file name are correct.
This issue can occur even if you log on by using an account that has administrative privileges.
To bypass this issue, go to Notepad (Start->All Programs->Accessories), right-click Notepad and click Run as administrator.
From the Notepad File menu, open the Lmhosts file (usually in C:\Windows\System32\drivers\etc\ folder) and make the changes you need to do. Click Save from the File menu and that’s it.