Linux学习整理-网络防火墙iptables-实践篇1

目录

1 实验环境

2 iptables常用用法

2.1 查看iptables链

2.2 规则追加

2.3 规则删除

2.4 规则全删除

2.5 自定义链(chain)

2.6 删除自定义链

2.7 插入规则

2.8 修改规则

2.9 其它

2.9.1 清除计数

2.9.2 打印规则

3 总结


1 实验环境


                       192.168.0.0/24
访问用机器(Centos) --------------------- iptables设定机器(Ubuntu)
   192.168.0.211                            192.168.0.203

2 iptables常用用法

2.1 查看iptables链

语法:iptables -t [table] -nvL

[table] :raw ,mangle,filter,nat,security

-n:让IP地址和端口号都表示成数字

-v:显示最详细的内容

-L:打印链的所有规则

 使用例

# 显示nat表的所有链的规则
root@node2:~# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 39 packets, 45144 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 39 packets, 45144 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 4 packets, 279 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 4 packets, 279 bytes)
 pkts bytes target     prot opt in     out     source               destination 


# 显示nat表的INPUT链的规则
root@node2:~# iptables -t nat -nvL INPUT
Chain INPUT (policy ACCEPT 39 packets, 45144 bytes)
 pkts bytes target     prot opt in     out     source               destination       

2.2 规则追加

iptables -t [table] -A [chain]  [match] [target]

[]里的参数的设定方式可以参照上一篇文章

 使用例

# 在filter表的INPUT链追加规则,icmp的时候,拒绝并告知送信方
root@node2:~# iptables -t filter -A INPUT -p icmp -j REJECT 

# 可以看到规则添加成功
root@node2:~# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 142 packets, 58712 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    4   336 REJECT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable


# 在Centos上ping Ubuntu发现被拒绝
[root@centos ~]# ping 192.168.0.203 -c 4
PING 192.168.0.203 (192.168.0.203) 56(84) bytes of data.
From 192.168.0.203 icmp_seq=1 Destination Port Unreachable
From 192.168.0.203 icmp_seq=2 Destination Port Unreachable
From 192.168.0.203 icmp_seq=3 Destination Port Unreachable
From 192.168.0.203 icmp_seq=4 Destination Port Unreachable

--- 192.168.0.203 ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 3023ms

2.3 规则删除

iptables -t [table] -D [chain] [linenumber]

 使用例

# 先查看规则的编号(-t省略的时候默认是filter表)
root@node2:~# iptables -nvL INPUT --line-numbers 
Chain INPUT (policy ACCEPT 70 packets, 15375 bytes)
num   pkts bytes target     prot opt 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值