当前位置:网站首页>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
边栏推荐
- Un an à dix ans
- go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
- Qstype implementation of self drawing interface project practice (II)
- Easy language ABCD sort
- ETH数据集下载及相关问题
- How to quickly distinguish controlled components from uncontrolled components?
- Jiuxian's IPO was terminated: Sequoia and Dongfang Fuhai were shareholders who had planned to raise 1billion yuan
- PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
- System Verilog implements priority arbiter
- QStyle实现自绘界面项目实战(二)
猜你喜欢

DGraph: 大规模动态图数据集

Blog theme "text" summer fresh Special Edition

Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity

【Leetcode】13. Roman numeral to integer

体验居家办公完成项目有感 | 社区征文

Sword finger offer 25 Merge two sorted linked lists
![[essay solicitation activity] Dear developer, RT thread community calls you to contribute](/img/31/11409606718e0f4837f4cc572172a3.png)
[essay solicitation activity] Dear developer, RT thread community calls you to contribute

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

配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)

Use the API port of the bridge of knowledge and action to provide resources for partners to access
随机推荐
871. Minimum refueling times
【Leetcode】13. 罗马数字转整数
In MySQL and Oracle, the boundary and range of between and precautions when querying the date
One year is worth ten years
Changwan group rushed to Hong Kong stocks: the annual revenue was 289million, and Liu Hui had 53.46% voting rights
畅玩集团冲刺港股:年营收2.89亿 刘辉有53.46%投票权
宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权
Xiaopeng P7 had an accident on rainy days, and the airbag did not pop up. Official response: the impact strength did not meet the ejection requirements
R及RStudio下载安装教程(超详细)
福元医药上交所上市:市值105亿 胡柏藩身价超40亿
Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!
MOSFET器件手册关键参数解读
Easy language ABCD sort
入行数字IC验证后会做些什么?
基于多元时间序列对高考预测分析案例
What will you do after digital IC Verification?
Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
TCP拥塞控制详解 | 2. 背景
Use the API port of the bridge of knowledge and action to provide resources for partners to access
Leetcode1380: lucky numbers in matrix