Logo Questions Linux Laravel Mysql Ubuntu Git Menu

iptables block access to all ports except from a partial IP address [closed]

I'm looking to block all ips from my server and it's ports with the exception of a partial ip 198.55..*. I'd like to limit access to an ISP region do to the fact my personal ip isn't static with our ISP. How would I go about this? Our server was compromised this evening and I'm trying to kill all other traffic to it.

The code below seems to allow a specific ip, but does it block every other IP? Also if I use the x will that act like a wildcard?

iptables -A INPUT -s 74.231.64.xx -j ACCEPT
like image 847
Code Junkie Avatar asked Feb 13 '23 18:02

Code Junkie

1 Answers

If you need to block all incomming traffic except an specific range, you should first change the default policy of the INPUT chain to DROP:

iptables --policy INPUT DROP

Then, you should give a netmask to iptables to allow many IP addresses altogether exceptionally. For example, if you need to only allow,, to, you can use following command:

iptables -A INPUT -s -j ACCEPT tells to iptables to apply the same role to all varying IPs between to Similarly, you can widen this range by passing or instead.

IMPORTANT NOTE: Before applying this change, you better have a direct access to the system, not an over-network access. This is because a miss type may block you from the server.

like image 96
lashgar Avatar answered Apr 06 '23 12:04