linux操作系统iptables命令备忘录

举报
蝉鸣一夏 发表于 2018/06/06 09:57:58 2018/06/06
【摘要】 1.综述参考:https://blog.csdn.net/u011456940/article/details/52634184 https://blog.csdn.net/reyleon/article/details/12976341总而言之,iptables的一般用法iptables [-t 规则表] [命令][匹配][操作]英文表达法: iptables [-t table] [op...

1.综述

参考:https://blog.csdn.net/u011456940/article/details/52634184 


https://blog.csdn.net/reyleon/article/details/12976341


总而言之,iptables的一般用法

iptables [-t 规则表] [命令][匹配][操作]

英文表达法: iptables [-t table] [operation chain] [match] [action]



例如:iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT



2.关键功能,增删改查;


查:

iptables -L -n --line-number; #-L,列表展示,-n,展示ip地址而非反向解析为主机名,--


line-number,显示规则的序号


增:

iptables -t filter -A INPUT -p tcp [-s 192.168.1.5 -d 127.0.0.1 -sport 22] -dport 22 


-j ACCEPT


iptables -t filter -I INPUT 3 -p tcp --dport=26 -j ACCEPT


改:(或者说是替换)

iptables -t filter -R INPUT 3 -p udp --dport=28 -j ACCEPT



删:

iptables -t filter -D INPUT 3 



3.关键功能,规则链chain的默认策略选择。

默认策略:ACCEPT,DROP,REJECT;


常见两种配置方式,

方式一:默认允许所有请求包通过

iptables [-t filter] -P INPUT ACCEPT;



方式一:默认拒绝或者丢弃所有请求包

iptables [-t filter] -P INPUT DROP; 



4.关键功能:清空规则链


iptables -F;

iptables -F -t nat/mangle/filter;

iptables -X; # 清空自定义规则链


5.关于-m参数

-m:match,后接扩展包匹配模块,可以实现很多更为复杂的功能,例如利用防火墙防御ddos攻击



介于用法复杂,故而设定时候,请参考man iptables;


这里介绍一些常见的用法

用法案例参考网址:

https://www.cnblogs.com/hukey/p/6934893.html


-m multiport

iptables -A INPUT -p tcp -m multiport -dport 22,80 -j ACCEPT; #同时支持两个端口



6.iptables的规则保存,重启生效,以及备份恢复。

规则保存两种方式:

service iptables save,保存iptables规则。


iptables-save > /etc/sysconfig/iptables



备份与恢复:

iptables-save > ~/iptables.bak

iptables-restore < ~/iptables.bak



7.如何确认iptables服务是否开启了

centos 6:

service iptables status;

centos 7:

systemctl status iptables;



8.常见问题:

1)重启后,发现iptables规则被清空了,查看/etc/sysconfig/iptables,又有规则记录。

处理方案:chkconfig iptables on;



9.关于reject;

使用案例:

--reject-with type

              The type given can be

               icmp-net-unreachable

               icmp-host-unreachable

               icmp-port-unreachable

               icmp-proto-unreachable

               icmp-net-prohibited

               icmp-host-prohibited or

               icmp-admin-prohibited (*)



1)icmp-host-prohibited,导致ping不通,返回错误信息如下所示:

C:\Documents and Settings\Administrator>ping 172.16.3.101


Pinging 172.16.3.101 with 32 bytes of data:


Reply from 172.16.3.101: Destination host unreachable.

Reply from 172.16.3.101: Destination host unreachable.


2)icmp-net-unreachable 

Pinging 172.16.3.101 with 32 bytes of data:


Reply from 172.16.3.101: Destination net unreachable.

Reply from 172.16.3.101: Destination net unreachable.


3) icmp-proto-unreachable

Pinging 172.16.3.101 with 32 bytes of data:


Reply from 172.16.3.101: Destination protocol unreachable.

Reply from 172.16.3.101: Destination protocol unreachable.


......


总而言之,reject-with是定义返回的错误包的。


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200