Iptables to reject all INPUT and OUTPUT except specific hosts


Transfered from Linux Config Disqus comments:


I have created a zone in my lan, where i have given 11 servers a dns address. But someone has connected 3 more(these three have only IP, not dns address), and i dont want to remove all servers to find out which ones it is.
Is there a way to make sure that only the servers with a dns-address provided by me is granted access to the internet? (block input and output)
Reject all outgoing traffic from source IP address different than

iptables -A OUTPUT -t filter ! -s -j REJECT

Reject all incoming traffic to destination address different than

iptables -A INPUT -t filter ! -d -j REJECT

Note, instead of an IP address you can supply hostname such as pc1 or pc2 however, when applying this rule, iptables will automatically resolve this into an IP address and IP address will by used instead.

Furthermore, you can supply a range of IP addresses in a single IP tables rule. However, I do not know whether your hosts are arranged in some systematic way that you can apply this rule in your scenario.

opened #2