Skip to content

Instantly share code, notes, and snippets.

@zipizap
Created September 4, 2010 11:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zipizap/565137 to your computer and use it in GitHub Desktop.
Save zipizap/565137 to your computer and use it in GitHub Desktop.
Script to configure dhclient to use as primary DNS the OpenDns servers
#!/bin/bash
DHCLIENTCONF_FILE="/etc/dhcp3/dhclient.conf"
#Prepend Opendns name-servers into DHCLIENTCONF_FILE
if [ "$(id -u)" -ne 0 ]; then
echo "Must be run as root"
exit 1
fi
if [ "$(egrep '^prepend domain-name-servers' $DHCLIENTCONF_FILE | wc -l)" -ne 0 ]; then
echo "There is already a line 'prepend domain-name-servers ...' in $DHCLIENTCONF_FILE - check it"
echo "Aborting"
exit 1
fi
echo " Resume of what's about to happen:"
echo " Everytime dhclient executes, the returned DNSs sent by the dhcp-server delete and overwrite the previous-ly existing /etc/resol.conf, leaving only the server-returned DNSs in it"
echo " This script will change the configuration file of dhclient (which is $DHCLIENTCONF_FILE), so that dhclient will always include in the resolv.conf, the Opendns servers before the DNSs sent by the dhcp-server"
echo ""
echo " (press any key to continue, or CTRL-C to abort)" && read
echo ""
echo "..Starting execution"
echo "..Prepend'ing Opendns servers into $DHCLIENTCONF_FILE "
echo '' >> $DHCLIENTCONF_FILE
echo '#:) Prepend the OpenDns servers into /etc/resolv.conf (which is overwritten everytime dhclient runs)' >> $DHCLIENTCONF_FILE
echo 'prepend domain-name-servers 208.67.220.220, 208.67.220.222;' >> $DHCLIENTCONF_FILE
echo "..Finished - from the next time dhclient executes, it will always use the Opendns as first DNS option, and use the server-returned DNSs as secondary option"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment