Search code examples
sshiptablescentos-6.9

How to block all ports except ssh


I have changed ssh default port to 2020, And add iptable rule in order to allow incoming traffic on that port using below command.

iptables -A INPUT -p tcp -m tcp --dport 2020 -j ACCEPT

And i would like to block all other ports on the server. And use below command after allowing ssh. All session are closed. How can i fix it.

iptables -P INPUT DROP

iptables -P OUTPUT DROP


Solution

  • You may need to enable OUTPUT

        iptables -P INPUT ACCEPT
        iptables -P OUTPUT ACCEPT
        iptables -F
        iptables -A INPUT -i lo -j ACCEPT
        iptables -A INPUT -p tcp -m tcp --dport 2020 -j ACCEPT
        iptables -A OUTPUT -p tcp -m tcp --sport 2020 -j ACCEPT
        iptables -P INPUT DROP
        iptables -P OUTPUT DROP