How to add entry to /etc/hosts with a built-in command line

It turns out that editing /etc/hosts is enough to add an entry of DNS.

Is there a built-in cmd , where we can:

  1. add host

    cmd: $ sudo mycmd 10.103.23.34 myme.com

    Result : 10.103.23.34 myme.com

  2. Add hostname

    cmd : $ sudo mycmd 10.103.23.34 omac.org

    Result : 10.103.23.34 myme.com omac.org

  3. Modify IP

    cmd : $ sudo mycmd 10.103.23.18 omac.org

    Result : 10.103.23.18 myme.com omac.org


Solution 1:

After seeking for CLI for /etc/hosts , we find : https://github.com/macmade/host-manager

Amazing :

host-manager -add www.example.org 127.0.0.1
host-manager -remove www.example.org

Solution 2:

I tried to get the host-manager tool listed above to work but it looks like maybe it's for Windows or macOS? It was trying to cross compile and didn't look right for Ubuntu. I found this tool 'Hostess' though which works great and seems to do exactly what the OP was looking for:

https://github.com/cbednarski/hostess

hostess add local.example.com 127.0.0.1
hostess add staging.example.com 10.0.2.16