Skip to content

Instantly share code, notes, and snippets.

@loplex
Created August 4, 2020 05:19
Show Gist options
  • Save loplex/a49da6035495c2a7e2f74202854fbe67 to your computer and use it in GitHub Desktop.
Save loplex/a49da6035495c2a7e2f74202854fbe67 to your computer and use it in GitHub Desktop.
#!/bin/bash -e
###
### * Save this script and make it executable:
### 1. Put the script to your filesystem into this filepath:
### - /usr/local/bin/fix-etc-resolv.conf-wsl.sh
### 2. Make it excecutable using:
### - sudo chmod a+x /usr/local/bin/fix-etc-resolv.conf-wsl.sh
###
### * Allow the script to be run as root without providing a password using:
### 1. Run:
### - sudo visudo -f /etc/sudoers.d/42-lopin_fix-etc-resolv-conf-wsl
### 2. Paste line:
### - ALL ALL=(ALL:ALL) NOPASSWD:/usr/local/bin/fix-etc-resolv.conf-wsl.sh
### 3. Save & exit
###
### * Make the script to run automatically via your ~/.bashrc by either:
### a) Manually - by appending the following line into your ~/.bashrc:
### - sudo /usr/local/bin/fix-etc-resolv.conf-wsl.sh
### b) Using this shell command:
### - echo "sudo /usr/local/bin/fix-etc-resolv.conf-wsl.sh" >> ~/.bashrc
###
resolvFile="/etc/resolv.conf"
linesToBeLastRegEx='^nameserver 8\.8\.(4\.4|8\.8)$'
backupFile="/tmp/resolv.conf.BAK.$(date +"%F_%H-%M-%S")"
cp "$resolvFile" "$backupFile"
cat \
<(echo "# Processed by: $0 $@") \
<(cat "$backupFile" | egrep -v "$linesToBeLastRegEx") \
<(cat "$backupFile" | egrep "$linesToBeLastRegEx") \
\
> "$resolvFile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment