只允许一个特定的端口可以同时连接一个IP地址

我有一个开放的端口,40002,我想限制在同一时间的端口只能通过一个IP地址(不具体地址)连接。 如果有一个IP地址已经连接到该端口,则其他IP将无法连接。

是可以通过iptables或脚本来configuration它? 我的系统是Ubuntu 14.04谢谢。

你可以通过configurationiptables来完成。

/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 

testing:

 #!/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限制模块的例子:

 iptables -A INPUT -p tcp --syn -dport 40002 -m iplimit --iplimit-above 3 -J REJECT 

如果有3个IP连接,这将拒绝连接。 对不起,如果我误解了你的问题;)