Linux – Check if iptables rules are working

firewalliptableslinuxnetcat

I have three virtualboxs.

1) Virtual machine VM-A that works as a router with two interfaces:

eth0 – 10.160.10.254

eth1 – 172.10.0.254

2) Virtual machine VM-B that works as an internal network with one interface:

eth0 – 10.160.10.1 (and with gw to 10.160.10.254)

3) Virtual machine VM-C that works as an external network with one interface:

eth0 172.10.0.1 (and with gw to 172.10.0.254)

I want to allow ssh connections to the router(VM-1) when originated for a server in the internal network with iptables.

So in the router vbox Im using this two commands below:

iptables -A INPUT -s 10.160.10.4 -d 10.160.10.254 -p udp --dport 22 -j ACCEPT

iptables -A INPUT -s 10.160.10.4 -d 10.160.10.254 -p tcp--dport 22 -j ACCEPT

To test if this is working Im trying to use netcat.

In the internal network machine Im using nc -lu 22 command and in the external network machine Im using nc -u 193.160.10.4 22 command, but nothing is appearing.

Do you know what needs to appear and how to use netcat corretcly to test the iptables rules?

Best Answer

SSH uses TCP, not UDP. You use nc -u so you send UDP packets. Just try

nc -vz <ip> <port>

If you want to test your iptables rules that way, you should set the policy for the INPUT chain to DROP or REJECT. Take care that you allow tcp packets to port 22 from your source before. You can allow it from the IP of the specific machine, the whole subnet or the interface.

Example source ip:

iptables -A INPUT -s <source ip> -p tcp --dport 22 -j ACCEPT

Example source subnet (Accepts everything from 192.168.xxx.xxx):

iptables -A INPUT -s 192.168.0.0/16 -p tcp --dport 22 -j ACCEPT

Example source interface (accepts every packet comming through interface eth0):

iptables -A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT

Set policy for INPUT to DROP (the default action if none of the rules applies):

iptables -P INPUT DROP

Best regards

EDIT: And of course what David said, but im presuming some typos in the question, otherwise there wont be any working connection.

Related Topic