大网数据广告活动
服务器运维问答

How does MASQUERADE choose an IP address if there are multiple?

· 52CE 我爱测

If I set up a Linux iptables MASQUERADE rule for traffic going out of a specific interface, but that interface has multiple IP addresses, how does the source IP get chosen? As an example, let's suppose I add a rule like: $ iptabl…

问题原文

原帖链接 · obeattie · CC BY-SA 3.0

If I set up a Linux iptables MASQUERADE rule for traffic going out of a specific interface, but that interface has multiple IP addresses, how does the source IP get chosen?


As an example, let's suppose I add a rule like:

$ iptables -t nat -A POSTROUTING -o eno1 -j MASQUERADE

…and that interface looks like:

$ ip addr show dev eno1
1: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 94:18:82:35:a2:c1 brd ff:ff:ff:ff:ff:ff
    inet 10.136.122.97/24 scope global eno1
       valid_lft forever preferred_lft forever
    inet 10.136.122.98/24 scope global eno1
       valid_lft forever preferred_lft forever

社区高分解答

原帖链接 · Anton Danilov · CC BY-SA 4.0

The masquerade selects an address in the same way as the source address is selected in routing.

So, you can use ip route get <dst> command to determine the address what will be used as the source address after masquerading.

If you interested in more details, you can look into the source code.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-08-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。