Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to add some rules to my iptables to prevent port scanning, how can I do this?
I find some solution but it's not efficient.

Port canning solution

share|improve this question

The best bet is having a default drop policy in iptables and then only allowing what's required. Something like:

# Drop all packets by default.
iptables -P INPUT DROP
# Allow preexisting connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow SSH from
iptables -A INPUT -p tcp -s --destination-port 22 -i eth0 -j ACCEPT
# Allow HTTP from all
iptables -A INPUT -p tcp --destination-port 80 -i eth0 -j ACCEPT

It won't stop people from doing portscans, but it will mean that all they'll see is port 80 open.

share|improve this answer
it didn't that do what I want :(, your solution force me to open specific ports – Amirreza Mar 12 '11 at 5:24
There's no way to block port scans specifically without blocking legitimate traffic. All you can do is make sure that as little as possible is open to the public and that it's well secured. The above script does a default "DROP" rather than "REJECT". A "REJECT" would tell the port scanner that the port is closed straight away. The DROP wastes their time by making the scanner have to waste a certain amount of time before moving onto another port. – Niall Donegan Mar 12 '11 at 12:05

For nmap port scanning, you can check the following answer: iptables Tips & Tricks

I'm not familiar with Hping, but if Hping uses NULLflags, the answer I've linked above should also work.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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