更新

更新

我像这样禁用防火墙:

#!/bin/bash
iptables -F
iptables -X
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT

更新

我还启用了防火墙:

 #!/bin/bash

ssh=x.x.x.x
http='x.x.x.x y.y.y.y'

# Clear any previous rules.
iptables -F

# Default drop policy.
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

# Allow anything over loopback.
iptables -A INPUT -i lo -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
iptables -A OUTPUT -o lo -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT

# Drop any tcp packet that does not start a connection with a syn flag.
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP

# Drop any invalid packet that could not be identified.
iptables -A INPUT -m state --state INVALID -j DROP

# Drop invalid packets.
iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,FIN SYN,FIN              -j DROP
iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,RST SYN,RST              -j DROP
iptables -A INPUT -p tcp -m tcp --tcp-flags FIN,RST FIN,RST              -j DROP
iptables -A INPUT -p tcp -m tcp --tcp-flags ACK,FIN FIN                  -j DROP
iptables -A INPUT -p tcp -m tcp --tcp-flags ACK,URG URG                  -j DROP

# Allow TCP/UDP connections out. Keep state so conns out are allowed back in.
iptables -A INPUT  -p tcp -m state --state ESTABLISHED     -j ACCEPT
iptables -A OUTPUT -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT  -p udp -m state --state ESTABLISHED     -j ACCEPT
iptables -A OUTPUT -p udp -m state --state NEW,ESTABLISHED -j ACCEPT

# Allow only ICMP echo requests (ping) in. Limit rate in. Uncomment if needed.
iptables -A INPUT  -p icmp -m state --state NEW,ESTABLISHED --icmp-type echo-reply -j ACCEPT
iptables -A OUTPUT -p icmp -m state --state NEW,ESTABLISHED --icmp-type echo-request -j ACCEPT

iptables -A INPUT -m state --state NEW,ESTABLISHED,RELATED --source $ssh -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -m state --state NEW,ESTABLISHED,RELATED -p tcp --dport 22 -j DROP

for web in $http; do
    iptables -A INPUT -m state --state NEW,ESTABLISHED,RELATED --source $web -p tcp -m multiport --dports 80,443 -j ACCEPT
    iptables -A INPUT -m state --state NEW,ESTABLISHED,RELATED -p tcp -m multiport --dports 80,443 -j DROP
done


# or block ICMP allow only ping out
iptables -A INPUT  -p icmp -m state --state NEW -j DROP
iptables -A INPUT  -p icmp -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p icmp -m state --state NEW,ESTABLISHED -j ACCEPT

所以我希望x.x.x.x能够通过 ssh 连接到我。我还希望以下 ip 能够看到我的80,443端口:

x.x.x.x
y.y.y.y

我怎样才能改变它?

答案1

我认为您的规则顺序错误。在 iptables 中,-A 追加到链条上,而不是将其插入到链条之前。通常情况下,你有...

iptables -A INPUT  -p tcp -m state --state ESTABLISHED     -j ACCEPT

进而

# Drop any tcp packet that does not start a connection with a syn flag.
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP

您可以使用 重写它,而不是更改顺序-I INPUT 1,以便将每个规则插入到前一个规则之前,即插入到顶部。但在这种情况下,必须移动尾部 ping/icmp 规则。

现在,回到你的问题:

iptables -N SSH_CHECK
iptables -N HTTP_CHECK

iptables -A INPUT -p tcp --dport 22 -j SSH_CHECK
iptables -A INPUT -p tcp --dport 80 -j HTTP_CHECK
iptables -A INPUT -p tcp --dport 443 -j HTTP_CHECK

iptables -A SSH_CHECK -s x.x.x.x -j ACCEPT -m comment --comment "allow joe to ssh from his IP"
iptables -A HTTP_CHECK -s y.y.y.y -j ACCEPT -m comment --comment "allow mary to visit my HTTP/S server"

您的默认丢弃策略会处理不合格的 IP。

相关内容