Iptables(防火墙)相关

一、iptables防火墙简介

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

二、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

三、iptables原理简介

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

  • 清楚所有规则

    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(ping的协议)、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请求(注意ping是协议,可以被禁止)

    iptables -I INPUT -p icmp –icmp-type 8 -s 192.168.50.100 -j DROP

  • 匹配端口范围

    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

  • 永久关闭防火墙

    chkconfig iptables off

  • 永久关闭后启用

    chkconfig iptables on

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值