iptables基本管理

1案例1:iptables基本管理

1.1问题

本案例要求练习iptables命令的使用,按照要求完成以下任务:

  1. 关闭firewalld,开启iptables服务
  2. 查看防火墙规则
  3. 追加、插入防火墙规则
  4. 删除、清空防火墙规则
1.2步骤

实现此案例需要按照如下步骤进行。

步骤一:关闭firewalld,启动iptables服务

1)关闭firewalld服务器

[root@proxy ~]# systemctl stop firewalld.service 
[root@proxy ~]# systemctl disable firewalld.service

2)安装iptables-service并启动服务

[root@proxy ~]# yum -y install iptables-services
[root@proxy ~]# systemctl start iptables.service

步骤二:熟悉iptables框架

1)iptables的4个表(区分大小写):
iptables默认有4个表,nat表(地址转换)、filter表(数据过滤表)、raw表(状态跟踪表)、mangle表(包标记表)。
2)iptables的5个链(区分大小写):
INPUT链(入站规则)
OUTPUT链(出站规则)
FORWARD链(转发规则)
PREROUTING链(路由前规则)
POSTROUTING(路由后规则)

步骤三:iptables命令的基本用法

1)iptables语法格式

[root@proxy ~]# iptables  [-t 表名]  选项  [链名]  [条件]  [-j 目标操作]
[root@proxy ~]# iptables  -t  filter  -I  INPUT -p  icmp  -j  REJECT
[root@proxy ~]# iptables -t filter -I  INPUT   -p  icmp  -j  ACCEPT
[root@proxy ~]# iptables  -I  INPUT  -p  icmp  -j  REJECT
//注意事项与规律:
//可以不指定表,默认为filter表
//可以不指定链,默认为对应表的所有链
//如果没有找到匹配条件,则执行防火墙默认规则
//选项/链名/目标操作用大写字母,其余都小写
########################################################################
//目标操作:
// ACCEPT:允许通过/放行
// DROP:直接丢弃,不给出任何回应
// REJECT:拒绝通过,必要时会给出提示
// LOG:记录日志,然后传给下一条规则

iptables命令的常用选项如表所示。

2)iptables命令的使用案例
创建规则的案例:

[root@proxy ~]# iptables  -t  filter  -A  INPUT  -p tcp  -j  ACCEPT
//追加规则至filter表中的INPUT链的末尾,允许任何人使用TCP协议访问本机
[root@proxy ~]# iptables  -I  INPUT  -p  udp  -j  ACCEPT
//插入规则至filter表中的INPUT链的开头,允许任何人使用UDP协议访问本机
[root@proxy ~]# iptables  -I  INPUT 2  -p  icmp  -j  ACCEPT
//插入规则至filter表中的INPUT链的第2行,允许任何人使用ICMP协议访问本机

查看iptables防火墙规则

[root@proxy ~]# iptables  -nL  INPUT                    //仅查看INPUT链的规则
target     prot opt source               destination
ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0
[root@proxy ~]# iptables  -L  INPUT  --line-numbers        //查看规则,显示行号
num  target     prot opt source         destination
1    ACCEPT     udp   --  anywhere     anywhere
2    ACCEPT     icmp --   anywhere     anywhere
3    ACCEPT     tcp  --   anywhere     anywhere

删除规则,清空所有规则

[root@proxy ~]# iptables  -D  INPUT  3
//删除filter表中INPUT链的第3条规则
[root@proxy ~]# iptables  -nL  INPUT                //查看规则,确认是否删除
[root@proxy ~]# iptables  -F
//清空filter表中所有链的防火墙规则
[root@proxy ~]# iptables  -t  nat  -F
//清空nat表中所有链的防火墙规则
[root@proxy ~]# iptables  -t  mangle  -F
//清空mangle表中所有链的防火墙规则
[root@proxy ~]# iptables  -t  raw  -F
//清空raw表中所有链的防火墙规则

设置防火墙默认规则

[root@proxy ~]# iptables  -t  filter  -P  INPUT  DROP
[root@proxy ~]# iptables  -nL
Chain INPUT (policy DROP)
… …
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值