CentOS7默認的防火牆不是iptables,而是firewalle.mysql
使用方法以下:sql
>>> 關閉防火牆tcp
systemctl stop firewalld.service #中止firewall
systemctl disable firewalld.service #禁止firewall開機啓動接口
>>> 開啓端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含義:
--zone #做用域
--add-port=80/tcp #添加端口,格式爲:端口/通信協議
--permanent #永久生效,沒有此參數重啓後失效
>>> 重啓防火牆
firewall-cmd --reloadip
經常使用命令介紹
firewall-cmd --state ##查看防火牆狀態,是不是running
firewall-cmd --reload ##從新載入配置,好比添加規則以後,須要執行此命令
firewall-cmd --get-zones ##列出支持的zone
firewall-cmd --get-services ##列出支持的服務,在列表中的服務是放行的
firewall-cmd --query-service ftp ##查看ftp服務是否支持,返回yes或者no
firewall-cmd --add-service=ftp ##臨時開放ftp服務
firewall-cmd --add-service=ftp --permanent ##永久開放ftp服務
firewall-cmd --remove-service=ftp --permanent ##永久移除ftp服務
firewall-cmd --add-port=80/tcp --permanent ##永久添加80端口
iptables -L -n ##查看規則,這個命令是和iptables的相同的
man firewall-cmd ##查看幫助作用域
更多命令,使用 firewall-cmd --help 查看幫助文件rem
>>> CentOS 7.0默認使用的是firewall做爲防火牆,使用iptables必須從新設置一下
一、直接關閉防火牆
systemctl stop firewalld.service #中止firewall
systemctl disable firewalld.service #禁止firewall開機啓動get
二、安裝iptable iptable-servicecmd
#先檢查是否安裝了iptables
service iptables status
#安裝iptables
yum install -y iptables
#升級iptables(安裝的最新版本則不須要)
yum update iptables
#安裝iptables-services
yum install iptables-servicestable
三、禁用/中止自帶的firewalld服務
#中止firewalld服務 systemctl stop firewalld #禁用firewalld服務 systemctl mask firewalld
四、設置現有規則
#查看iptables現有規則 iptables -L -n #先容許全部,否則有可能會杯具 iptables -P INPUT ACCEPT #清空全部默認規則 iptables -F #清空全部自定義規則 iptables -X #全部計數器歸0 iptables -Z #容許來自於lo接口的數據包(本地訪問) iptables -A INPUT -i lo -j ACCEPT #開放22端口 iptables -A INPUT -p tcp --dport 22 -j ACCEPT #開放21端口(FTP) iptables -A INPUT -p tcp --dport 21 -j ACCEPT #開放80端口(HTTP) iptables -A INPUT -p tcp --dport 80 -j ACCEPT #開放443端口(HTTPS) iptables -A INPUT -p tcp --dport 443 -j ACCEPT #容許ping iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT #容許接受本機請求以後的返回數據 RELATED,是爲FTP設置的 iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #其餘入站一概丟棄 iptables -P INPUT DROP #全部出站一概綠燈 iptables -P OUTPUT ACCEPT #全部轉發一概丟棄 iptables -P FORWARD DROP
五、其餘規則設定
#若是要添加內網ip信任(接受其全部TCP請求) iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT #過濾全部非以上規則的請求 iptables -P INPUT DROP #要封停一個IP,使用下面這條命令: iptables -I INPUT -s ***.***.***.*** -j DROP #要解封一個IP,使用下面這條命令: iptables -D INPUT -s ***.***.***.*** -j DROP
六、保存規則設定
#保存上述規則 service iptables save
七、開啓iptables服務
#註冊iptables服務 #至關於之前的chkconfig iptables on systemctl enable iptables.service #開啓服務 systemctl start iptables.service #查看狀態 systemctl status iptables.service
八、映射端口(如將mysql默認的3306端口映射成1306對外提供服務)
iptables -t mangle -I PREROUTING -p tcp --dport 1306 -j MARK --set-mark 883306 iptables -t nat -I PREROUTING -p tcp --dport 1306 -j REDIRECT --to-ports 3306 iptables -I INPUT -p tcp --dport 3306 -m mark --mark 883306 -j ACCEPT