CentOS 7防火牆的設置以及端口的開啓

一、配置防火牆,開啓80端口、3306端口

CentOS 7 默認使用firewalld來管理iptables規則,由於防火牆規則變動的情況很少,動不動態變得無所謂了。但是習慣是魔鬼,跟之前不一樣,總是感覺不太習慣。

systemctl disable firewalld
yum remove firewalld -y

使用下面的辦法來恢復原來的習慣,同時解決iptables開機啓動的問題。

yum install iptables-services -y
systemctl enable iptables

這樣的話,iptables服務會開機啓動,自動從/etc/sysconfig/iptables 文件導入規則。

爲了讓/etc/init.d/iptables save 這條命令生效,需要這麼做

cp /usr/libexec/iptables/iptables.init /etc/init.d/iptables
/etc/init.d/iptables save

而chkconfig iptables 命令會自動重定向到sytemctl enable iptables

vi /etc/sysconfig/iptables #編輯防火牆配置文件
Firewall configuration written by system-config-firewall Linux學習,http:// linux.it.NET.cn
Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0] Linux學習,http:// linux.it.Net.cn
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT
-A INPUT -j REJECT –reject-with icmp-host-prohibited
-A FORWARD -j REJECT –reject-with icmp-host-prohibited IT網,http://www.it.net.cn
COMMIT IT網,http://www.it.net.cn
:wq! #保存退出
systemctl restart iptables.service #最後重啓防火牆使配置生效
systemctl enable iptables.service #設置防火牆開機啓動

二、關閉SELINUX
vi /etc/selinux/config
SELINUX=enforcing #註釋掉
SELINUXTYPE=targeted #註釋掉 IT網,http://www.it.net.cn
SELINUX=disabled #增加 Linux學習,http:// linux.it.net.cn
:wq! #保存退出
setenforce 0 #使配置立即生效

打開3306端口

/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
/etc/rc.d/init.d/iptables save      #修改生效
/etc/init.d/iptables status         #查看配置
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章