Skip to content

Instantly share code, notes, and snippets.

@alandipert
Created November 7, 2016 19:20
Show Gist options
  • Select an option

  • Save alandipert/6bea29ea46087e27468cd8e070b708c7 to your computer and use it in GitHub Desktop.

Select an option

Save alandipert/6bea29ea46087e27468cd8e070b708c7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# with_host.sh 54.90.136.89 foo.bar.com chromium-browser
# Runs a program with a one-off /etc/hosts via unshare(1)
# http://unix.stackexchange.com/a/242830
if [[ $1 == "--" ]]; then
mount $2 /etc/hosts --bind
USER=$3
PROGNAME=$4
shift 4
exec sudo -u $USER -- "$PROGNAME" "$@"
else
set -e
HOSTIP=$1
HOSTNAME=$2
PROGNAME="$3"
TMPHOSTS=$(mktemp)
cp /etc/hosts $TMPHOSTS
echo "$HOSTIP $HOSTNAME" >> $TMPHOSTS
echo "Temporary hosts file: $TMPHOSTS"
shift 3
sudo unshare --mount $0 -- $TMPHOSTS $(whoami) "$PROGNAME" "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment