原标题:实战经验:Linux Source NAT在Ping场景下的应用

有时候,有这样的一种需求:

需要修改IP数据包中的源地址,比如,从某一个主机发送Ping包到另一个主机,需要修改源地址为另一个源(通常,发出Ping请求的主机有多个网卡地址)。

为了解决这一需求,Linux下的netfilter组件中有个Source NAT的功能,可以修改IP数据包中的源地址。

此功能实际上是通过iptables在POSTROUTING链中添加一条规则,此规则在数据包被最终发送出去之前被应用。下面是一个实例:

主机A网络配置:

eth0: 192.168.10.10

eth1: 172.18.10.10

主机B:

eth0: 192.168.10.1

1) 第一张场景

从A发送Ping请求到B:

# ping 192.168.10.1

通过WireShark抓包可以知道,Ping包中的源地址为192.168.10.10(默认Ping请求从eth0出来),目的地址是192.168.10.1。

2) 第二种场景

从A发送Ping请求到B,并使用-I选项:

# ping 192.168.10.1 -I 172.18.10.10

在此场景下,这里指定了-I选项,表明指定源地址为172.18.10.10。

所以,Ping请求包中的源地址变为172.18.10.10,目的地址不变,依然为192.168.10.1。

问题来了:怎样在第二种场景中(在指定-I选项的情况下)将源地址修改为192.168.10.10?

解决方法:添加Source NAT规则。具体步骤如下:

添加规则:

# iptables -t nat -A POSTROUTING -o eth0 -j SNAT –to 192.168.10.10

添加完上述规则后,再次执行ping 192.168.10.1 -I 172.18.10.10,可以通过抓包发现Ping请求中的源地址已经由172.18.10.10修改为192.168.10.10。

备注:

如果想删除上面添加的Source NAT规则,可以执行如下指令删除:

删除规则:

# iptables -t nat -A POSTROUTING -o eth0 -j SNAT –to 192.168.10.10

查看规则:

# iptables -nvL -t nat

参考资料:

以下内容来自netfilter官网帮助文档,也记录在这里留作参考:

1) Source NAT

You want to do Source NAT; change the source address of connections to something different. This is done in the POSTROUTING chain, just before it is finally sent out; this is an important detail, since it means that anything else on the Linux box itself (routing, packet filtering) will see the packet unchanged. It also means that the `-o’ (outgoing interface) option can be used.

Source NAT is specified using `-j SNAT’, and the `–to-source’ option specifies an IP address, a range of IP addresses, and an optional port or range of ports (for UDP and TCP protocols only).

Masquerading

There is a specialized case of Source NAT called masquerading: it should only be used for dynamically-assigned IP addresses, such as standard dialups (for static IP addresses, use SNAT above).

You don’t need to put in the source address explicitly with masquerading: it will use the source address of the interface the packet is going out from. But more importantly, if the link goes down, the connections (which are now lost anyway) are forgotten, meaning fewer glitches when connection comes back up with a new IP address.

2) Destination NAT

This is done in the PREROUTING chain, just as the packet comes in; this means that anything else on the Linux box itself (routing, packet filtering) will see the packet going to its `real’ destination. It also means that the `-i’ (incoming interface) option can be used.

Destination NAT is specified using `-j DNAT’, and the `–to-destination’ option specifies an IP address, a range of IP addresses, and an optional port or range of ports (for UDP and TCP protocols only).

Redirection

There is a specialized case of Destination NAT called redirection: it is a simple convenience which is exactly equivalent to doing DNAT to the address of the incoming interface.

Note that squid needs to be configured to know it’s a transparent proxy!返回搜狐,查看更多

责任编辑:

linux添加源地址ping,实战经验:Linux Source NAT在Ping场景下的应用相关推荐

  1. Linux 高并发服务器实战 - 2 Linux多进程开发

    Linux 高并发服务器实战 - 2 Linux多进程开发 进程概述 概念1: 概念2: 微观而言,单CPU任意时刻只能运行一个程序 并发:两个队列交替使用一台咖啡机 并行:两个队列同时使用两台咖啡机 ...

  2. linux带source地址ping,实战经验:Linux Source NAT在Ping场景下的应用

    有时候,有这样的一种需求: 需要修改IP数据包中的源地址,比如,从某一个主机发送Ping包到另一个主机,需要修改源地址为另一个源(通常,发出Ping请求的主机有多个网卡地址). 为了解决这一需求,Li ...

  3. linux服务器运维实战记录,linux运维好书推荐《高性能Linux服务器运维实战》

    本书整体分为4个篇幅,以Linux运维平台下的开源应用软件为中心,涉及Linux运维的各个方面,主要从系统基础运维(命令.shell编程).系统性能调优.智能运维监控平台构建.运维实战案例四个方面展开 ...

  4. Linux 高并发服务器实战 - 1 Linux系统编程入门

    Linux 高并发服务器实战-1Linux系统编程入门 在本机和服务器端设置公共密钥(配置免密登录) 在本机cmd里输入 ssh-keygen -t rsa,生成本机的公密钥 在服务器端里也配置 ss ...

  5. Linux添加/删除用户和用户组(linux中,添加cvs用户,实质就是添加linux用户。)

    来源:http://www.cnblogs.com/xd502djj/archive/2011/11/23/2260094.html 本文总结了Linux添加或者删除用户和用户组时常用的一些命令和参数 ...

  6. linux添加用户命令_为Linux的cp和mv命令添加进度条

    cp和mv可能是大家日常中使用最多的Linux命令之一.但是有一个突出的问题是这两个命令都不会有任何提示信息,这在操作大文件时候只能干等.可能大家对此已经习以为常,但是其实上也有解决解决方法,本文我们 ...

  7. Linux从安装到实战+学校Linux+瑞吉外卖Linux项目部署

    1.0初识Linux 1.1虚拟机介绍 1.2VMware Workstation虚拟化软件 下载CentOS; 1.3远程链接Linux系统 &FinalShell 链接finalshell ...

  8. linux添加ssl信任根证书,linux系统添加根证书linux证书信任列表

    1.linux 访问 https 证书问题 [root@boss-test-dev001-jydx ~]# curl -v https://mobile.mycard520.com.tw * Abou ...

  9. linux添加硬盘分区设置柱面,linux 下添加新硬盘设备和硬盘分区格式化挂载使用磁盘配额限制...

    磁盘管理 1.查看硬盘设备信息 fdisk -l 2.磁盘分区.格式化.挂载使用 1).查看硬盘信息:fdisk -l [root@localhost ~]# fdisk -l Disk /dev/s ...

最新文章

  1. Python实现:开始日期 + 间隔天数,依次输入所有日期
  2. Linux CNTOS7 修改网络配置
  3. python opencv 拼接 连接 显示 图片
  4. 基于postfix一步一步构建Mailserver,支持虚拟用户,支持WebMail
  5. 无序列表属性 隐藏方式 JS简介
  6. Redis 05_List列表 数组 Hash散列
  7. 计算机系统基础:校验码知识笔记
  8. dev 中 gridcontrol1 滚动条重绘_浏览器的重绘和回流(Repaint amp; Reflow)
  9. phpcms发布文章:overflow不显示问题(解决“代码横向溢出”)- 含代码、案例、截图
  10. 数据库的主键和外键总结
  11. 《Managed DirectX +C# 开发(入门篇)》系列文章
  12. 日历小程序C语言,微信小程序实现日历功能
  13. Dos Cmd命令整理
  14. 思科Cisco Nexus 9508交换机的QSFP+端口高速线缆DAC布线方案
  15. 658. 一元二次方程公式
  16. ios 开发 flurry 资料
  17. 赛码网输入输出(js v8)问题并配置赛码网vscode本地环境
  18. discuz_result
  19. SpringBoot集成rabbitmq错误:org.springframework.amqp.AmqpConnectException: java.net.ConnectException的解决办法
  20. swiper一行多图踩坑

热门文章

  1. 为什么我学了6个月Python,还是找不到工作?
  2. 商家笑了 设计师哭了,京东+英特尔的AI这招太绝
  3. 漫画 | Kubernetes带你一帆风顺去远航
  4. win7计算机右键属性桌面进程重启,win7计算机右键属性打不开怎么办
  5. ElasticSearch 从安装开始_01
  6. linux shell脚本关闭指定端口号的进程
  7. Java基础--反射
  8. setScaledContents的看法
  9. setGeometry: Unable to set geometry 493x379+674+326 (frame: 517x443+662+274) on QWidgetWindow/“Dialo
  10. BugkuCTF-WEB题web16备份是个