
Tenho uma porta aberta, 40002, quero limitar que ao mesmo tempo a porta só possa ser conectada por um endereço IP (não um endereço específico). se já houver um endereço IP conectado a essa porta, outros IPs não conseguirão se conectar.
é possível configurá-lo por Iptables ou scripts? meu sistema é Ubuntu 14.04 obrigado.
Responder1
Você pode fazer isso configurando o iptables.
/sbin/iptables -A INPUT -p tcp --syn --dport $port -m connlimit --connlimit-above N -j REJECT --reject-with tcp-reset
# save the changes see iptables-save man page, the following is redhat and friends specific command
service iptables save
Exemplo: Limitar conexões SSH por IP/host
/sbin/iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 3 -j REJECT
# save the changes see iptables-save man page, the following is redhat and friends specific command
service iptables save
TESTE:
#!/bin/bash
ip="202.1.2.3"
port="80"
for i in {1..100}
do
# do nothing just connect and exit
echo "exit" | nc ${ip} ${port};
done
OK: Para limitar no máximo n conexões, aqui está um exemplo usando o módulo ip limit:
iptables -A INPUT -p tcp --syn -dport 40002 -m iplimit --iplimit-above 3 -J REJECT
Isso REJEITARÁ conexões se houver 3 IPs conectados. Desculpe se entendi mal sua pergunta ;)