Created: 2014-05-19 04:53
Updated: 2016-03-26 14:46


A CLI tool to easily switch between different hosts settings


$ host-switch help
    host-switch                     show current setting
    host-switch list <set>          show current setting of specific set
    host-switch dig <set>           show current host table of specific set
    host-switch all-off <set>       (root) turn off all local settings (Use DNS)
    host-switch on <set> <group>    (root) to switch <group> hosts setting ON


  • GROUP: indicate one specific setting of some hostnames
  • SET: indicate an aggregation of hosts. You can define several setting group for one set, but only one group setting can be activated simultaneously.


  1. For example, I have two host-set in my daily development, say foo and bar.

  2. So I prepare my /etc/hosts file as the following formats:

     # # begin foo:DEV-my host setting
     # # put any comment / description here if you want
     # # end foo:DEV-my host setting
     # # begin foo:STAGING host setting
     # # end foo:STAGING host setting
     # # begin bar:QA host setting
     # # end bar:QA host setting
     # # begin bar:testing host setting
     # # end bar:testing host setting

    Note: host setting name (case-sesitive, which are DEV-my and STAGING in the above case) should match on begin and end line.

  3. Then I can use CLI to manage my hosts table switching:

    Show current setting

     $ host-switch

    Show current setting for foo set

     $ host-switch list set

    Turn on DEV-my host setting for foo set

     $ sudo host-switch on foo DEV-my

    Turn on STAGING host setting (and implicitly turn off other exists setting)

     $ sudo host-switch on foo STAGING

    Turn off all local host setting (will use global DNS lookup)

     $ sudo host-switch all-off foo

    Are you lazy to type password for sudo? Then you can use the shell feature. Behind the shell prompt, all the commands are the same as the CLI mode, but no need to type the script name (i.e., host-switch should be stripped).

     $ sudo host-switch shell


FreeBSD License

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more