What is the hosts file ?

Map Of Internet 1973

About

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.

Format

The hosts file contains lines of text consisting of:

Each field is separated by white space or tabs

Example:

127.0.0.1  localhost
127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4 
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

127.0.0.1  fqdn name1 name2

OS

Windows

It is located in the directory for windows:

C:\WINDOWS\system32\drivers\etc\hosts

Linux

cat /etc/hosts

# The HOSTFILE shell variable may have the location
echo $HOSTFILE

Example

When you call:

  • localhost,
  • or gerardnico.com

Windows transform it by the IP 127.0.0.1 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:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost
127.0.0.1       localhost1
127.0.0.1       localhost2
10.10.10.10     ngerard.nf.newfrontiers.nl     ngerard
127.0.0.1       gerardnico.com   

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

Management

Powershell

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

From https://gist.github.com/markembling/173887

# 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"
}

Ansible

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

Documentation / Reference





Discover More
Card Puncher Data Processing
Apache - Virtual Host (Vhost)

virtual host configuration with Apache in the Apache Configuration file (apache/conf/httpd.conf), uncomment the line “Include conf/extra/httpd-vhosts.conf” Example: Below is an example...
Azure Firewall Public Ip Restriction
Azure - Oracle Database Manually Installation on Linux

The installation of the Oracle database on bare-bone Linux. For a quick an dirty installation, see Creation of a VM with Oracle Linux 7 Update 4 and a Standard_DS1_v2...
Bash Liste Des Attaques Ovh
Bash - Complete (Builtin command) - Completion

The programmable completion feature in Bash permits typing a partial command, then pressing the [Tab] key to auto-complete the command sequence The process of applying these completion specifications...
Map Of Internet 1973
DNS - nslookup command line

Nslookup is a DNS client program to query (ie lookup) domain name servers and get back DNS record information nslookuphost fileping Nslookup has two modes: The local DNS file is /etc/resolv.conf...
Map Of Internet 1973
DNS clients - A list of client that can query the DNS database

A DNS client is an application that calls the resolver to query name database in order to perform forward lookup (IP from name) or reverse lookup(Name from IP). This page lists this application
Toolbox Componentns
Docker - Installation for Windows 7 (Docker Toolbox)

How I have installed and configured Docker in my Windows 7 laptop. The software for windows 7 is called Docker Toolbox. Before version Windows 10: Toolbox:...
HTTP - Client (User agents)

in HTTP. An HTTP client in HTTP is also known as the user agents because user-agent is the name of a header field that identify the client. HTTP identifies the client (ie user-agent_ using the user-agent...
How to see and update the Fully Qualified Domain Name (FQDN) on Linux

This article shows you how to manage the Fully Qualified Domain Name (FQDN) in Linux You can check: the FQDN or the domain name The recommended method of setting the FQDN is to make...
Kafka Commit Log Messaging Process
Kafka - Stream Word Count demo

Stream Word count demo adapted from the stream quickstart. Install docker: Map the hostname zookeeper and broker to your docker host...
Kafka Commit Log Messaging Process
Kafka Connect - Sqlite in Distributed Mode

Sqlite JDBC source connector demo. The same steps than in the article but with a distributed worker Install Docker: Install Git: If you want to make the call with the kafka console utilities...



Share this page:
Follow us:
Task Runner