在Linux系統中,配置IP白名單通常涉及使用iptables或firewalld等工具。以下是使用這兩種工具配置IP白名單的基本步驟:
清空現有規則(可選,但推薦):
sudo iptables -F
設置默認策略(可選):
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
允許本地回環接口的流量:
sudo iptables -A INPUT -i lo -j ACCEPT
允許已建立的連接:
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
添加IP白名單規則:
假設你想允許IP地址為192.168.1.100
的設備訪問:
sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT
保存規則(根據發行版不同,命令可能有所不同):
sudo sh -c "iptables-save > /etc/iptables/rules.v4"
sudo service iptables save
重啟iptables服務(如果需要):
sudo systemctl restart iptables
啟用firewalld服務(如果尚未啟用):
sudo systemctl start firewalld
sudo systemctl enable firewalld
添加IP白名單規則:
假設你想允許IP地址為192.168.1.100
的設備訪問:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" accept'
重新加載firewalld配置:
sudo firewall-cmd --reload
無論使用哪種工具,都可以通過以下命令查看當前的防火墻規則:
iptables:
sudo iptables -L -v -n
firewalld:
sudo firewall-cmd --list-all
通過這些步驟,你可以成功配置Linux系統的IP白名單,確保只有指定的IP地址可以訪問你的系統。