Security
Difficulty: Advanced
4 min read

Portsentry: Port Scan Detection

Tutorial to install and configure Portsentry, a tool that detects port scans and blocks attackers' IP addresses.

Back to tutorials
What is Portsentry?
Portsentry is a proactive defense tool that acts as a "honeypot". It listens on TCP and UDP ports that you do not use. If an attacker attempts to connect to one of these ports (which is typical of a reconnaissance scan), Portsentry detects it immediately and can block the attacker's IP address at the firewall level.

Why use Portsentry?

  • Early detection: A port scan is often the very first step of an attack. Detecting it lets you block an attacker before they even find a real vulnerability.
  • Deterrence: Automatically blocking scanners reduces the "noise" in the logs and discourages automated attacks.
  • Lightweight: Consumes very few resources.

Prerequisites

  • A Linux server (Ubuntu/Debian, CentOS/RHEL).
  • Root access or sudo privileges.
  • A firewall such as iptables or firewalld.

Premium Content

This advanced tutorial is reserved for premium members.

9,90€ / month
  • All advanced tutorials
  • New content every week
  • Progress tracking
  • Cancel anytime

Written by

Morgann Riu

Cybersecurity and Linux administration expert. I share my knowledge through free tutorials and training to help system administrators and developers secure their infrastructures.

Frequently asked questions

Portsentry logs the scan but the IP is never blocked, why?
Two causes cover almost every case. Either BLOCK_TCP and BLOCK_UDP are still set to 0, which is detection-only mode: the event is logged and nothing else happens. Or KILL_ROUTE is still commented out, or does not match your firewall — an iptables command on a system that only runs firewalld will fail silently. Uncomment the right line, set both BLOCK_* values to 2, then restart the service.
What is the difference between BLOCK_TCP="1" and BLOCK_TCP="2"?
Value 1 adds the IP to /etc/hosts.deny. That file is only consulted by services built against TCP wrappers, which has become rare, so the protection is very partial. Value 2 runs the KILL_ROUTE command and inserts a real firewall rule, blocking all traffic from that IP whatever service it targets. On a modern system, 2 is the only sensible choice.
I blocked a legitimate IP by mistake, how do I unblock it?
The rule sits at the top of the INPUT chain, so just delete it: sudo iptables -D INPUT -s BLOCKED_IP -j DROP. Then confirm with sudo iptables -L INPUT -n that it is gone. Under firewalld, the --timeout=600 variant of the rich rule avoids the problem entirely since the block expires by itself after the delay.
Should I enable the advanced atcp and audp modes?
They change the picture: in standard mode Portsentry only listens on unused ports below 1024, so a scanner sweeping the high ports goes unnoticed. With TCP_MODE="atcp" and UDP_MODE="audp" in /etc/default/portsentry, it listens on every unassigned port and catches far more scanners. The trade-off is a higher risk of false positives, so have your unblocking procedure ready before enabling them.
How do I test Portsentry without locking myself out?
The test is to run nmap -p 1-1024 YOUR_SERVER_IP, but it must come from a machine whose IP you can afford to lose — definitely not your admin workstation, and not the IP your SSH session is coming from. During the scan, follow sudo tail -f /var/log/syslog (or /var/log/messages on CentOS): you will see the alert and then the addition to the block list, confirmed by sudo iptables -L INPUT -n.

Share this tutorial

Did you enjoy this article?

Comments

Checklist Sécurité Linux

30 points essentiels pour sécuriser un serveur Linux. Recevez aussi les nouveaux tutoriels par email.

Pas de spam. Désabonnement en 1 clic.