
開いているポート 40002 がありますが、同時にポートに接続できる IP アドレスを 1 つだけ (特定のアドレスではない) に制限したいと考えています。そのポートにすでに IP アドレスが接続している場合、他の IP は接続に失敗します。
Iptables またはスクリプトで設定することは可能ですか? 私のシステムは Ubuntu 14.04 です。ありがとうございます。
答え1
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
例: IP / ホストごとに SSH 接続を制限する
/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
テスト:
#!/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: 最大 n 個の接続を制限するには、ip limit モジュールを使用する例を次に示します。
iptables -A INPUT -p tcp --syn -dport 40002 -m iplimit --iplimit-above 3 -J REJECT
3 つの IP が接続されている場合、接続は拒否されます。質問を誤解していたら申し訳ありません ;)