EN

Linux and networking

Identify and block an IP address

Spot a suspicious IP address in connections and logs, then block it with UFW, firewalld, nftables or iptables without cutting off your own SSH access.

9 min read, updated on 18 November 2026

On this page

Your values

Enter them once and the commands in this guide update.

This guide explains how to find the IP address behind unwanted traffic, block it with the firewall already in place on your server, check the result and undo the block. It covers UFW, firewalld, nftables and iptables on Debian, Ubuntu, Rocky Linux and AlmaLinux.

Prerequisites

  • SSH access with root or sudo privileges
  • Only one firewall tool active on the server

The addresses 203.0.113.45, 198.51.100.0/24 and 2001:db8::45 are reserved for documentation. Replace them with the ones you want to block.

Protect your access before blocking

1. Note your public IP address, as the server sees it. Run this command without sudo:

Terminal
echo "$SSH_CONNECTION" | awk '{print $1}'

Check that it is neither the address to block nor part of the targeted range.

2. Note the SSH port:

Terminal
sudo sshd -T | awk '$1 == "port" {print $2}'

3. Schedule an automatic rollback. If you lose access, the server will undo the change after 5 minutes. Example with UFW:

Terminal
sudo systemd-run --on-active=5min --timer-property=AccuracySec=1s --unit=secours-pare-feu /usr/sbin/ufw --force disable

Adapt the final command to your tool:

Tool Command to put at the end Effect
UFW /usr/sbin/ufw --force disable Disables the whole firewall
firewalld /usr/bin/firewall-cmd --reload Removes non-permanent rules
nftables /usr/sbin/nft delete table inet blocage Deletes the table used in this guide
iptables /usr/sbin/iptables -D INPUT -s 203.0.113.45 -j DROP Removes the specified rule

4. After blocking, open a new SSH connection from a second terminal. If it works, cancel the timer:

Terminal
sudo systemctl stop secours-pare-feu.timer

If the timer fired with UFW, the firewall is disabled. Check your rules, then re-enable it with sudo ufw enable.

Identify the address to block

Currently established connections

This command counts established TCP connections per remote address:

Terminal
ss -Htn state established | awk '{print $4}' | sed -E 's/:[0-9]+$//; s/^\[//; s/\]$//; s/^::ffff://' | sort | uniq -c | sort -rn | head -n 20

To look at a single service, for example port 443:

Terminal
ss -Htn state established '( sport = :443 )'

Failed SSH logins

On Debian and Ubuntu:

Terminal
sudo journalctl -u ssh --since "24 hours ago" --no-pager \
  | grep -E 'Failed password|Invalid user' \
  | grep -oP 'from \K[0-9a-fA-F:.]+(?= port [0-9]+( ssh2)?$)' \
  | sort | uniq -c | sort -rn | head -n 20

On Rocky Linux and AlmaLinux, replace -u ssh with -u sshd.

The count gives a rough idea: a single attempt can produce several lines. The filter reads the address at the end of the line, because an attacker chooses the username written to the log and can slip a fake address into it.

Web requests

Terminal
sudo awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -n 20

For Apache, use /var/log/apache2/access.log on Debian and Ubuntu, or /var/log/httpd/access_log on Rocky Linux and AlmaLinux. These commands assume the default log format.

Check before blocking

  • It is not your address, or that of a colleague, a monitoring tool or a backup system.
  • It is not one of the server's own addresses, which you can see with ip -br address.
  • An address can be shared by many customers of the same provider. Prefer a short block if you are in doubt.
  • The whois 203.0.113.45 command shows the organization responsible (whois package).

Choose the right tool

Use the firewall that is already active. Mixing several makes the rules hard to understand and can remove some of them at restart.

Terminal
sudo ufw status | head -n 1
systemctl is-active firewalld
sudo nft list ruleset | head -n 30
sudo iptables -S | head -n 30
Result Section to follow
Status: active UFW
firewalld is active firewalld
Neither UFW nor firewalld, or hand-written nftables rules nftables
iptables rules managed by your own scripts iptables

A command not found error only means that the tool is not installed.

Block with UFW

UFW applies the first rule that matches. A deny rule added at the end would be useless if an allow comes before it. So use prepend, which places it before the other rules of the same address type:

