Hosts file switcher on linux

Solution 1:

Host Switcher

A small tool to manage and switch host config. It was tested in Linux, Windows and Mac OS X. (does not work on ubuntu < 10.10 due to Qt 4.7 requirement)

Host Profiles

Host Profiles is a cross platofrm host file management tool. Quickly switch host profiles and flush dns automatically. Currently, only root user can launch the program.

Dirk Krause's tools - hostsadm

The hostsadm program reads a MySQL database and creates several configuration, documentation files, and license reports. Host related information on Linux/Unix systems is maintained in several configuration files (i.e. /etc/hosts, /etc/ethers, /etc/netgroup or the corresponding LDAP OUs). Adding or modifying a host requires consistent modifications in multiple places. Having all information about a host in one place allows easier maintenance and ensures consistency.

The network documentation is generated from the same database as the configuration files, so it is consistent to the configuration. As the license report is generated using from the same database the host names are consistent to the network documentation.

Firefox: SwitchHosts

SwitchHosts allows you to manage and switch between multiple hosts file quickly and easily. Of course, the refresh of DNS is executed at once. The Hosts file is audited, security is safe.

Not what you asked for, but also worth knowing about:

GNU Name Service Switch

Various functions in the C Library need to be configured to work correctly in the local environment. Traditionally, this was done by using files (e.g., /etc/passwd), but other nameservices (like the Network Information Service (NIS) and the Domain Name Service (DNS)) became popular, and were hacked into the C library, usually with a fixed search order (see frobnicate).

The GNU C Library contains a cleaner solution of this problem. It is designed after a method used by Sun Microsystems in the C library of Solaris 2. GNU C Library follows their name and calls this scheme Name Service Switch (NSS).

Though the interface might be similar to Sun's version there is no common code. We never saw any source code of Sun's implementation and so the internal interface is incompatible. This also manifests in the file names we use as we will see later.

Avoid editing ‘hosts’ file by using FoxyProxy plugin for FireFox

FoxyProxy acts as a proxy server between the Firefox and the internet. You set a ‘proxy’ which essentially allows you to direct traffic to a single point – I use one that directs traffic to ‘localhost’ – then you can add as many patterns as you want that will be associated with the Proxy. Once all is setup, you can then turn this Proxy on or off with a single right click at the bottom of the browser. For me, the setup is quite simple and it works great.

Ok, so there is one caviat here: sometimes I do need to test the websites in different browsers. In this case, this browser add-on will simply not cut it. But for most of my daily web dev needs – it is great!

Dnsmasq

Dnsmasq is a lightweight, easy to configure DNS forwarder and DHCP server. It is designed to provide DNS and, optionally, DHCP, to a small network. It can serve the names of local machines which are not in the global DNS. The DHCP server integrates with the DNS server and allows machines with DHCP-allocated addresses to appear in the DNS with names configured either in each host or in a central configuration file. Dnsmasq supports static and dynamic DHCP leases and BOOTP/TFTP/PXE for network booting of diskless machines.

Dnsmasq is targeted at home networks using NAT and connected to the internet via a modem, cable-modem or ADSL connection but would be a good choice for any smallish network (up to 1000 clients is known to work) where low resource use and ease of configuration are important.

Supported platforms include Linux (with glibc and uclibc), Android, *BSD, Solaris and Mac OS X. Dnsmasq is included in at least the following Linux distributions: Gentoo, Debian, Slackware, Suse, Fedora, Smoothwall, IP-Cop, floppyfw, Firebox, LEAF, Freesco, fli4l, CoyoteLinux, Endian Firewall and Clarkconnect. It is also available as FreeBSD, OpenBSD and NetBSD ports and is used in Linksys wireless routers (dd-wrt, openwrt and the stock firmware) and the m0n0wall project.

Dnsmasq provides the following features:

  • The DNS configuration of machines behind the firewall is simple and doesn't depend on the details of the ISP's dns servers
  • Clients which try to do DNS lookups while a modem link to the internet is down will time out immediately.
  • Dnsmasq will serve names from the /etc/hosts file on the firewall machine: If the names of local machines are there, then they can all be addressed without having to maintain /etc/hosts on each machine.
  • The integrated DHCP server supports static and dynamic DHCP leases and multiple networks and IP ranges. It works across BOOTP relays and supports DHCP options including RFC3397 DNS search lists. Machines which are configured by DHCP have their names automatically included in the DNS and the names can specified by each machine or centrally by associating a name with a MAC address in the dnsmasq config file.
  • Dnsmasq caches internet addresses (A records and AAAA records) and address-to-name mappings (PTR records), reducing the load on upstream servers and improving performance (especially on modem connections).
  • Dnsmasq can be configured to automatically pick up the addresses of its upstream nameservers from ppp or dhcp configuration. It will automatically reload this information if it changes. This facility will be of particular interest to maintainers of Linux firewall distributions since it allows dns configuration to be made automatic.
  • On IPv6-enabled boxes, dnsmasq can both talk to upstream servers via IPv6 and offer DNS service via IPv6. On dual-stack (IPv4 and IPv6) boxes it talks both protocols and can even act as IPv6-to-IPv4 or IPv4-to-IPv6 forwarder.
  • Dnsmasq can be configured to send queries for certain domains to upstream servers handling only those domains. This makes integration with private DNS systems easy.
  • Dnsmasq supports MX and SRV records and can be configured to return MX records for any or all local machines.

This thread suggests using WICD pre/post scripts, so that selecting a different network in the WICD GUI automatically switches your /etc/hosts file:

Ubuntu Forums - Switch between hosts file based on connection

The WICD Network Manager is capable of doing exactly what you want. You can install it via Synaptic (or the Software Center). Beware that since it does the same as the default Network Manager, this will uninstall the latter. You can always swap back by installing the network-manager-gnome package.

With WICD, you can use different pre/post connection scripts for different wireless and wired networks.

In case you want to keep the default Network Manager, there is the option of putting scripts directly in the /etc/network/if-* folders... though I have little experience with those because I'm happily using WICD. ;)

Solution 2:

Yes, you can try dacuoxian -- also on github

It does not provide a GUI, but it's easier to use than GUI programs. It's a shell script that you can run it with a single command.

For example,

$ sudo ./dacuoxian.sh google-hosts