着信 SSH および ICMP のみを許可する Iptables ポリシー

着信 SSH および ICMP のみを許可する Iptables ポリシー

iptables着信SSHおよびICMP接続を許可し、他のすべての接続をブロックするために、次のコマンドを実行しました。

sudo iptables -F INPUT
sudo iptables --policy INPUT ACCEPT && sudo iptables --policy OUTPUT 
ACCEPT && sudo iptables --policy FORWARD ACCEPT
sudo iptables -I INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -I INPUT -p icmp -j ACCEPT
sudo iptables -I OUTPUT -p tcp --sport 5051  -j REJECT
sudo iptables -A INPUT -j REJECT

生成されたiptablesのリストは次のとおりです。

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     tcp  --  anywhere             anywhere             match-set minuteman dst,dst tcp flags:FIN,SYN,RST,ACK/SYN reject-with icmp-port-unreachable
DOCKER-ISOLATION  all  --  anywhere             anywhere            
DOCKER     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     tcp  --  anywhere             anywhere             tcp spt:ita-agent reject-with icmp-port-unreachable
REJECT     tcp  --  anywhere             anywhere             match-set minuteman dst,dst tcp flags:FIN,SYN,RST,ACK/SYN reject-with icmp-port-unreachable

Chain DOCKER (1 references)
target     prot opt source               destination         

Chain DOCKER-ISOLATION (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             anywhere            

SSHを許可する行が他のすべてのトラフィックを拒否する行の前にあることを確認しました。しかし、まだSSH経由でコンピュータに接続することはできません。

これがうまくいかない理由についての手がかりはありますか?

ベストアンサー1

SSHとICMPのみを許可する必要がある場合

# Flush the FW Rules 
iptables -F
iptables  -X

# Block all traffic
iptables  -P INPUT DROP
iptables  -P FORWARD DROP
iptables  -P OUTPUT DROP


# Allow SSH
iptables  -A OUTPUT -p tcp --dport 22 -j ACCEPT 
iptables  -A INPUT -p tcp --dport 22 -j ACCEPT


# Allow ICMP (ping)
iptables  -A INPUT -p icmp -j ACCEPT
iptables  -A OUTPUT -p icmp -j ACCEPT

おすすめ記事