Terminal
sudo ufw prepend deny from 203.0.113.45

Other useful cases:

Terminal
sudo ufw prepend deny from 198.51.100.0/24
sudo ufw prepend deny from 2001:db8::45
sudo ufw prepend deny proto tcp from 203.0.113.45 to any port 443

Check the order of the rules:

Terminal
sudo ufw status numbered

To unblock, repeat the rule with delete in front of it:

Terminal
sudo ufw delete deny from 203.0.113.45
sudo ufw delete deny proto tcp from 203.0.113.45 to any port 443

Block with firewalld

The drop zone ignores all incoming traffic from the sources attached to it. Start with a temporary rule, which is lost at the next reload:

Terminal
sudo firewall-cmd --zone=drop --add-source=203.0.113.45

After your SSH test, make it permanent:

Terminal
sudo firewall-cmd --permanent --zone=drop --add-source=203.0.113.45
sudo firewall-cmd --reload
sudo firewall-cmd --zone=drop --list-sources

The same command accepts a range (198.51.100.0/24) or an IPv6 address (2001:db8::45).

To unblock:

Terminal
sudo firewall-cmd --permanent --zone=drop --remove-source=203.0.113.45
sudo firewall-cmd --reload

Temporary or port-specific block

A rich rule can expire on its own:

Terminal
sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="203.0.113.45" drop' --timeout=1h

It can also target a single port:

Terminal
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.45" port port="443" protocol="tcp" drop'
sudo firewall-cmd --reload
sudo firewall-cmd --list-rich-rules

Block a long list

Terminal
sudo firewall-cmd --permanent --new-ipset=liste_noire --type=hash:net
sudo firewall-cmd --permanent --ipset=liste_noire --add-entry=198.51.100.0/24
sudo firewall-cmd --permanent --zone=drop --add-source=ipset:liste_noire
sudo firewall-cmd --reload

Block with nftables

This method creates a dedicated table with one IPv4 set and one IPv6 set. It does not change your other tables. Install the nftables package if needed.

Terminal
sudo nft add table inet blocage
sudo nft add set inet blocage ipv4_bloquees '{ type ipv4_addr; flags interval; }'
sudo nft add set inet blocage ipv6_bloquees '{ type ipv6_addr; flags interval; }'
sudo nft add chain inet blocage entree '{ type filter hook input priority -10; policy accept; }'
sudo nft add rule inet blocage entree ip saddr @ipv4_bloquees counter drop
sudo nft add rule inet blocage entree ip6 saddr @ipv6_bloquees counter drop

Then add the addresses:

Terminal
sudo nft add element inet blocage ipv4_bloquees '{ 203.0.113.45, 198.51.100.0/24 }'
sudo nft add element inet blocage ipv6_bloquees '{ 2001:db8::45 }'

Check the result. The counters show how many packets have been dropped:

Terminal
sudo nft list table inet blocage

To unblock an address:

Terminal
sudo nft delete element inet blocage ipv4_bloquees '{ 203.0.113.45 }'

Keep the block after a restart

Terminal
sudo mkdir -p /etc/nftables.d
sudo sh -c 'nft list table inet blocage > /etc/nftables.d/blocage.nft'
grep -q 'nftables.d' /etc/nftables.conf || echo 'include "/etc/nftables.d/*.nft"' | sudo tee -a /etc/nftables.conf
sudo nft -c -f /etc/nftables.conf
sudo systemctl enable nftables

Run the nft list table command to the file again after each address you add or remove. On Rocky Linux and AlmaLinux, the file loaded is /etc/sysconfig/nftables.conf, but firewalld remains the recommended solution on these systems.

Block with iptables

Terminal
sudo iptables -I INPUT 1 -s 203.0.113.45 -j DROP
sudo ip6tables -I INPUT 1 -s 2001:db8::45 -j DROP
sudo iptables -L INPUT -n -v --line-numbers

To unblock:

Terminal
sudo iptables -D INPUT -s 203.0.113.45 -j DROP
sudo ip6tables -D INPUT -s 2001:db8::45 -j DROP

These rules disappear at restart. If iptables is your only firewall on Debian or Ubuntu, save them:

Terminal
sudo apt install iptables-persistent
sudo netfilter-persistent save

Do not use this save with UFW, firewalld or Docker, as it would also freeze their rules.

