How do I create my own custom hosts file?

For some specific reasons I don't want to edit the /etc/hosts file and that is why I needed to create my own hosts file in suppose ~/hosts and use it for all IP to HOSTNAME mapping along with the system's /etc/hosts file. How will I be able to do this?


Solution 1:

Using the environment variable HOSTALIASES:

  1. Define the environment variable HOSTALIASES

    export HOSTALIASES=~/.hosts
    
  2. Create your private hosts file and add a test entry

    echo 'myprivategoogle www.google.com' > ~/.hosts
    
  3. Start a test

    curl myprivategoogle
    

    without the entry in ~/.hosts you would get

    curl: (6) Could not resolve host: myprivategoogle
    

    with the entry you have

    <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
    <TITLE>302 Moved</TITLE></HEAD><BODY>
    <H1>302 Moved</H1>
    The document has moved
    <A HREF="http://www.google.com/">here</A>.
    </BODY></HTML>