On Centos 6 machine, how can I block port 8888 from being accessed except by localhost/ only using iptables? Actually, I have another port that I want to listen on 8000 instead of 8888.

This command blocks access to localhost:8888

iptables -I INPUT -p tcp --dport 8888 -j DROP

I don't want access to but only to localhost:8888


If you mean by any other IP different than localhost, you can use the following (if I understood you well):

iptables -I OUTPUT -p tcp --dport 8888 -j DROP
iptables -I OUTPUT -s -p tcp --dport 8888 -j ACCEPT

The first drop all outgoing traffic to TCP/8888 and the second rule accept it when it originates from localhost.

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.