Network - Hosts File


A host file is a database name in the form of a file on the local file system.

It is a computer file used by the operating system resolver to map an IP name to an IP address.

The mapping information have a higher priority that the DNS of your network.


The hosts file contains lines of text consisting of:

Each field is separated by white space or tabs

Example:  localhost  localhost localhost.localdomain localhost4 localhost4.localdomain4 
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6  fqdn name1 name2



It is located in the directory for windows:



cat /etc/hosts

# The HOSTFILE shell variable may have the location


When you call:

  • localhost,
  • or

Windows transform it by the IP with the host file below:

# Copyright (c) 1993-1999 Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
# For example:
#          # source server
#              # x client host       localhost       localhost1       localhost2     ngerard   

The entry ( for instance, permit me to have a copy of my website on my computer. When I ask in my browser (and that Apache is also configured with a virtual host for, I will see my local copy of my internet website.



Powershell script for adding/removing/showing entries to the hosts file.


# Known limitations:
# - does not handle entries with comments afterwards ("<ip>    <host>    # comment")

$file = "C:\Windows\System32\drivers\etc\hosts"

function add-host([string]$filename, [string]$ip, [string]$hostname) {
    remove-host $filename $hostname
    $ip + "`t`t" + $hostname | Out-File -encoding ASCII -append $filename

function remove-host([string]$filename, [string]$hostname) {
    $c = Get-Content $filename
    $newLines = @()

    foreach ($line in $c) {
        $bits = [regex]::Split($line, "\t+")
        if ($bits.count -eq 2) {
            if ($bits[1] -ne $hostname) {
                $newLines += $line
        } else {
            $newLines += $line

    # Write file
    Clear-Content $filename
    foreach ($line in $newLines) {
        $line | Out-File -encoding ASCII -append $filename

function print-hosts([string]$filename) {
    $c = Get-Content $filename

    foreach ($line in $c) {
        $bits = [regex]::Split($line, "\t+")
        if ($bits.count -eq 2) {
            Write-Host $bits[0] `t`t $bits[1]

try {
    if ($args[0] -eq "add") {

        if ($args.count -lt 3) {
            throw "Not enough arguments for add."
        } else {
            add-host $file $args[1] $args[2]

    } elseif ($args[0] -eq "remove") {

        if ($args.count -lt 2) {
            throw "Not enough arguments for remove."
        } else {
            remove-host $file $args[1]

    } elseif ($args[0] -eq "show") {
        print-hosts $file
    } else {
        throw "Invalid operation '" + $args[0] + "' - must be one of 'add', 'remove', 'show'."
} catch  {
    Write-Host $error[0]
    Write-Host "`nUsage: hosts add <ip> <hostname>`n       hosts remove <hostname>`n       hosts show"


- name: Add mappings to /etc/hosts
    path: /etc/hosts
    block: |
      {{ item.ip }} {{ }}
    marker: "# {mark} ANSIBLE MANAGED BLOCK {{ }}"
    - { name: host1, ip: }
    - { name: host2, ip: }
    - { name: host3, ip: }

Documentation / Reference

Powered by ComboStrap