Como Configurar um Servidor VPN WireGuard no OpenWrt

Configuração do Servidor WireGuard no OpenWrt
Código copiado para a área de transferência!

Configuração do Servidor WireGuard no OpenWrt

Este artigo baseia-se nos seguintes aspectos:

  • Acesso à interface web / interface de linha de comando
  • Gerenciamento de configs / pacotes / serviços / logs

Instruções de Linha de Comando

1. Preparação

Instale os pacotes necessários e especifique os parâmetros de configuração para o servidor VPN.

# Instalar pacotes
opkg update &&
opkg install wireguard-tools kmod-wireguard luci-proto-wireguard qrencode
        
# Parâmetros de configuração
VPN_IF="vpn"
VPN_PORT="51820"
VPN_ADDR="192.168.9.1/24"
VPN_ADDR6="fd00:9::1/64"
        

2. Gerenciamento de Chaves

Gere e troque as chaves entre o servidor e o cliente.

# Gerar chaves
umask go=
wg genkey | tee wgserver.key | wg pubkey > wgserver.pub
wg genkey | tee wgclient.key | wg pubkey > wgclient.pub
wg genpsk > wgclient.psk

# Chave privada do servidor
VPN_KEY="$(cat wgserver.key)"

# Chave pré-compartilhada
VPN_PSK="$(cat wgclient.psk)"

# Chave pública do cliente
VPN_PUB="$(cat wgclient.pub)"
        

3. Firewall

Considere a rede VPN como privada. Atribua a interface VPN à zona LAN para minimizar a configuração do firewall. Permita o acesso ao servidor VPN a partir da zona WAN.

# Configurar firewall
uci rename firewall.@zone[0]="lan"
uci rename firewall.@zone[1]="wan"
uci del_list firewall.lan.network="${VPN_IF}"
uci add_list firewall.lan.network="${VPN_IF}"
uci -q delete firewall.wg
uci set firewall.wg="rule"
uci set firewall.wg.name="Allow-WireGuard"
uci set firewall.wg.src="wan"
uci set firewall.wg.dest_port="${VPN_PORT}"
uci set firewall.wg.proto="udp"
uci set firewall.wg.target="ACCEPT"
uci commit firewall
service firewall restart
        

4. Rede

Configure a interface VPN e os peers.

# Configurar rede
uci -q delete network.${VPN_IF}
uci set network.${VPN_IF}="interface"
uci set network.${VPN_IF}.proto="wireguard"
uci set network.${VPN_IF}.private_key="${VPN_KEY}"
uci set network.${VPN_IF}.listen_port="${VPN_PORT}"
uci add_list network.${VPN_IF}.addresses="${VPN_ADDR}"
uci add_list network.${VPN_IF}.addresses="${VPN_ADDR6}"
        
# Adicionar peers VPN
uci -q delete network.wgclient
uci set network.wgclient="wireguard_${VPN_IF}"
uci set network.wgclient.public_key="${VPN_PUB}"
uci set network.wgclient.preshared_key="${VPN_PSK}"
uci add_list network.wgclient.allowed_ips="${VPN_ADDR%.*}.2/32"
uci add_list network.wgclient.allowed_ips="${VPN_ADDR6%:*}:2/128"
uci commit network
service network restart
        

Teste

Estabeleça a conexão VPN. Verifique seu roteamento com traceroute e traceroute6.

traceroute openwrt.org
traceroute6 openwrt.org
    

Solução de Problemas

Coleta e análise das seguintes informações.

# Reiniciar serviços
service log restart; service network restart; sleep 10

# Log e status
logread -e vpn; netstat -l -n -p | grep -e "^udp\s.*\s-$"

# Configuração em tempo real
pgrep -f -a wg; wg show; wg showconf vpn
ip address show; ip route show table all
ip rule show; ip -6 rule show; nft list ruleset

# Configuração persistente
uci show network; uci show firewall; crontab -l
    

Postar um comentário

Postagem Anterior Próxima Postagem