Debian 12 com 3 Interfaces: NAT, DHCP e Captive Portal

Este guia detalha a configuração de um servidor Debian 12 com três interfaces de rede:

  • eno1: Interface WAN (acesso à internet via DHCP)
  • eno2: Interface LAN (rede interna com DHCP e acesso à internet via NAT)
  • eno3: Interface OPT (rede isolada com captive portal)

1. Pré-Requisitos

Habilitar o encaminhamento de pacotes IP

echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p

Instalar pacotes necessários

apt update
apt install isc-dhcp-server iptables-persistent build-essential git \
libmicrohttpd-dev libjson-c-dev libncurses-dev libssl-dev pkg-config \
iptables libcurl4-openssl-dev

2. Configuração de Rede

Edite o arquivo /etc/network/interfaces:

auto eno1
iface eno1 inet dhcp

auto eno2
iface eno2 inet static
  address 192.168.10.1
  netmask 255.255.255.0

auto eno3
iface eno3 inet static
  address 192.168.20.1
  netmask 255.255.255.0

Reinicie as interfaces:

ifdown eno1 eno2 eno3 && ifup eno1 eno2 eno3

3. Configuração do Servidor DHCP

Arquivo /etc/dhcp/dhcpd.conf

subnet 192.168.10.0 netmask 255.255.255.0 {
  range 192.168.10.100 192.168.10.200;
  option routers 192.168.10.1;
  option domain-name-servers 8.8.8.8, 1.1.1.1;
}

subnet 192.168.20.0 netmask 255.255.255.0 {
  range 192.168.20.100 192.168.20.200;
  option routers 192.168.20.1;
  option domain-name-servers 8.8.8.8;
}

Definir interfaces de escuta

Edite /etc/default/isc-dhcp-server:

INTERFACESv4="eno2 eno3"

Habilitar e iniciar o serviço

systemctl enable isc-dhcp-server
systemctl restart isc-dhcp-server

4. Configuração de NAT

Regras do iptables

iptables -t nat -A POSTROUTING -o eno1 -j MASQUERADE
iptables -A FORWARD -i eno2 -o eno1 -j ACCEPT
iptables -A FORWARD -i eno1 -o eno2 -m state --state RELATED,ESTABLISHED -j ACCEPT

Salvar regras

iptables-save > /etc/iptables/rules.v4

5. Instalação e Configuração do Nodogsplash

Clonar e compilar o Nodogsplash

git clone https://github.com/nodogsplash/nodogsplash.git /opt/nodogsplash
cd /opt/nodogsplash
make
make install

Criar serviço systemd

Crie o arquivo /etc/systemd/system/nodogsplash.service com o seguinte conteúdo:

[Unit]
Description=Nodogsplash Captive Portal
After=network.target

[Service]
ExecStart=/usr/local/bin/nodogsplash -f -c /etc/nodogsplash/nodogsplash.conf
Restart=always

[Install]
WantedBy=multi-user.target

Ative e inicie o serviço:

systemctl daemon-reexec
systemctl daemon-reload
systemctl enable nodogsplash
systemctl start nodogsplash

Configurar o Nodogsplash

Edite /etc/nodogsplash/nodogsplash.conf:

GatewayInterface eno3
GatewayAddress 192.168.20.1
MaxClients 50
AuthIdleTimeout 3600

# URL de redirecionamento após autenticação
RedirectURL http://192.168.10.1/status.html

# Regras de firewall para tráfego pré-autenticado (não autenticados)
FirewallRule allow tcp port 53   # Permite tráfego DNS
FirewallRule allow udp port 53   # Permite tráfego DNS
FirewallRule allow tcp port 80   # Permite tráfego HTTP
FirewallRule allow tcp port 443  # Permite tráfego HTTPS

# Regras de firewall para usuários autenticados
FirewallRule allow all  # Permite todo o tráfego após a autenticação

Crie a página de autenticação em /etc/nodogsplash/htdocs/splash.html:

<!DOCTYPE html>
<html>
<head><title>Portal Captivo</title></head>
<body>
<h1>Acesso à Rede</h1>
<p>Clique abaixo para navegar</p>
<a href="/nodogsplash_auth?token=authenticated">Liberar Acesso</a>
</body>
</html>

6. Redirecionamento de HTTP e HTTPS

Adicione as seguintes regras ao iptables:

iptables -t nat -A PREROUTING -i eno3 -p tcp --dport 80 -j DNAT --to-destination 192.168.20.1:2050
iptables -t nat -A PREROUTING -i eno3 -p tcp --dport 443 -j DNAT --to-destination 192.168.20.1:2050

Nota: Redirecionar HTTPS pode causar avisos de certificado nos navegadores.

7. Teste Final

  • Conecte um cliente à interface LAN (eno2) e verifique se recebe IP via DHCP e acesso à internet.
  • Conecte um cliente à interface OPT (eno3) e verifique se recebe IP via DHCP e é redirecionado para a página de autenticação do portal.

Rolar para cima