当前位置:网站首页>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
边栏推荐
- 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
- 宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权
- 【Leetcode】14. 最长公共前缀
- Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
- What will you do after digital IC Verification?
- 剑指 Offer 27. 二叉树的镜像
- 871. Minimum refueling times
- 2、 Expansion of mock platform
- PWM controlled steering gear
- Executive engine module of high performance data warehouse practice based on Impala
猜你喜欢

几行代码搞定RPC服务注册和发现

綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東

你想要的宏基因组-微生物组知识全在这(2022.7)

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

Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
![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])

PhD battle-11 preview | review and prospect backdoor attack and defense of neural network

智能垃圾桶(五)——点亮OLED

绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东

电脑自带软件使图片底色变为透明(抠图白底)
随机推荐
Amazon cloud technology community builder application window opens
The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
Deep learning image data automatic annotation [easy to understand]
Sword finger offer 25 Merge two sorted linked lists
Ap和F107数据来源及处理
lsf基础命令
vscode设置删除行快捷键[通俗易懂]
Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品
几行代码搞定RPC服务注册和发现
Exploration of mobile application performance tools
Configure MySQL under Linux to authorize a user to access remotely, which is not restricted by IP
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
VMware install win10 image
DGraph: 大规模动态图数据集
Soul, a social meta universe platform, rushed to Hong Kong stocks: Tencent is a shareholder with an annual revenue of 1.28 billion
酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
智能垃圾桶(五)——点亮OLED
Understand one article: four types of data index system
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
Timing / counter of 32 and 51 single chip microcomputer