Close existing connections

A block does not have the same immediate effect with every tool:

Method in this guide Established connections New connections
UFW Continue Blocked
firewalld Continue Blocked
nftables Cut off Blocked
iptables Cut off Blocked

With UFW and firewalld, close the remaining connections:

Terminal
sudo ss -K dst 203.0.113.45

The command only closes what the kernel allows it to close. If the connection is still visible with ss, restart the service concerned. Never run this command on your own address.

Special cases

Docker containers

Ports published by Docker bypass UFW, because their traffic is redirected before the firewall rules. To block an address in front of your containers, add the rule to the chain Docker provides for this purpose, replacing eth0 with your external interface:

Terminal
sudo iptables -I DOCKER-USER -i eth0 -s 203.0.113.45 -j DROP

ip route show default shows your external interface. This rule is not kept after a restart.

Fail2ban

If Fail2ban already protects a service, you can go through it:

Terminal
sudo fail2ban-client status sshd
sudo fail2ban-client set sshd banip 203.0.113.45
sudo fail2ban-client set sshd unbanip 203.0.113.45

If your own address has been banned, sudo fail2ban-client unban YOUR_IP removes it from all jails.

Saturation attacks

A firewall installed on the server does not protect against an attack that saturates the network: the packets reach the machine before being dropped. HostMyServers instances include anti-DDoS protection. If the problem persists, contact support from your client area, stating the time, addresses and ports involved.

Verify the block

  1. Review the rule with your tool's check command.

  2. From a machine that is not blocked, check that your service still responds:

    Terminal
    nc -vz -w 5 SERVER_IP 22
  3. Watch the logs: the address should no longer create new lines. On Rocky Linux and AlmaLinux, use -u sshd.

    Terminal
    sudo journalctl -u ssh -f
  4. With nftables and iptables, watch the packet counters go up if the address keeps trying.

Troubleshooting

You lost SSH access

First wait for the automatic rollback if you scheduled one. Otherwise, open your instance in your client area, click Accéder à la console VNC (Access the VNC console), then NoVNC Console graphique (NoVNC graphical console). Log in with an account that has a password, then remove the offending rule:

Terminal
sudo ufw delete deny from YOUR_IP
sudo firewall-cmd --permanent --zone=drop --remove-source=YOUR_IP && sudo firewall-cmd --reload
sudo nft delete element inet blocage ipv4_bloquees '{ YOUR_IP }'
sudo iptables -D INPUT -s YOUR_IP -j DROP

Run only the line for your tool. If no password is available, follow Change the root password. If you use your instance's built-in firewall, also check that it allows your SSH port.

The address still gets through

  • With UFW, the rule must appear before the ALLOW rules in sudo ufw status numbered.
  • The connection may already have been established: see Close existing connections.
  • The service may be running in Docker: see Docker containers.
  • The visible address may be a proxy's, or the attacker may be using several addresses.
  • An IPv4 rule does not block the same machine over IPv6.

Common error messages

Message Cause Solution
ERROR: Invalid position '1' ufw insert 1 with an IPv6 address Use ufw prepend
Could not delete non-existent rule The rule is not written identically Check sudo ufw status numbered, then run sudo ufw delete NUMBER
ERROR: Bad source address Malformed address Correct the address
FirewallD is not running The service is stopped Check systemctl status firewalld, or use the tool that is actually active
Error: ZONE_CONFLICT The address already belongs to another zone Remove it from that zone first
Error: UNKNOWN_SOURCE The address is not attached to any zone Check with --list-sources
Can't specify timeout for permanent action. --timeout used with --permanent Remove one of the two options
Error: No such file or directory (nft) The table or set does not exist Create them first
Error: Could not resolve hostname: Address family for hostname not supported IPv6 address in an IPv4 set Use ipv6_bloquees
Error: interval overlaps with an existing one The address is already covered by a range Nothing needs to be added
Error: element does not exist The address is not in the set Check with nft list set
iptables: Bad rule (does a matching rule exist in that chain?). No identical rule to delete Check sudo iptables -S INPUT
host/network '2001:db8::45' not found IPv6 address passed to iptables Use ip6tables
iptables: No chain/target/match by that name. The DOCKER-USER chain does not exist Check that Docker is running with its iptables backend

References