CentOS

[2019-03-31]CentOS7-防火墙开放端口

2019-03-31  本文已影响2人  刘浩_d94d

第一步:先检查是否安装了iptables

[root@localhost ~]# service iptables status

结果一:需要安装iptable iptable-service (执行1.1、1.2、1.3命令)

image.png
结果二:不需要安装iptable iptable-service image.png

1.1 安装iptable

[root@localhost ~]# yum install -y iptables

1.2 升级iptables

[root@localhost ~]# yum update -y iptables

1.3 安装iptable-service

[root@localhost ~]# yum install -y iptables-services

第二步: 停止/禁用 自带的firewalld服务

2.1 停止firewalld服务

[root@localhost ~]# systemctl stop firewalld

2.2 禁用firewalld服务

[root@localhost ~]# systemctl mask firewalld

第三步: 设置现有规则

3.1 查看iptables现有规则

[root@localhost ~]# iptables -L -n

3.2 先允许所有(不然有可能会杯具)

[root@localhost ~]# iptables -P INPUT ACCEPT

3.3 清空所有默认规则

[root@localhost ~]# iptables -F

3.3 清空所有自定义规则

[root@localhost ~]# iptables -X

3.4 所有计数器归0

[root@localhost ~]# iptables -Z

3.5 允许来自于lo接口的数据包(本地访问)

[root@localhost ~]# iptables -A INPUT -i lo -j ACCEPT

3.6 开放22端口

[root@localhost ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT

3.7 允许ping,开放8端口

[root@localhost ~]# iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT

第四步: 按照实际要求,添加规则

4.1 允许接受本机请求之后的返回数据 RELATED,是为FTP设置的

[root@localhost ~]# iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

4.2 其他入站一律丢弃

[root@localhost ~]# iptables -P INPUT DROP

4.3 所有出站一律绿灯

[root@localhost ~]# iptables -P OUTPUT ACCEPT

4.4 所有转发一律丢弃

[root@localhost ~]# iptables -P FORWARD DROP

第五步:其他规则设定

5.1 如果要添加内网ip信任(接受其所有TCP请求)

[root@localhost ~]# iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT

5.2 过滤所有非以上规则的请求

[root@localhost ~]# iptables -P INPUT DROP

5.3 要封停一个IP,使用下面这条命令:

[root@localhost ~]# iptables -I INPUT -s ***.***.***.*** -j DROP

5.4 要解封一个IP,使用下面这条命令:

[root@localhost ~]# iptables -D INPUT -s ***.***.***.*** -j DROP

第六步 保存规则设定

[root@localhost ~]# service iptables save

第七步 开启iptables服务

7.1 注册iptables服务

相当于以前的chkconfig iptables on

[root@localhost ~]# systemctl enable iptables.service

7.2 开启服务

[root@localhost ~]# systemctl start iptables.service

7.3 查看状态

[root@localhost ~]# systemctl status iptables.service

查看

[root@localhost ~]# vim /etc/sysconfig/iptables

上一篇 下一篇

猜你喜欢

热点阅读