iptables summary

本文详细介绍了iptables的基本概念及其在Linux系统中的应用。包括iptables的工作原理、常用命令、配置规则等,并通过实例讲解了SNAT、DNAT及MASQUERADE等功能。

iptables is a user counterpart of Netfilter, which can filter, mangle packets it received. Some NAT gateway, stateless/stateful firewall, transparent proxy in Linux are implemented by iptables. Here are some items and properties of iptables.

 

 

1、Tables: for -t table.  NAT(SNAT, DNAT, MASQERADE etc), filter(default table), mangle.

SNAT: iptables modify source ip in outgoing packet as designated IP. In the view of receiver, the packets are from designated IP.

MASQERADE: For dial or DHCP network(by PPP or SLIP to access the Internet, the Internet IP is dynamic or we don't know the IP, it's the best choice), gateway modify the source IP of outgoing packets by the computer who sends them.  (实现router带LAN上网的方式)

DNAT: iptables modify dest IP of packets. For example, if you want to access A, but gateway modify dest IP as B, you think you are accessing A, but in fact you are accessing B. (将内网的服务端口映射到外网,从而可以被外部主机访问)

 

Since the route are selected by destination, DNAT =>PREROUTING(BEFORE the diamond in the figure), whil SNAT =>POSTROUTING.

SNAT example: iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to-source 58.20.51.66

DNAT example: iptables -t nat -A PREROUTING -d 202.103.96.112 -j DNAT --to-destination 192.168.0.112

MASQUERADE example: iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

 

 

2、Hook points:for -A/-I/-R/-Z . PREROUTING, FORWARD, INPUT, OUTPUT, POSTROUTING in a chain.

Routing Decision: Whethrer the the dest IP is our local, if so, deliver the packet to INPUT, otherwise, FORWARD it.

3、Target:for -j target. (ACCEPT, DROP, RETURN etc)

4、Match:for -m match_option. Too much, have subcommand(IP, TCP, ICMP etc) .

 

Command examples:

1、List some table:

iptables -t nat -L -n -v

2、Stop it: /etc/init.d/iptables stop or service iptables stop

disable it when boot: chkconfig iptables off

3、config data locates in /etc/sysconfig/iptables.

if you want to save current iptables rules, run iptables save (it call iptables-save >/etc/sysconfig/iptables)

if you want to load external modules, modify in /etc/sysconfig/iptables-config

4、You should set all rules as ACCEPT first if you want to custom you firewall.

For example:

iptables -t nat -P OUTPUT ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT

5、"First-match" rule:

iptables -P INPUT ACCEPT
iptables -A INPUT -s 192.168.1.1 -j ACCEPT
iptables -A INPUT -s 192.168.1.0/24 -j DROP  <===allow 192.168.1.1 in 192.168.1.0/24.

is not the same as:

iptables -P INPUT ACCEPT
iptables -A INPUT -j ACCEPT <=== packets for other IP match this rule, they will not be passed to next.
iptables -A INPUT -s 192.168.1.0/24 -j DROP

 6、set rules for passive FTP:

modprobe ip_conntrack_ftp

iptables -P INPUT DROP
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT

 7、iptables -A POSTROUTING -t nat -s 192.168.1.0/24 -j MASQUERADE

equal to

iptables -A POSTROUTING -t nat -s 192.168.1.0/24 -j SNAT --to 210.1.1.1

to some extent(know the Internet IP).

 

 

 

This is an URL about iptables:

http://linux.chinaunix.net/bbs/viewthread.php?tid=812400

iptables tutorial by Oskar Andreasson:

http://www.frozentux.net/iptables-tutorial/iptables-tutorial.html

and Chinese version:

http://www.frozentux.net/iptables-tutorial/cn/iptables-tutorial-cn-1.1.19.html

this article describe common use for a static FW:

http://linux.ccidnet.com/art/737/20060705/596613_1.html

 

 

AI-PPT 一键生成 PPT:用户输入主题关键词,AI-PPT 可快速生成完整 PPT,涵盖标题、正文、段落结构等,还支持对话式生成,用户可在 AI 交互窗口边查看边修改。 文档导入转 PPT:支持导入 Word、Excel、PDF 等多种格式文档,自动解析文档结构,将其转换为结构清晰、排版规范的 PPT,有保持原文和智能优化两种模式。 AI-PPT 对话 实时问答:用户上传 PPT 或 PPTX 文件后,可针对演示内容进行提问,AI 实时提供解答,帮助用户快速理解内容。 多角度内容分析:对 PPT 内容进行多角度分析,提供全面视野,帮助用户更好地把握内容结构和重点。 多语言对话支持:支持多语言对话,打破语言障碍,方便不同语言背景的用户使用。 AI - 绘图 文生图:用户输入文字描述,即可生成符合语义的不同风格图像,如油画、水彩、中国画等,支持中英文双语输入。 图生图:用户上传图片并输入描述,AI - 绘图能够根据参考图和描述生成新的风格化图像,适用于需要特定风格或元素的创作需求。 图像编辑:提供如 AI 超清、AI 扩图、AI 无痕消除等功能,用户可以上传图片进行细节修改和优化,提升图片质量。 AI - 文稿 文案生成:能够根据用户需求生成多种类型的文章,如市场营销文案、技术文档、内部沟通内容等,提升文案质量和创作效率。 文章润色:对已有文章进行改善和优化,包括语言表达、逻辑连贯性、内容流畅度等方面,使文章更符合用户期望和风格。 文章续写:AI 技术理解文本语境,为用户提供新的想法、补充资料或更深层次的见解,帮助用户丰富文档内容。 AI - 医生 智能健康咨询:包括症状自查,用户输入不适症状,AI 结合病史等信息提供疾病可能性分析与初步建议;用药指导,支持查询药品适应症、禁忌症等,并预警潜在冲突;中医辨证,提供体质辨识与调理建议。 医学报告解读:用户上传体检报告
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值