centos7的默认防火墙firewall与ipstable
1、默认防火墙firewall
CentOS 7查看所有已开放端口命令:
1 | firewall-cmd --zone=public --list-ports |
查看所有开启的端口号。
1 | netstat -aptn |
查看系统中所有使用 udp协议 的端口号。
1 | netstat -nupl |
查看系统中使用 tcp协议 的端口号信息。
1 | netstat -ntpl |
开放centos7防火墙的端口:
-添加开放端口号
1 | firewall-cmd --zone=public --add-port=5672/tcp --permanent # 开放5672端口 |
-重启firewall
1 | firewall-cmd —reload |
-检查是否生效
1 | firewall-cmd —zone=public —query-port=8080/tcp |
-停止firewall
1 | systemctl stop firewalld.service |
-禁止firewall开机启动
1 | systemctl disable firewalld.service |
命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效( —permanent放在前面与后面都行)
2、防火墙ipstable
(1)Linux防火墙(Iptables)重启系统生效
开启:
1 | [root@S1 core]# chkconfig iptables on |
关闭:
1 | [root@S1 core]# chkconfig iptables off |
(2)Linux防火墙(Iptables) 即时生效,重启后失效
开启:
1 | [root@S1 core]# service iptables start |
关闭:
1 | [root@S1 core]# service iptables stop |
(3)查看iptables规则
1 | [root@S1 core]# iptables -L -n |
(4)开放1333端口(tcp)
1 | iptables -A INPUT -p tcp --dport 1333 -j ACCEPT |
(5)例子
1 | iptables -I INPUT 6 -m state --state NEW -p tcp --dport 80 -j ACCEPT |
添加此规则至第6行规则(规则先后顺序有优先性)
1 | 问题: |
补充:
CentOS7出现
1 | Unit iptables.service could not be found |
CentOS7默认的防火墙不是iptables,而是firewalle.
出现此情况可能是iptables防火墙未安装。
#停止firewalld服务
1 | systemctl stop firewalld |
#禁用firewalld服务
1 | systemctl mask firewalld |
#-开启-非步骤,仅备注
1 | systemctl unmask firewalld |
安装iptables-services:
1 | yum install iptables-services |
设置开机启动:
1 | systemctl enable iptables |
然后
1 | service iptables status |