Skip to content

Instantly share code, notes, and snippets.

@samidunimsara
Created December 5, 2025 16:31
Show Gist options
  • Select an option

  • Save samidunimsara/9885f6d251b2cff79aba75eb4909b7f6 to your computer and use it in GitHub Desktop.

Select an option

Save samidunimsara/9885f6d251b2cff79aba75eb4909b7f6 to your computer and use it in GitHub Desktop.
s
#!/bin/bash
#
# Arch Linux Network Hyper-Optimizer
# Combines TCP BBR, Sysctl tuning, and Power Management for maximum speed/stability.
#
# Usage: sudo ./optimize_net.sh
#
# --- Configuration ---
BACKUP_DIR="/var/backups/network_opt_$(date +%Y%m%d_%H%M%S)"
SYSCTL_CONF="/etc/sysctl.d/99-arch-speed-boost.conf"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
# Check Root
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}Error: This script must be run as root (sudo).${NC}"
exit 1
fi
echo -e "${CYAN}=== Arch Linux Network Optimizer v3.0 ===${NC}"
# --- 1. Backup Current Settings ---
echo -e "\n${YELLOW}[*] Creating Backups in $BACKUP_DIR...${NC}"
mkdir -p "$BACKUP_DIR"
if [ -f /etc/sysctl.conf ]; then cp /etc/sysctl.conf "$BACKUP_DIR/"; fi
sysctl -a > "$BACKUP_DIR/sysctl_original_dump.txt"
echo -e "${GREEN}[+] Backup complete.${NC}"
# --- 2. Enable BBR & Queue Discipline ---
echo -e "\n${YELLOW}[*] Configuring TCP BBR...${NC}"
# Load modules immediately
modprobe tcp_bbr
modprobe sch_fq
# Check if BBR is available
if grep -q "bbr" /proc/sys/net/ipv4/tcp_available_congestion_control; then
echo -e "${GREEN}[+] Kernel supports BBR.${NC}"
else
echo -e "${RED}[!] Error: BBR module not loaded. Are you on Kernel 4.9+?${NC}"
echo "Run 'pacman -Syu' and reboot first."
exit 1
fi
# --- 3. Apply Sysctl Optimizations ---
echo -e "\n${YELLOW}[*] Writing Sysctl Rules to $SYSCTL_CONF...${NC}"
cat <<EOF > "$SYSCTL_CONF"
# --- TCP BBR & Queuing ---
# Use BBR for congestion control (Speed)
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
# --- TCP Buffer Sizes (Speed for High Bandwidth) ---
# Increase max buffer size
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.udp_rmem_min = 8192
net.ipv4.udp_wmem_min = 8192
# --- Connection Management (Smoothness) ---
# Enable TCP Fast Open (Reduces latency)
net.ipv4.tcp_fastopen = 3
# Don't cache metrics (Start fresh every connection)
net.ipv4.tcp_no_metrics_save = 1
# Reduce Keepalive time (Detect dead connections faster)
net.ipv4.tcp_keepalive_time = 120
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 5
# --- Stability & Crash Prevention ---
# MTU Probing (Fixes "hanging" websites due to packet size issues)
net.ipv4.tcp_mtu_probing = 1
# Protection against SYN floods
net.ipv4.tcp_syncookies = 1
# Reuse Time-Wait connections (Better resource usage)
net.ipv4.tcp_tw_reuse = 1
# Increase backlog for high load
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_max_syn_backlog = 8192
EOF
# Apply settings
sysctl --system > /dev/null
echo -e "${GREEN}[+] Sysctl optimizations applied.${NC}"
# --- 4. DNS Optimization (Systemd-Resolved) ---
# Arch uses systemd-resolved by default. We will set Cloudflare/Google DNS there.
if systemctl is-active --quiet systemd-resolved; then
echo -e "\n${YELLOW}[*] Optimizing systemd-resolved DNS...${NC}"
mkdir -p /etc/systemd/resolved.conf.d
cat <<EOF > /etc/systemd/resolved.conf.d/dns_override.conf
[Resolve]
# Cloudflare (Primary) and Google (Backup)
DNS=1.1.1.1 1.0.0.1 8.8.8.8
DNSOverTLS=yes
EOF
systemctl restart systemd-resolved
echo -e "${GREEN}[+] DNS updated to Cloudflare (DoT Enabled).${NC}"
else
echo -e "${YELLOW}[!] systemd-resolved not found. Skipping DNS (Manage via NetworkManager).${NC}"
fi
# --- 5. Network Stability (Disable Power Save) ---
echo -e "\n${YELLOW}[*] Disabling WiFi Power Saving (Fixes random disconnects)...${NC}"
# Create a udev rule to disable power management for all network interfaces permanently
cat <<EOF > /etc/udev/rules.d/70-wifi-powersave.rules
ACTION=="add", SUBSYSTEM=="net", KERNEL=="wl*", RUN+="/usr/bin/iw dev %k set power_save off"
EOF
# Attempt to turn it off immediately for current session
for interface in $(ls /sys/class/net/ | grep -E '^(wlan|wl|wi)'); do
if command -v iw > /dev/null; then
iw dev "$interface" set power_save off 2>/dev/null
echo -e "${GREEN}[+] Power save disabled for $interface${NC}"
fi
done
# --- 6. Verification ---
echo -e "\n${CYAN}=== Verification ===${NC}"
CURRENT_ALGO=$(sysctl net.ipv4.tcp_congestion_control | awk '{print $3}')
CURRENT_QDISC=$(sysctl net.core.default_qdisc | awk '{print $3}')
if [[ "$CURRENT_ALGO" == "bbr" ]]; then
echo -e "Congestion Control: ${GREEN}BBR (Active)${NC}"
else
echo -e "Congestion Control: ${RED}$CURRENT_ALGO (Failed)${NC}"
fi
if [[ "$CURRENT_QDISC" == "fq" ]]; then
echo -e "Queue Discipline: ${GREEN}FQ (Active)${NC}"
else
echo -e "Queue Discipline: ${RED}$CURRENT_QDISC (Failed)${NC}"
fi
echo -e "\n${GREEN}Optimization Complete! Reboot recommended.${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment