


How to use an IP blacklist to block malicious IP addresses from accessing your CentOS server
如何使用IP黑名单来阻止恶意IP地址访问CentOS服务器
在互联网上运营的服务器经常面临来自恶意IP地址的攻击,这些攻击可能导致服务器的性能下降甚至系统崩溃。为了保护服务器的安全性和稳定性,CentOS服务器提供了一种简单而有效的方式来阻止恶意IP地址的访问,即使用IP黑名单。
IP黑名单是一种名单,列出了被认为是威胁或恶意的IP地址。当服务器收到来自这些IP地址的请求时,服务器会拒绝响应并直接关闭连接。下面将介绍如何在CentOS服务器上配置和使用IP黑名单。
- 查看当前IP连接情况
在开始配置IP黑名单之前,我们需要先查看当前服务器上活动连接的IP地址。打开终端,运行以下命令:
netstat -an | grep :80 | awk '{ print $5 }' | cut -d: -f1 | sort | uniq -c | sort -n
这条命令会列出服务器上与端口80建立连接的IP地址及连接数量。请注意,这里假设服务器的服务端口是80,如果你的服务器使用的是其他端口,需要相应修改命令。
- 安装IP黑名单工具
CentOS服务器默认没有安装IP黑名单工具,我们需要先安装一个称为"fail2ban"的工具来实现IP黑名单的功能。执行以下命令进行安装:
sudo yum install epel-release sudo yum install fail2ban
- 配置fail2ban
安装完成后,我们需要配置fail2ban以使用IP黑名单。打开终端,运行以下命令:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local sudo nano /etc/fail2ban/jail.local
在打开的文件中,找到并修改以下行:
[DEFAULT] bantime = 3600 findtime = 600 maxretry = 3
这里的意思是,如果一个IP地址在600秒内尝试连接3次失败,则会被列入IP黑名单,并在之后的3600秒内禁止该IP地址访问服务器。
- 创建IP黑名单
接下来,我们需要创建一个文件来存储IP黑名单列表。运行以下命令:
sudo touch /etc/fail2ban/ip_blacklist.conf
然后,打开终端,运行以下命令编辑创建的文件:
sudo nano /etc/fail2ban/ip_blacklist.conf
在文件中,每行添加一个需要被列入IP黑名单的IP地址,例如:
192.168.0.100 123.456.789.0
保存并关闭文件。
- 配置fail2ban以使用IP黑名单
编辑fail2ban的主配置文件,运行以下命令:
sudo nano /etc/fail2ban/jail.local
在文件中,找到以下行并进行修改:
[DEFAULT] # 省略其他配置 # 将这一行修改为下面这行 bantime = 3600
然后,在同一文件中添加以下内容:
[ip-blacklist] enabled = true filter = apache-noscript logpath = /var/log/httpd/access.log banaction = iptables-multiport bantime = 3600 maxretry = 1 findtime = 600 action = iptables[name=IPBlacklist, port=80, protocol=tcp]
这里是将IP黑名单与fail2ban的其他功能(如阻止重复登录等)一并配置了。如果你只想使用IP黑名单功能,可以适当删除一些配置项。
- 重启fail2ban
完成所有配置后,最后一步是重启fail2ban使配置生效。运行以下命令:
sudo service fail2ban restart
- 验证IP黑名单是否生效
通过以下命令可以验证IP黑名单是否已经生效:
sudo iptables -L -n
如果你能看到输出中列出了之前添加的IP地址,说明IP黑名单已经生效。
总结
通过使用IP黑名单,我们可以有效地阻止恶意IP地址对CentOS服务器进行访问和攻击。使用fail2ban工具,我们可以配置IP黑名单并自动对恶意IP地址进行封禁。我通过本文简要介绍了如何在CentOS服务器上配置和使用IP黑名单,希望能对你的服务器安全提供帮助。
The above is the detailed content of How to use an IP blacklist to block malicious IP addresses from accessing your CentOS server. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use IP blacklists to prevent malicious IP addresses from accessing CentOS servers Servers operating on the Internet often face attacks from malicious IP addresses, and these attacks may cause server performance degradation or even system crashes. In order to protect the security and stability of the server, CentOS server provides a simple and effective way to block access from malicious IP addresses, that is, using an IP blacklist. An IP blacklist is a list of IP addresses that are considered threatening or malicious. When the server receives data from these IP

How to Protect CentOS Servers Using Network Intrusion Detection Systems (NIDS) Introduction: In modern network environments, server security is crucial. Attackers use a variety of means to try to break into our servers and steal sensitive data or compromise systems. To ensure server security, we can use a Network Intrusion Detection System (NIDS) for real-time monitoring and detection of potential attacks. This article will introduce how to configure and use NIDS on a CentOS server to protect the server. Step 1: Install and configure SN

How to protect data on CentOS servers using secure file system encryption In today’s digital age, data security has become even more important. Especially sensitive data stored on servers, if not properly protected, may be attacked by hackers, leading to serious consequences. In order to ensure data confidentiality and integrity, we can use file system encryption to protect data on the CentOS server. This article will explain how to use secure file system encryption to protect data on CentOS servers and

With the rapid development of the Internet, network security has become an increasingly important issue. Malicious attacks and phishing incidents occur from time to time, posing a great threat to websites and users. Therefore, it is crucial to establish an effective network security defense system. Nginx is a popular web server software that not only provides high-performance web services, but also plays the role of a reverse proxy. Nginx also provides rich modules to help administrators protect web servers and applications. One of the important features is the IP blacklist

How to Use Antivirus Software to Protect CentOS Servers from Malware In today’s digital age, server security is crucial. The intrusion of malware may lead to the leakage of personal information, system failure and even hacker attacks. To protect CentOS servers from these risks, we can use antivirus software to increase the security of the server. This article will introduce how to use antivirus software to protect CentOS servers, and attach some code examples for reference. Choosing the right antivirus software First, I

How to Protect CentOS Server from Unauthorized Access Using Intrusion Detection System (IDS) Introduction: As a server administrator, protecting the server from unauthorized access is a very important task. The Intrusion Detection System (IDS for short) can help us achieve this goal. This article will introduce how to install and configure Snort, a commonly used IDS tool, on a CentOS server to protect the server from unauthorized access. 1. An

How to use two-factor authentication to secure access to CentOS servers Summary: With the increase in network attacks, it is particularly important to secure access to servers. Two-factor authentication is a way to enhance server security. This article will introduce how to use two-factor authentication on CentOS servers to improve access security. Keywords: two-factor authentication, CentOS server, access security, code example 1. What is two-factor authentication? Two-factor authentication refers to the use of two or more different identities.

How to use key authentication to implement more secure SSH login on CentOS servers In server management, it is crucial to ensure system security. SSH (SecureShell) is an encrypted network protocol used for remote login to Linux and UNIX servers. To further strengthen the security of the server, we can use key authentication instead of password login. This article will introduce how to implement a more secure SSH login on a CentOS server and provide corresponding code examples. Step 1: Generate
