day2-Iptables笔记

1.   iptables防火墙简介

Iptables也叫netfilter是Linux下自带的一款免费且优秀的基于包过滤的防火墙工具,它的功能十分强大,使用非常灵活,可以对流入、流出、流经服务器的数据包进行精细的控制。iptables是Linux2.4及2.6内核中集成的模块。

2.   Iptables服务相关命令

1.查看iptables状态

service iptables status

2.开启/关闭iptables

service iptables start

service iptables stop

3.查看iptables是否开机启动

chkconfig iptables --list

4.设置iptables开机启动/不启动

chkconfig iptables on

chkconfig iptables off

3.   iptables原理简介

3.1.  iptables的结构

在iptables中有四张表,分别是filter、nat、mangle和raw每一个表中都包含了各自不同的链,最常用的是filter表。

 

filter表:

filter是iptables默认使用的表,负责对流入、流出本机的数据包进行过滤,该表中定义了3个链:

         INPOUT 负责过滤所有目标地址是本机地址的数据包,就是过滤进入主机的数据包。

         FORWARD       负责转发流经本机但不进入本机的数据包,起到转发的作用。

         OUTPUT  负责处理所有源地址是本机地址的数据包,就是处理从主机发出去的数据包。

--------------------------------常用命令-------------------------------------------

#查看帮助
iptables -h
man iptables

列出iptables规则
iptables -L -n
列出iptables规则并显示规则编号
iptables -L -n --line-numbers

列出iptables nat表规则(默认是filter表)
iptables -L -n -t nat

清除默认规则(注意默认是filter表,如果对nat表操作要加-t nat)
#清楚所有规则
iptables -F

#重启iptables发现规则依然存在,因为没有保存
service iptables restart

#保存配置
service iptables save

#禁止ssh登陆(若果服务器在机房,一定要小心)
iptables -A INPUT -p tcp --dport 22 -j DROP
#删除规则
iptables -D INPUT -p tcp --dport 22 -j DROP

-A, --append chain 追加到规则的最后一条
-D, --delete chain [rulenum] Delete rule rulenum (1 = first) from chain
-I, --insert chain [rulenum] Insert in chain as rulenum (default 1=first) 添加到规则的第一条
-p, --proto proto protocol: by number or name, eg. 'tcp',常用协议有tcp、udp、icmp、all
-j, --jump target 常见的行为有ACCEPT、DROP和REJECT三种,但一般不用REJECT,会带来安全隐患

注意:INPUT和DROP这样的关键字需要大写

#禁止192.168.33.0网段从eth0网卡接入
iptables -A INPUT -p tcp -i eth0 -s 192.168.33.0 -j DROP
iptables -A INPUT -p tcp --dport 22 -i eth0 -s 192.168.33.61 -j ACCEPT

#禁止ip地址非192.168.10.10的所有类型数据接入
iptables -A INPUT ! -s 192.168.10.10 -j DROP

#禁止ip地址非192.168.10.10的ping请求
iptables -I INPUT -p icmp --icmp-type 8 -s 192.168.50.100 -j DROP

#扩展匹配:1.隐式扩展 2.显示扩展
#隐式扩展
-p tcp
--sport PORT 源端口
--dport PORT 目标端口

#显示扩展:使用额外的匹配规则
-m EXTENSTION --SUB-OPT
-p tcp --dport 22 与 -p tcp -m tcp --dport 22功能相同

state:状态扩展,接口ip_contrack追踪会话状态
NEW:新的连接请求
ESTABLISHED:已建立的连接请求
INVALID:非法连接
RELATED:相关联的连接

#匹配端口范围
iptables -I INPUT -p tcp --dport 22:80 -j DROP

#匹配多个端口
iptables -I INPUT -p tcp -m multiport --dport 22,80,3306 -j ACCEPT

#不允许源端口为80的数据流出
iptables -I OUTPUT -p tcp --sport 80 -j DROP


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

原文地址:https://www.cnblogs.com/bee-home/p/7832691.html