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

What is the difference between Loopback Interface and Dummy Interface in Ubuntu 20.04

· 52CE 我爱测

I would like to know whats the differences between loopback and dummy interfaces in Ubuntu and how they behave. Because when I assign /24 IP address on Loopback interface and route this range in my network, the server responds to…

问题原文

原帖链接 · Mesbah Ahmadi · CC BY-SA 4.0

I would like to know whats the differences between loopback and dummy interfaces in Ubuntu and how they behave.

Because when I assign /24 IP address on Loopback interface and route this range in my network, the server responds to all IP addresses even to those ip addresses which is not assigned to the interface. But dummy interface behave like physical interface and responds to specific IP address which I've assigned to the interface.

已采纳解答

原帖链接 · A.B · CC BY-SA 4.0

Contrary to other interfaces, when the Linux kernel adds an address to the loopback interface, its LAN route as also added as type local and scope host (these are what matter) to the local routing table (this is what makes it harder to find), instead of the main routing table.

To check the difference, check all routing tables entries (table all) that were added by the kernel (proto kernel) and are of type local.

From a system with only lo up and nothing else done:

# ip route show proto kernel type local table all
local 127.0.0.0/8 dev lo table local scope host src 127.0.0.1 
local 127.0.0.1 dev lo table local scope host src 127.0.0.1 
# 

Here all addresses in the range 127.0.0.0/8 belong to the host (scope host).

Create dummy0 and an address on it:

# ip link add name dummy0 up type dummy
# ip addr add 192.0.2.2/24 dev dummy0

# ip route show proto kernel type local table all
local 127.0.0.0/8 dev lo table local scope host src 127.0.0.1 
local 127.0.0.1 dev lo table local scope host src 127.0.0.1 
local 192.0.2.2 dev dummy0 table local scope host src 192.0.2.2 
# ip route
192.0.2.0/24 dev dummy0 proto kernel scope link src 192.0.2.2 
# 

Instead add it on lo

# ip addr flush dev dummy0
# ip addr add 192.0.2.2/24 dev lo

# ip route show proto kernel type local table all
local 127.0.0.0/8 dev lo table local scope host src 127.0.0.1 
local 127.0.0.1 dev lo table local scope host src 127.0.0.1 
local 192.0.2.0/24 dev lo table local scope host src 192.0.2.2 
local 192.0.2.2 dev lo table local scope host src 192.0.2.2 
# ip route
# 

This time 192.0.2.0/24 is of scope host, making the whole range belong to the host just as 127.0.0.0/8, instead of only 192.0.2.2 (/32) in the previous results. Linux following the Weak Host Model, will answer for these addresses for requests made on any interface, including ARP requests when relevant.


There are multiple ways to obtain the same results. So what can or should be used depends on the goal to achieve. pro and con below aren't always pro and con, but that's to compare them.

  • dummy0

    • pro (for something else than dummy)

      • if routing/forwarding is enabled, the kernel, having by default added an automatic (proto kernel) LAN route will route received packets to this interface if not its own address. Doesn't make much sense for a dummy interface though, since traffic will be discarded. Yet using a dummy interface could be one way to blackhole a route (one other more permanent way being to add a route of type blackhole which doesn't rely on an interface).
    • con

      • one address. Multiple commands/operations are needed to add multiple addresses, one per command.
  • lo

    • pro

      • add a whole range of host addresses in one swoop

        This should be reserved to projects related to intercepting traffic on a large scale. Note that what matters is the route added by the kernel, not the address itself. Such route is used for example by Linux' tproxy for transparent proxying:

        # ip rule add fwmark 1 lookup 100
        # ip route add local 0.0.0.0/0 dev lo table 100
        

        but with specific policy-routing rules in place so it doesn't always apply.

        Outside of this, just add /32 addresses when using lo if it's ever done.

      • permanent interface, neutral role

        This interface is guaranteed to exist, so can be used as an anchor to add addresses in advance without knowing what interface will exist or should be used. The routes related to this address won't change when an interface goes down, since it's on lo (which has no reason to go down). Some routing daemons might handle in a special way addresses added on the loopback interface.

    • con

      • cannot be used for static routing/forwarding

        Routes added for the lo interface are not meant to route elsewhere: the target is the host.

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

资料来源与适用范围

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

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

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