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

需要修改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 -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带source地址ping,实战经验:Linux Source NAT在Ping场景下的应用相关推荐

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

    原标题:实战经验:Linux Source NAT在Ping场景下的应用 有时候,有这样的一种需求: 需要修改IP数据包中的源地址,比如,从某一个主机发送Ping包到另一个主机,需要修改源地址为另一个 ...

  2. Linux克隆Mac地址一样,详解Linux系统中网卡MAC地址克隆方法

    怎么临时性地改变 mac 地址? 你可以在 linux 运行的时候改变 mac 地址.需要注意的是当 mac 地址转换的那一会时间,你的网络会掉线.当电脑重启时 mac 地址又会变回原来的.下面介绍几 ...

  3. linux查询mac地址分屏,Tmux: linux下的分屏神器

    tmux tmux(terminal multiplexer)是Linux上的终端窗口的分屏神器.在一个终端窗口中可以分多个屏同时使用. 安装 Tmux 的安装. # Ubuntu $ sudo ap ...

  4. 免费linux脚本下载地址,shell脚本 下载|Linux shell脚本 v免费版下载 - D9下载站

    在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而且是一门非常棒的编程语言.您可以通过使用shell使大量的任务自动化,shel ...

  5. linux 查看内存地址 rw,如何查看Linux内存中的程序所有堆的地址

    匿名用户 1级 2016-11-12 回答 linux 下面查看内存有多种渠道,比如通过命令 ps ,top,free 等,比如通过/proc系统,一般需要比较详细和精确地知道整机内存/某个进程内存的 ...

  6. linux获取ip地址的接口,获取Linux上接口的IP地址

    sjsam.. 23 如果您正在寻找特定接口的地址(IPv4),请说wlan0然后尝试使用getifaddrs()的代码: #include #include #include #include #i ...

  7. linux获取ip地址的接口,在Linux上获取接口的IP地址

    尝试这个: #include #include #include /* for strncpy */ #include #include #include #include #include #inc ...

  8. 目标检测YOLO实战应用案例100讲-自动驾驶场景下的三维目标检测技术研究

    目录 辅助驾驶场景下的目标检测算法研究 传统目标检测方法 基于深度学习的目标检测方法

  9. 大量Linux资源下载地址

    大量Linux资源下载地址 关键词: 大量Linux资源下载地址 最近在网上找到了很多有关Linux方面的资源的下载地址,现提供给大家!大家千万不要客气啊,把需要的都给下载回去,(反正不是我的网站,下 ...

最新文章

  1. Android Studio 设置git 提交代码
  2. 技术有时间衰减因子.
  3. 新版gitbook导出pdf
  4. 2. python 参数个数可变的函数
  5. 【转】Windows版本,OS内核版本,Windows SDK之间的关系
  6. Kubernetes插件部署
  7. nginx安装错误:c compiler cc is not found
  8. 机器学习算法的差异_我们的机器学习算法可放大偏差并永久保留社会差异
  9. jclasslib插件_JVM-工具安装-jclasslib-win64
  10. linux进程地址空间内核,菜鸟求问linux进程地址空间问题
  11. 程序包com.wonhyoo.common.entity不存在, 找不到符号
  12. Vue:前端体系与前后端分离
  13. 为什么建议大家使用 Linux 开发?有那么爽吗?
  14. 在win10中注册老的的DLL文件
  15. 我的编程竞赛之路 ——中国大学生计算机编程第一人楼天城访谈
  16. Hbulider中,QQ分享到好友,总是提示,分享失败,请检查网络并重试
  17. ios开发 多人语音聊天_iOS语音通话功能实现流程(实时语音通话二)
  18. 计算机毕业设计springboot+vue+elementUI学生公寓管理系统
  19. 将组成字符串的所有 非英文字母的字符删除
  20. 使用everything批量修改文件名

热门文章

  1. Kronecker积及其等式性质
  2. 提高计算机存储性能的技术,提高电脑存储速度的方法
  3. 怎么打开华硕电脑计算机功能,华硕笔记本小键盘怎么开(笔记本电脑虚拟键盘怎么打开)...
  4. shell 脚本 基础
  5. u盘误删文件怎么恢复
  6. Google基本查询语法
  7. 如果电脑蓝屏0xc000021a怎么办
  8. 自动驾驶(十一)---------泰勒展开式、雅克比矩阵、主成分分析
  9. 用python和sympy库解决方程组问题_使用 Python 解数学方程
  10. 关于win11兼容性视图模式