Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Windows 'hosts' encoding is?

Tags:

hosts

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?

like image 220
noober Avatar asked Oct 31 '22 18:10

noober


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

like image 66
nealibob Avatar answered Nov 08 '22 04:11

nealibob


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.

Simple case

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.

ANSI

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.

UTF8 with BOM

note: BOM 付き means with BOM.

4. UTF-8 without BOM

DOES NOT work.

UTF8 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.)

emoji and without BOM does not work

Use emoji and save as UTF8 with BOM can resolve host correctly. emoji and with BOM WORK

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.

do not forget double-quote when save as hosts with notepad.exe

Appended: (Asked in comment) The hosts file supports inline comments. enter image description here

like image 44
fliedonion Avatar answered Nov 08 '22 05:11

fliedonion