How to block certain IP address to access certain domain/IP in Linux ?

I have CentOS installed on my server for virtual server purposes. I have a few virtual servers clients each one with his own IP. I need to block their IP to access certain domain.

I know this iptables rules totally blocks access to certain IP but I only need to limit the access only to a few clients, so this one block total access :

iptables -A OUTPUT -d -j DROP

I need one rule that will mention what IP would be blocked from access

  • 2
    Are you looking also to restrict the source address affected? Have you tried iptables -A OUTPUT -s -d -j DROP? – MadHatter Nov 23 '15 at 7:45
  • I need to block IP 123.123.123 to access, actually I need to block some IP from my network to access certain website. – Blazer Nov 23 '15 at 7:53
  • 1
    So specify the required source address/block to be DROPped with -s, the required destination address/block with -d. Blocking destination domains can't be done reliably in iptables, as a very small amount of searching on SF would have told you (eg, this one or this one ). – MadHatter Nov 23 '15 at 8:20
  • If you need to prevent access to some website you can also use some access rule in the webserver configuration. E.g.: for apache 2.2 you can use mod_authz rules: order allow,deny and allow from all deny from Other webserver has similar ways to block ip from accessing a website without having to fiddle with low-level tool. The advantage is that you can prevent access to a single website on a shared host. – SimoneLazzaris Nov 23 '15 at 9:31

Use the -s | --source address parameter to filter on the source address.

iptables -I OUTPUT -s a.b.c.d  -d w.x.y.z -j DROP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.