I run CentOS on my server, and I often find that my server is being attacked by other computers. Brute force SSH attacks, port scanning, viruses scanning for the ability to spread, things like that. In this article, I’ll show you how to block an IP address on Linux server using IPTables.
The First, I’ll assume you are already using iptables. If you need help setting that up, read this article.
How do I block an IP address ?
Example I want to block incoming request from IP 220.127.116.11, login as root and type the following command
# iptables -I INPUT -s 18.104.22.168 -j DROP
– I: Inserts the chain at the top of the rules.
– s: Match source IP address.
– j: Jump to the specified target chain when the packet matches the current rule.
To drop packets coming in on interface eth0 from 22.214.171.124, type the following command
# iptables -I INPUT -i eth0 -s 126.96.36.199 -j DROP
How do I block a subnet ?
Use the following syntax to block 10.0.0.0/8
# iptables -I INPUT -s 10.0.0.0/8 -j DROP
How do I save blocked IP address ?
To save blocked IP address to iptables config file, type the following command
# service iptables save
# /etc/init.d/iptables save
How Do I Unblock An IP Address?
First, you need to display blocked IP address along with line number and other information, type the following command
# iptables -L INPUT -n --line-numbers # iptables -L INPUT -n --line-numbers | grep 188.8.131.52
Chain INPUT (policy DROP) num target prot opt source destination 1 DROP all -- 184.108.40.206 0.0.0.0/0 2 LOCALINPUT all -- 0.0.0.0/0 0.0.0.0/0 3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 4 ACCEPT udp -- 220.127.116.11 0.0.0.0/0 udp spts:1024:65535 dpt:53
To unblock 18.104.22.168 you must delete line number 1, enter:
# iptables -D INPUT 1
- How To Save/Restore Iptables Rules
- Linux Shutdown Command
- Repel port flood by CSF and IPT_Recent
- What is the role of this variables in php.ini file (expose_php – allow_url_fopen – register_globals) ?
- How To Disable Ctrl + Alt + Del On Ubuntu 11
- How To Disable Ctrl + Alt + Del On Centos / RedHat
- How To Display All Cron Jobs On Linux
- 10 lsof Command Examples
- How To Install Ksplice on CentOS / RedHat
- How To Update Linux Kernel With Ksplice Uptrack