iptables和SELinux是Linux系統中兩種不同的安全機制,它們可以相互配合使用,以提供更強大的安全保護。以下是它們如何配合使用的一些建議:
# 允許SSH連接
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 阻止所有來自特定IP的連接
iptables -A INPUT -s 192.168.1.100 -j DROP
# 保存規則(具體命令可能因發行版而異)
service iptables save
# 查看當前SELinux狀態
sestatus
# 設置SELinux為 enforcing 模式(強制執行策略)
setenforce 1
# 修改文件上下文以符合SELinux策略
chcon system_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html
# 編輯SELinux策略文件(通常位于 /etc/selinux/config)
SELINUX=enforcing
iptables -A INPUT -p tcp --dport 80 -s 192.168.1.100 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j DROP
# 假設Web服務器進程的PID是1234
ps -ef | grep httpd
chcon system_u:object_r:httpd_sys_content_t:s0 /var/www/html/index.html
semanage fcontext -a -t httpd_sys_content_t "/var/www/html(/.*)?"
restorecon -Rv /var/www/html
總之,通過合理地結合使用iptables和SELinux,你可以顯著提高Linux系統的安全性。