Linux中Iptables防火墙的使用

2021/12/28 7:07:08

本文主要是介绍Linux中Iptables防火墙的使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Linux中Iptables防火墙的使用

  • Iptables的使用
  • Iptables动作
  • Iptables基本的条件匹配
  • -s源地址、-d目标地址
  • --sport源端口、--dport目标端口
  • -i、-o、-m、-j动作
  • 模块

 

一、Iptables的使用

  1.安装Iptables

[root@m01 ~]# yum install iptables*
可以使用 rpm -q iptables 命令查看iptables是否安装

  2.启动Iptables

[root@m01 ~]# systemctl start iptables
可以使用 systemctl status iptables 命令查看iptables状态

  3.关闭firewalld

[root@m01 ~]# systemctl disable --now firewalld

  格式:

-t               指定操作的表
-L, --list       列出当前的规则
-v               显示数据包和数据包大小
-n               不反解地址
-A, --append     追加一条规则到链中
-I, --insert     插入一条规则,插入到顶部
-F, --flush      清空
-Z, --zero       清空计数器(  包数量 、包大小)

-D, --delete     删除链中的规则

-R, --replace    修改
-S, --list-rules 列出所有的规则


-N, --new-chain  创建一个自定义 链
-X, --delete-chain 删除一个自定义链
-P, --policy     指定链的默认策略

 

二、Iptables动作

ACCEPT     将数据包放行,进行完此处理动作后,将不再比对其它规则,直接跳往下一个规则链。
REJECT     拦阻该数据包,并传送数据包通知对方。
DROP       丢弃包不予处理,进行完此处理动作后,将不再比对其它规则,直接中断过滤程序。
REDIRECT   将包重新导向到另一个端口,进行完此处理动作后,将会继续比对其它规则。

 

三、Iptables基本的条件匹配

TCP(http)
UDP
ICMP(ping)
ALL

 

四、-s源地址、-d目标地址

  源地址:发送请求的地址

  目标地址:访问的地址

 

五、--sport源端口、--dport目标端口

  源端口:发送请求的端口

  目标端口:访问的端口

 

六、-i、-o、-m、-j动作

-i : 进来的网卡
-o : 出去的网卡
-m : 指定模块
-j : 转发动作
-p :指定协议

 

七、案例

  案例1:只允许22端口可以访问,其他端口全部无法访问

iptables -t filter -A INPUT -p TCP --dport 22  -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

  案例2:只允许22,80,443端口可以访问,其他端口全部无法访问

iptables -t filter -A INPUT -p TCP --dport 22  -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 80  -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 443  -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

  案例3:只允许22,80,443端口可以访问,其他端口全部无法访问,但是本机可以访问百度

  

  案例4:要求使用192.168.15.81能够通过22端口链接,但是其他的不行

iptables -t filter -A INPUT -p TCP -d 192.168.15.81 --dport 22  -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

  案例5:只允许192.168.15.71能够通过22端口链接,其他的不行

iptables -t filter -A INPUT -p  TCP -s 192.168.15.71  -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

  案例6:要求192.168.15.71对外部不可见

iptables -t filter -A INPUT -p TCP -d 192.168.15.71 -j DROP

  案例7:要求使用eth0网卡的所有请求全部拒绝

iptables -t filter -A INPUT -p TCP -i eth0 -j DROP

  案例8:要求访问服务器的8080端口转发至80端口

iptables -t nat -A PREROUTING -p TCP --dport 8080 -j REDIRECT --to-port 80

  案例9:要求只允许windows通过ssh连接192.168.15.81,其他的拒绝

iptables -t filter -I INPUT -p TCP -s 192.168.15.1 -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 22 -j DROP

  知识储备:

    查看本机端口占用命令

netstat -nutlp

 

八、模块

  拓展Iptables的功能

-m : 指定模块

  1.连续匹配多个端口(multiport)

--dports  : 指定多个端口(不同端口之间以逗号分割,连续的端口使用冒号分割)

  2.指定一段连续的ip地址范围(iprange)

--src-range from[-to]:源地址范围
--dst-range from[-to]:目标地址范围

  3.匹配指定字符串(string)

--string pattern  # 指定要匹配的字符串
--algo {bm|kmp}  # 匹配的查询算法

  4.根据时间段匹配报文(time)

--timestart hh:mm[:ss]    # 开始时间
--timestop hh:mm[:ss]    # 结束时间
--monthdays day[,day...]    # 指定一个月的某一天
--weekdays day[,day...]    # 指定周 还是  周天 

  5.禁ping, 默认本机无法ping别人 、别人无法ping自己

--icmp-type {type[/code]|typename}
echo-request  (8) 请求 
echo-reply    (0) 回应

  6.限制链接数,并发连接数(connlimit)

--connlimit-upto n        #  如果现有连接数小于或等于  n  则 匹配
--connlimit-above n       #  如果现有连接数大于n 则匹配

  7.针对 报文速率 进行限制。 秒、分钟、小时、天

--limit rate[/second|/minute|/hour|/day]  # 报文数量 
--limit-burst number  # 报文数量(默认:5)

  

九、模块案例

  1.要求将22,80,443以及30000-50000之间所有的端口向外暴露,其他端口拒绝

iptables -t filter -A INPUT -p TCP -m multiport --dports 22,80,443,30000:50000 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

  2.要求访问数据包中包含HelloWorld的数据不允许通过

iptables -t filter -A INPUT -p TCP -m string --string "HelloWorld" --algo kmp -j DROP

  3.要求192.168.15.1 - 192.168.15.10之间的所有IP能够连接192.168.15.81,其他拒绝

iptables -t filter -A INPUT -p TCP -m iprange --src-range 192.168.15.1-192.168.15.10 -j ACCEPT 
iptables -t filter -A INPUT -p TCP -j DROP

  4.要求每天的12到13之间,不允许访问

iptables -t filter -A INPUT -p TCP -m time  --timestart 4:00   --timestop 5:00 -j DROP

    注意:时间必须使用UTC时间

  5.要求别人不能ping本机,但是本机可以ping别人

iptables -t filter -A INPUT -p TCP -m icmp --icmp-type "echo-request" -j DROP

  6.要求主机连接最多有2个

iptables -t filter -A INPUT -p TCP --dport 22 -m connlimit --connlimit-above 2 -j DROP

  7.要求限制速率在500k/s左右

iptables -t filter -A INPUT -p TCP -m limit 333/s -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP

 



这篇关于Linux中Iptables防火墙的使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程