当前位置:网站首页>IPtables中SNAT、DNAT和MASQUERADE的含义
IPtables中SNAT、DNAT和MASQUERADE的含义
2022-07-02 14:42:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
IPtables中可以灵活的做各种网络地址转换(NAT),网络地址转换主要有两种:SNAT和DNAT。
SNAT是source networkaddress translation的缩写,即源地址目标转换。比如,多个PC机使用ADSL路由器共享上网,每个PC机都配置了内网IP,PC机访问外部网络的时候,路由器将数据包的报头中的源地址替换成路由器的ip,当外部网络的服务器比如网站web服务器接到访问请求的时候,他的日志记录下来的是路由器的ip地址,而不是pc机的内网ip,这是因为,这个服务器收到的数据包的报头里边的“源地址”,已经被替换了,所以叫做SNAT,基于源地址的地址转换。
DNAT是destination networkaddress translation的缩写,即目标网络地址转换,典型的应用是,有个web服务器放在内网配置内网ip,前端有个防火墙配置公网ip,互联网上的访问者使用公网ip来访问这个网站,当访问的时候,客户端发出一个数据包,这个数据包的报头里边,目标地址写的是防火墙的公网ip,防火墙会把这个数据包的报头改写一次,将目标地址改写成web服务器的内网ip,然后再把这个数据包发送到内网的web服务器上,这样,数据包就穿透了防火墙,并从公网ip变成了一个对内网地址的访问了,即DNAT,基于目标的网络地址转换。
MASQUERADE,地址伪装,算是snat中的一种特例,可以实现自动化的snat。
在iptables中有着和SNAT相近的效果,但也有一些区别,但使用SNAT的时候,出口ip的地址范围可以是一个,也可以是多个,例如:
如下命令表示把所有10.8.0.0网段的数据包SNAT成192.168.5.3的ip然后发出去,
iptables-t nat -A POSTROUTING -s 10.8.0.0/255.255.255.0 -o eth0 -j SNAT –to-source192.168.5.3
如下命令表示把所有10.8.0.0网段的数据包SNAT成192.168.5.3/192.168.5.4/192.168.5.5等几个ip然后发出去
iptables-t nat -A POSTROUTING -s 10.8.0.0/255.255.255.0 -o eth0 -j SNAT –to-source192.168.5.3-192.168.5.5
这就是SNAT的使用方法,即可以NAT成一个地址,也可以NAT成多个地址,但是,对于SNAT,不管是几个地址,必须明确的指定要SNAT的ip,假如当前系统用的是ADSL动态拨号方式,那么每次拨号,出口ip192.168.5.3都会改变,而且改变的幅度很大,不一定是192.168.5.3到192.168.5.5范围内的地址,这个时候如果按照现在的方式来配置iptables就会出现问题了,因为每次拨号后,服务器地址都会变化,而iptables规则内的ip是不会随着自动变化的,每次地址变化后都必须手工修改一次iptables,把规则里边的固定ip改成新的ip,这样是非常不好用的。
MASQUERADE就是针对这种场景而设计的,他的作用是,从服务器的网卡上,自动获取当前ip地址来做NAT。
比如下边的命令:
iptables-t nat -A POSTROUTING -s 10.8.0.0/255.255.255.0 -o eth0 -j MASQUERADE
如此配置的话,不用指定SNAT的目标ip了,不管现在eth0的出口获得了怎样的动态ip,MASQUERADE会自动读取eth0现在的ip地址然后做SNAT出去,这样就实现了很好的动态SNAT地址转换。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/148098.html原文链接:https://javaforall.cn
边栏推荐
- Qwebengineview crash and alternatives
- P6774 [NOI2020] 时代的眼泪(分块)
- Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
- 智能垃圾桶(五)——点亮OLED
- Method of C language self defining function
- 13、Darknet YOLO3
- Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
- IP地址转换地址段
- 对接保时捷及3PL EDI案例
- What if the default browser cannot be set?
猜你喜欢

Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers

The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?

剑指 Offer 24. 反转链表

七张图,学会做有价值的经营分析

Digital IC hand tearing code -- voting device

Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!

Soul, a social meta universe platform, rushed to Hong Kong stocks: Tencent is a shareholder with an annual revenue of 1.28 billion
![john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])](/img/4c/ddf7f8085257d0eb8766dbec251345.png)
john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])

Day 18 of leetcode dynamic planning introduction

How to transfer business data with BorgWarner through EDI?
随机推荐
What is generics- Introduction to generics
绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东
PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
体验居家办公完成项目有感 | 社区征文
A case study of college entrance examination prediction based on multivariate time series
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
如何与博格华纳BorgWarner通过EDI传输业务数据?
OpenPose的使用
Dgraph: large scale dynamic graph dataset
剑指 Offer 27. 二叉树的镜像
PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
Listing of chaozhuo Aviation Technology Co., Ltd.: raising 900million yuan, with a market value of more than 6billion yuan, becoming the first science and technology innovation board enterprise in Xia
AP and F107 data sources and processing
What will you do after digital IC Verification?
Domestic relatively good OJ platform [easy to understand]
Leetcode1380: lucky numbers in matrix
国内比较好的OJ平台[通俗易懂]
Detailed explanation of @accessories annotation of Lombok plug-in
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition (a sign in, B sign in, C sign in, D thinking +mst
社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东