在CentOS系統中,你可以使用firewalld
來限制訪問特定IP。以下是具體步驟:
啟動firewalld服務(如果尚未啟動):
sudo systemctl start firewalld
啟用firewalld服務(如果尚未啟用):
sudo systemctl enable firewalld
添加富規則以限制特定IP訪問:
假設你想阻止IP地址192.168.1.100
訪問你的服務器上的某個端口(例如80端口),可以使用以下命令:
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="80" reject'
解釋:
--permanent
:使規則永久生效。--zone=public
:指定防火墻區域為public。--add-rich-rule
:添加富規則。rule family="ipv4"
:指定規則適用于IPv4。source address="192.168.1.100"
:指定源IP地址。port protocol="tcp" port="80"
:指定端口和協議。reject
:拒絕訪問。重新加載firewalld配置以應用新規則:
sudo firewall-cmd --reload
如果你更喜歡使用iptables
,可以按照以下步驟操作:
阻止特定IP訪問某個端口:
假設你想阻止IP地址192.168.1.100
訪問你的服務器上的80端口,可以使用以下命令:
sudo iptables -A INPUT -s 192.168.1.100 -p tcp --dport 80 -j DROP
解釋:
-A INPUT
:將規則添加到INPUT鏈。-s 192.168.1.100
:指定源IP地址。-p tcp
:指定協議為TCP。--dport 80
:指定目標端口為80。-j DROP
:丟棄匹配的數據包。保存iptables規則(CentOS 7及以上版本):
sudo systemctl stop firewalld
sudo iptables-save > /etc/sysconfig/iptables
sudo systemctl start firewalld
解釋:
iptables-save > /etc/sysconfig/iptables
:將當前的iptables規則保存到文件中。systemctl stop firewalld
和systemctl start firewalld
:停止并重新啟動firewalld服務以應用iptables規則。通過以上兩種方法,你可以有效地限制特定IP地址訪問你的CentOS服務器上的資源。選擇哪種方法取決于你的個人偏好和系統配置。