What the Windows 'hosts' file encoding is? Is it UTF-8? Or ASCII + system codepage? How IDN (international domain names with umlauts etc.) entries should be added and can they be added at all?
-
What's provoking the question? – Bill_Stewart Aug 22 '14 at 14:39
-
I'm writing a GUI editor for this file. Of course, very special one. And now I'm looking for the right encoding for serialization purpose. – noober Aug 22 '14 at 14:44
-
IDNs should probably be stored in their canonical Punycode encoded form, the rest is then just ASCII; hence the question is pretty moot. – deceze Aug 22 '14 at 14:49
-
Malware commonly wants to edit the hosts file. Not saying your app is malware, but many anti-malware apps will probably think it is. – Bill_Stewart Aug 22 '14 at 15:49
-
1It's an internal GUI tool for our company sysadmins. I hope they know what they're doing. – noober Aug 22 '14 at 16:01
2 Answers
It should be ANSI or UTF-8 without BOM. I just dealt with a server that had the hosts file encoding set to UCS-2 Little Endian, and that led to the file being ignored.
There is a wealth of information here: https://serverfault.com/questions/452268/hosts-file-ignored-how-to-troubleshoot
The simple answer is ANSI or UTF-8 WITH BOM.
(UTF-8 without BOM is NOT valid).
Details:
As far as I have tried, the encoding of the hosts file on Windows should be
ANSI or UTF-8 with BOM.
I know this question is many years old, but a colleague made the mistake of looking at this post and the ServerFault post, so I decided to add an answer.
1. Simple case only ASCII
Works.
Without any multi-byte characters, This is equivalent to ANSI, also equivalent to UTF-8 without BOM.
2. ANSI (with Japanese ANSI multi-byte characters)
Works.
note: There are Japanese characters but this is valid ANSI encoding in windows.
In Japanese editions of Windows, this code page
cp932
is referred to as "ANSI",https://en.wikipedia.org/wiki/Code_page_932_(Microsoft_Windows)
3. UTF-8 with BOM
Works.
note: BOM 付き
means with BOM
.
4. UTF-8 without BOM
DOES NOT work.
5. Additional test cases
If you use emoji instead of Japanese, the result will be the same.
Use emoji and save as UTF8 without BOM
does not work.
(However, other lines not include emoji may be worked correctly.)
Use emoji and save as UTF8 with BOM
can resolve host correctly.
note: If you use Notepad to check it yourself, be sure to put double quotes in the file name when you save it, or Notepad will be create hosts.txt
.
Appended:
(Asked in comment)
The hosts file supports inline comments.

- 912
- 8
- 13
-
I don't think the hosts file supports inline comments; comments have to be on their own line. So that could be affecting results. – Triynko Mar 11 '22 at 19:34
-
@Triynko Microsoft shows it as an example in the hosts file. (Add that to my answer) – fliedonion Mar 29 '22 at 15:22