在Linux Centos 6与Centos 7中该如何关闭或开启防火墙呢?因为Linux防火墙很重要,比如我们的web网站服务器或SVN如果要让外部直接访问,就得关闭防火墙,否则网页就会出错,无法访问。
在Centos 6服务器中关闭或开启防火墙的方法,使用service命令,分别如下:
查询防火墙状态 : [root@localhost ~]# service iptables status
停止防火墙 : [root@localhost ~]# service iptables stop
启动防火墙 : [root@localhost ~]# service iptables start
重启防火墙 : [root@localhost ~]# service iptables restart
永久关闭防火墙 : [root@localhost ~]# chkconfig iptables off
永久关闭后启用 : [root@localhost ~]# chkconfig iptables on
那么在Centos 6中如何只开启某个端口的防火墙呢?以开启8080端口为例,请运行如下命令:
//开启8080端口防火墙
sudo iptables -I INPUT -p tcp -m tcp --dport 8080 -j ACCEPT
//保存设置
sudo service iptables save
注意:此设置并不是永久性的,Linux重启之后会恢复以前的设置。
在Centos7中关闭或开启防火墙的方法,使用systemctl命令,如下:
永久关闭防火墙:systemctl disable firewalld
开启防火墙:systemctl start firewalld
关闭防火墙:systemctl stop firewalld
重启防火墙:systemctl reload firewalld
查询防火墙状态:systemctl status firewalld
那怎么开启一个端口呢
添加
firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效)
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=80/tcp
删除
firewall-cmd --zone= public --remove-port=80/tcp --permanent
注意:在开启某个端口的防火墙时,如果没有“--permanent”标识,则标识它不是永久开启的,在服务器重启之后会恢复之前的设置。