I am trying to block all ip's other few ip's given in OUTPUT
Chain.
When I install Iptables OUTPUT
chain was empty & input chain added STATED, ESTABLISHED
ip's along with REJECT all ip's other than given INPUT
chain rules.
It works, I only needed to add ssh, and other ports to allow traffic from input chain.
REJECT rule got added in INPUT
when I installed iptables.
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT tcp -- anywhere anywhere tcp dpt:<....>
2 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
3 ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
4 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
For OUTPUT
chain, it was empty.I added below rule which will accept ssh connection for outbound traffic and some other ports and saved the rule, it works.
sudo iptables -I OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT
sudo iptables -I OUTPUT -p tcp -m tcp --dport <...> -j ACCEPT
sudo iptables -I OUTPUT -p tcp -m tcp --dport <..> -j ACCEPT
sudo service iptables save
NOTE: Do not run below rule, it may lock you out in VM.
While I am adding rule to block all ports in OUTPUT
>> sudo iptables -A OUTPUT -j REJECT
,VM is getting locked immidiately and lost access to VM.
After restarting VM from cloud console, I was able to regain access as aboveiptable
rule was not saved.
I tried >> sudo iptables -A OUTPUT -j Drop
, after some time, but it locked VM again and need to restart it to gain access to VM.
Let me know how to block all OUTBOUND ports like INPUT Chain,without getting locked out.
Current Iptables config as below.
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT tcp -- anywhere anywhere tcp dpt:<....>
2 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
3 ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
4 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT tcp -- anywhere anywhere tcp dpt:<>
2 ACCEPT tcp -- anywhere anywhere tcp dpt:<>
3 ACCEPT tcp -- anywhere anywhere tcp dpt:<>
4 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh