Skip to content

Instantly share code, notes, and snippets.

@TrQ-Hoan
Created April 7, 2025 08:20
Show Gist options
  • Save TrQ-Hoan/a93ed1acca8da95dd1d851e3b2b05732 to your computer and use it in GitHub Desktop.
Save TrQ-Hoan/a93ed1acca8da95dd1d851e3b2b05732 to your computer and use it in GitHub Desktop.
Add dns for WSL
#!/bin/bash
RESOLV_CONF="/etc/resolv.conf"
NEW_NAMESERVERS=(
"nameserver 45.90.28.0"
"nameserver 45.90.30.0"
"nameserver 208.67.222.222"
"nameserver 208.67.220.220"
"nameserver 8.8.8.8"
"nameserver 1.1.1.1"
)
MARKER_COMMENT="# Added by custom script" # Optional: To prevent adding multiple times
# --- Check for root privileges ---
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# --- Check if file exists ---
if [[ ! -f "$RESOLV_CONF" ]]; then
echo "Error: $RESOLV_CONF not found."
exit 1
fi
# --- Optional: Check if already added ---
if grep -qF "$MARKER_COMMENT" "$RESOLV_CONF"; then
echo "Nameservers seem to be already added (marker found). Exiting."
exit 0
fi
# More robust check (optional): Check for the first specific IP
# first_new_ns=$(echo "${NEW_NAMESERVERS[0]}" | awk '{print $2}')
# if grep -q "nameserver $first_new_ns" "$RESOLV_CONF"; then
# echo "First new nameserver (${first_new_ns}) already found. Exiting to prevent duplicates."
# exit 0
# fi
# --- Prepare the nameserver block ---
ns_block=""
if [[ -n "$MARKER_COMMENT" ]]; then
ns_block+="$MARKER_COMMENT\n"
fi
for ns in "${NEW_NAMESERVERS[@]}"; do
ns_block+="$ns\n"
done
# Remove trailing newline
ns_block=${ns_block%\\n}
# --- Use awk to insert the block ---
awk -v ns="$ns_block" '
BEGIN { inserted = 0 }
# Print comments and empty lines directly
/^\s*#/ || /^\s*$/ { print; next }
# If not a comment/empty and not inserted yet, print new servers first
!inserted {
print ns
inserted = 1
}
# Print the current non-comment line (either after insertion or if already inserted)
{ print }
# Handle case where file only has comments/empty lines
END {
if (!inserted) {
print ns
}
}
' "$RESOLV_CONF" > "${RESOLV_CONF}.tmp" && mv "${RESOLV_CONF}.tmp" "$RESOLV_CONF"
if [[ $? -eq 0 ]]; then
echo "Successfully updated $RESOLV_CONF"
# Optional: Set permissions if needed (mv usually preserves them)
# chmod 644 "$RESOLV_CONF"
# chown root:root "$RESOLV_CONF" # Or appropriate owner/group
else
echo "Error updating $RESOLV_CONF"
rm -f "${RESOLV_CONF}.tmp" # Clean up temp file on failure
exit 1
fi
exit 0
#!/bin/bash
RESOLV_CONF="/etc/resolv.conf"
NEW_NAMESERVERS=(
"nameserver 45.90.28.0"
"nameserver 45.90.30.0"
"nameserver 208.67.222.222"
"nameserver 208.67.220.220"
"nameserver 8.8.8.8"
"nameserver 1.1.1.1"
)
MARKER_COMMENT="# Added by custom script" # Optional: To prevent adding multiple times
# --- Check for root privileges ---
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# --- Check if file exists ---
if [[ ! -f "$RESOLV_CONF" ]]; then
echo "Error: $RESOLV_CONF not found."
exit 1
fi
# --- Optional: Check if already added ---
if grep -qF "$MARKER_COMMENT" "$RESOLV_CONF"; then
echo "Nameservers seem to be already added (marker found). Exiting."
exit 0
fi
# More robust check (optional): Check for the first specific IP
# first_new_ns=$(echo "${NEW_NAMESERVERS[0]}" | awk '{print $2}')
# if grep -q "nameserver $first_new_ns" "$RESOLV_CONF"; then
# echo "First new nameserver (${first_new_ns}) already found. Exiting to prevent duplicates."
# exit 0
# fi
# --- Create temporary file ---
TMP_FILE=$(mktemp)
if [[ ! -f "$TMP_FILE" ]]; then
echo "Error: Could not create temporary file."
exit 1
fi
# Ensure temp file is removed on exit/error
trap 'rm -f "$TMP_FILE"' EXIT
inserted=0
# --- Process the original file ---
while IFS= read -r line || [[ -n "$line" ]]; do
# Check if it's a comment or empty line (allowing whitespace before #)
if [[ "$line" =~ ^[[:space:]]*# ]] || [[ "$line" =~ ^[[:space:]]*$ ]]; then
echo "$line" >> "$TMP_FILE"
else
# First non-comment/non-empty line found, insert new nameservers if not already done
if [[ $inserted -eq 0 ]]; then
if [[ -n "$MARKER_COMMENT" ]]; then
echo "$MARKER_COMMENT" >> "$TMP_FILE" # Add marker
fi
for ns in "${NEW_NAMESERVERS[@]}"; do
echo "$ns" >> "$TMP_FILE"
done
inserted=1
fi
# Add the current non-comment line
echo "$line" >> "$TMP_FILE"
fi
done < "$RESOLV_CONF"
# --- Handle edge case: File only contained comments/empty lines ---
if [[ $inserted -eq 0 ]]; then
if [[ -n "$MARKER_COMMENT" ]]; then
echo "$MARKER_COMMENT" >> "$TMP_FILE" # Add marker
fi
for ns in "${NEW_NAMESERVERS[@]}"; do
echo "$ns" >> "$TMP_FILE"
done
fi
# --- Replace the original file ---
# Get original permissions and ownership (optional but good practice)
ORIG_PERMS=$(stat -c "%a" "$RESOLV_CONF")
ORIG_OWNER=$(stat -c "%u:%g" "$RESOLV_CONF")
# Move temp file to original location (atomic operation)
if mv "$TMP_FILE" "$RESOLV_CONF"; then
echo "Successfully updated $RESOLV_CONF"
# Restore permissions and ownership
chmod "$ORIG_PERMS" "$RESOLV_CONF"
chown "$ORIG_OWNER" "$RESOLV_CONF"
# Clean up trap (mv already removed TMP_FILE)
trap - EXIT
else
echo "Error: Failed to replace $RESOLV_CONF"
# Trap will clean up TMP_FILE
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment