iptables 脚本实战

本文简述 iptables 主要功能语法,及 iptables 快捷使用脚本,网络命令如此繁多的linux,想把都记在脑海中确实比较难,特意将常用命令写成脚本形式可以直接运行,提高使用效率,文末附有 iptables.sh 快捷脚本的 github地址,脚本工具刚开始制作,本着完全开源的、以实用为目的来进行开发,有想法的朋友可以一起丰富它的功能,更好的提供给更多的人使用。

 

        The roses in her hand,the flavor in mine.                       -----  For everyone  

 

目录

1.iptables简介

2.基本语法

3.iptables中的“四表五链”及“堵通策略”

4.iptables tools 简述

5.iptables tools快捷工具源码 github 地址

6.iptables优秀博客推荐


1.iptables简介

iptables的主要功能是实现对网络数据包进出设备及转发的控制。当数据包需要进入设备、从设备中流出或者经该设备转发、路由时,都可以使用iptables进行控制。

2.基本语法及常用命令

基本语法:

Usage: iptables -[AD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)

常用命令:

      -t<表>:指定要操纵的表;
      -A:向规则链中添加条目;
      -D:从规则链中删除条目;
      -i:向规则链中插入条目;
      -R:替换规则链中的条目;
      -L:显示规则链中已有的条目;
      -F:清楚规则链中已有的条目;
      -Z:清空规则链中的数据包计算器和字节计数器;
      -N:创建新的用户自定义规则链;
      -P:定义规则链中的默认目标;
      -h:显示帮助信息;
      -p:指定要匹配的数据包协议类型;
      -s:指定要匹配的数据包源ip地址;
      -j<目标>:指定要跳转的目标;
      -i<网络接口>:指定数据包进入本机的网络接口;
      -o<网络接口>:指定数据包要离开本机所使用的网络接口。

3.iptables中的“四表五链”及“堵通策略”

 

A.“四表”是指,iptables的功能——filter, nat, mangle, raw.

    filter, 控制数据包是否允许进出及转发(INPUT、OUTPUT、FORWARD),可以控制的链路有input, forward, output

    nat, 控制数据包中地址转换,可以控制的链路有prerouting, input, output, postrouting

    mangle,修改数据包中的原数据,可以控制的链路有prerouting, input, forward, output, postrouting

    raw,控制nat表中连接追踪机制的启用状况,可以控制的链路有prerouting, output

  注:在centos7中,还有security表,不过这里不作介绍

B.“五链”是指内核中控制网络的NetFilter定义的五个规则链,分别为

    PREROUTING, 路由前

    INPUT, 数据包流入口

    FORWARD, 转发管卡

    OUTPUT, 数据包出口

    POSTROUTING, 路由后

C.堵通策略是指对数据包所做的操作,一般有两种操作——“通(ACCEPT)”、“堵(DROP)”,还有一种操作很常见REJECT.

谈谈REJECT和DROP之间的区别,Ming写了一封信,向Rose示爱。Rose如果不愿意接受,她可以不回应Ming,这个时候Ming不确定Rose是否接到了信;Rose也可以同样写一封信,在信中明确地拒绝Ming。前一种操作就如同执行了DROP操作,而后一种操作就如同REJECT操作。

4.iptables tools 简述

下面为iptables 工具运行主菜单,目前脚本支持 CentOS 6.x.x 上正常运行,其他平台需稍作调整,有想法的朋友可以增加更多的平台或者丰富更多的功能。

fs@ubuntu:~/test$ ./iptables.sh 

-------------- iptables  快捷通用脚本 v 1.0.1 ---------------

	注意:该版本仅适用 CentOS 6.x.x 版本

-------------- 菜单 ---------------

  0. 展示iptables语法规则
  1. 展开所有规则链
  2. 打开 INPUT 端口,同时指定tcp udp,并保存配置重启防火墙
  3. 删除 INPUT 端口,同时指定tcp udp,并保存配置重启防火墙
  4. 保存防火墙配置
  5. 重启防火墙
  6. 产看某端口占用情况
  7. 查看系统端口占用情况
  8. 查看所有的进程和端口使用情况
  9. 指定删除已添加的iptables规则
 10. 屏避IP
 11. 退出应用

Please enter your slect: 

5.iptables tools快捷工具源码 github 地址

https://github.com/SunJiahao2017/shell-tools

6.iptables优秀博客推荐

朱双印 iptables详解: http://www.zsythink.net/archives/tag/iptables/

iptables使用详解:https://www.cnblogs.com/vathe/p/6973656.html

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的iptables脚本,可以阻止所有入站流量,允许所有出站流量: ``` #!/bin/bash # 清空所有规则 iptables -F # 设置默认策略 iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT # 允许回环接口(localhost) iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT # 允许已经建立的连接 iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # 允许ssh连接 iptables -A INPUT -p tcp --dport 22 -j ACCEPT # 允许ping iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT # 允许HTTP和HTTPS连接 iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 防止DOS攻击 iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT # 允许DNS查询 iptables -A INPUT -p udp --dport 53 -j ACCEPT iptables -A INPUT -p tcp --dport 53 -j ACCEPT # 允许NTP时间同步 iptables -A INPUT -p udp --dport 123 -j ACCEPT # 允许SMTP邮件 iptables -A INPUT -p tcp --dport 25 -j ACCEPT # 允许POP3邮件 iptables -A INPUT -p tcp --dport 110 -j ACCEPT # 允许IMAP邮件 iptables -A INPUT -p tcp --dport 143 -j ACCEPT # 允许FTP连接 iptables -A INPUT -p tcp --dport 20:21 -j ACCEPT # 允许MySQL连接 iptables -A INPUT -p tcp --dport 3306 -j ACCEPT # 允许SSH连接 iptables -A INPUT -p tcp --dport 22 -j ACCEPT # 允许Git连接 iptables -A INPUT -p tcp --dport 9418 -j ACCEPT # 允许SVN连接 iptables -A INPUT -p tcp --dport 3690 -j ACCEPT # 允许Redis连接 iptables -A INPUT -p tcp --dport 6379 -j ACCEPT # 允许Memcached连接 iptables -A INPUT -p tcp --dport 11211 -j ACCEPT # 允许MongoDB连接 iptables -A INPUT -p tcp --dport 27017 -j ACCEPT # 允许Elasticsearch连接 iptables -A INPUT -p tcp --dport 9200 -j ACCEPT iptables -A INPUT -p tcp --dport 9300 -j ACCEPT # 允许Zookeeper连接 iptables -A INPUT -p tcp --dport 2181 -j ACCEPT # 允许Kafka连接 iptables -A INPUT -p tcp --dport 9092 -j ACCEPT # 允许Kibana连接 iptables -A INPUT -p tcp --dport 5601 -j ACCEPT # 允许RabbitMQ连接 iptables -A INPUT -p tcp --dport 5672 -j ACCEPT # 允许Zabbix连接 iptables -A INPUT -p tcp --dport 10050 -j ACCEPT # 允许Prometheus连接 iptables -A INPUT -p tcp --dport 9090 -j ACCEPT # 允许Grafana连接 iptables -A INPUT -p tcp --dport 3000 -j ACCEPT # 允许Jenkins连接 iptables -A INPUT -p tcp --dport 8080 -j ACCEPT # 允许Docker连接 iptables -A INPUT -p tcp --dport 2375 -j ACCEPT iptables -A INPUT -p tcp --dport 2376 -j ACCEPT # 保存规则 iptables-save > /etc/sysconfig/iptables ``` 这个脚本假定你的系统默认是没有任何防火墙规则的。如果你已经有一些防火墙规则,那么需要根据实际情况进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值