How to Block an IP Address on Linux Server

If you are using Linux server, you may experienced all kinds of attacks from many IP addresses. These attacks can be blocked with firewall.

This tutorial will use iptables firewall since it is the most common firewall on servers and it is very simple to do basic operations with it.


Log in to your server as root user, or use sudo if you have sudo privileges.

How to Block an IP Address

Replace <IP ADDRESS> with actual IP address you want to block.

It will look like this

This command will block access to all ports on your server.

If you want to block access to specific port, you need the command below:

Replace <IP ADDRESS> with actual IP address you want to block.

It will look like this:

This command will drop all packets coming from IP address 123.123.123.123 on SSH port (port 22).

You can block whatever port you want, only change port number in command above.

How to Unblock an IP Address

To unblock IP address, you need to delete firewall rule for that IP address.

You can do that by typing:

Replace <IP ADDRESS> with actual IP address, so it will look like this:

To save new rules, run this:

List Firewall Rules

To see rules list of your firewall, type this:

This command will list all existing rules of your firewall.


That’s it, you have now learned some of the basic actions to do with the iptables firewall that may help you manage your server security.

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *