2

When running ip addr add to assign an address to a network interface, you need (or can) specify the subnet of the interface. So then when you run ifconfig you can see the subnet:

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::f36f:407:e015:2633  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:73:e9:14  txqueuelen 1000  (Ethernet)
        RX packets 105172  bytes 120417336 (120.4 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27040  bytes 4403397 (4.4 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

I don't understand why is this necessary? I don't believe it's for routing purposes, since the routing table is used for this, so what is it good for?

EDIT: to be more clear: I know that the netmask is used to know the range of addresses that are directly linked to the network, i.e., can be reached without routing. But that's set in the routing table. So my question is: what's the relation between the setting of the interface and the routing table.

It's seems that they're not directly connected, see this:

root@yoav-VirtualBox:/home/yoav# ip addr add 192.168.1.2/24 dev first
root@yoav-VirtualBox:/home/yoav# ip addr
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: second@first: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether a2:a9:4f:c3:c5:be brd ff:ff:ff:ff:ff:ff
3: first@second: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether f2:38:32:ea:b4:8f brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/24 scope global first
       valid_lft forever preferred_lft forever
root@yoav-VirtualBox:/home/yoav# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
root@yoav-VirtualBox:/home/yoav# 

3 Answers 3

2

On Linux, assigning an address to an interface adds an implicit LAN route based on this address. This route will use the CIDR mask assigned to the address (as well as the address itself as a hint source address):

# ip link set eth0 down
# ip address flush dev eth0
# ip address add 192.0.2.3/24 dev eth0
# ip route

(nothing: the route requires the interface to be admistratively UP too)

# ip link set eth0 up
# ip route
192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.3 

Removing the address always removes routes that can't exist because of the lack of referenced addresses or networks defined by the address.

# ip address flush dev eth0
# ip route

(nothing)

It's possible for some special network settings (often involving other interfaces using clashing addresses) to avoid this implicit route to be added by using the flag noprefixroute. Tools like NetworkManager often set this flag so they have better control on the way they configure routes.

# ip address add 192.0.2.3/24 dev eth0 noprefixroute
# ip route

(nothing)

This implicit LAN route might not exist on all OSes and might have to be always explicitly set on some of them.

1

The subnet specifies which hosts can be reached through this interface, without routing. In your case, enp0s3 can be used to contact any host from 10.0.2.0 to 10.0.2.255 (I’m ignoring impossible values here since the definition of impossible is evolving), without having to use a gateway (which is where routing comes into the picture).

1
  • please refer to the edit of the question
    – YoavKlein
    Dec 18, 2021 at 16:31
-1

routing table needs to know whether this packet should be routed or whether it should be delivered to a host connected to the same "link"/subnet directly using underlying layer 2 protocol.

Note: if the packet should be routed, then it is delivered to its next hop/gateway using underlying layer 2 protocol. So basically the routing table tells to layer 2 which next-hop IP to use, which is either the same as the packet (if it is in the same subnet) or next hop in the routing table. So, routing table needs the mask to determine that.

1
  • this is not my question.
    – YoavKlein
    Dec 18, 2021 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.