Linux系统iptables与firewalld防火墙

iptables

  iptables服务用于处理或过滤流量的策略条目(规则),多条规则可以组成一个规则链,而规则链则依据数据包处理位置的不同进行分类。

      在进行路由选择前处理数据包(PREROUTING);

      处理流入的数据包(INPUT);

      处理流出的数据包(OUTPUT);

      处理转发的数据包(FORWORD);

      在进行路由选择后处理数据包(POSTROUTING)。

  一般来说,从内网向外网发送的流量一般都是可控且良性的,因此使用最多的就是INPUT规则链,该规则链可以增大黑客人员从外网入侵内网的难度。

iptables中的基本参数

iptables中常用的参数以及作用

参数作用
-P设置默认策略
-F清空规则链
-L查看规则链
-A在规则链的末尾加入新规则
-I num在规则链的头部加入新规则
-D num删除某一条规则
-s匹配来源地址IP/MASK,加叹号“!”表示这个IP除外
-d匹配目标地址
-i 网卡名称匹配从这块网卡流入的数据
-o 网卡名称匹配从这块网卡流出的数据
-p匹配协议,如TCP、UDP、ICMP
--dport num匹配目标端口号
--sport num匹配来源端口号

  在iptables命令后添加-L参数查看已有的防火墙规则链

[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination

 target prot opt source destination
 ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
 ACCEPT all -- anywhere anywhere
 INPUT_direct all -- anywhere anywhere
 INPUT_ZONES_SOURCE all -- anywhere anywhere
 INPUT_ZONES all -- anywhere anywhere
 ACCEPT icmp -- anywhere anywhere
 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

 

  在iptables命令后添加-F参数清空已有的防火墙规则链

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

  把INPUT规则链的默认策略设置为拒绝:

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

   将INPUT规则链设置为只允许指定网段的主机访问本机的22端口,拒绝来自其他所有的主机流量:

[root@localhost ~]# iptables -I INPUT -s 10.6.12.0/24 -p tcp --dport 22 -j ACCEPT
[root@localhost ~]# iptables -A INPUT -p tcp --dport 22 -j REJECT
[root@localhost ~]# iptables -L

Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 10.6.12.0/24 anywhere tcp dpt:ssh
ACCEPT icmp -- anywhere anywhere
REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable

  防火墙策略规则是按照从上到下的顺序匹配的,因此一定要把允许动作放在拒绝动作前面。否则所有的流量就将被拒绝掉

使用CRT

一个是来自192.168.72.0/24的主机访问,会被拒绝

Connection timed out

来自10.6.72.0/24的主机访问

Last login: Sat Aug 31 11:43:09 2019 from 10.6.12.47
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 

 

转载于:https://www.cnblogs.com/DevonL/p/11438414.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值