Skip to content

Instantly share code, notes, and snippets.

@ravage
Created February 8, 2015 15:36
Show Gist options
  • Save ravage/9360e129f4c90ce90c47 to your computer and use it in GitHub Desktop.
Save ravage/9360e129f4c90ce90c47 to your computer and use it in GitHub Desktop.
Replace 127.0.1.1 in /etc/hosts entry with real address
#!/bin/sh
# find the interface with the default route
interface=$(ip route | grep default | awk '{ print $5; exit }')
# get ip address from discovered interface
address=$(ifconfig $interface | egrep '([0-9]{1,3}\.){3}[0-9]{1,3}' -o | head -1)
# prepare address for sed
escaped_address=$(echo $address | sed -E 's/\./\\\./g')
# what to replace
search_address=127\.0\.1\.1
# execute replace saving the file with .bak extension
sed -i.bak "s/$search_address/$escaped_address/g" /etc/hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment