When assigning an IP address to an interface, why do I need to specify a subnet mask? I understand the purpose of subnet masks in a routing table for example (to specify a range of IP addresses as destination). But why is it necessary on an interface? What uses it?

Why is it specified on the interface if it is already specified in the routing table?


6 Answers 6


The router uses the IP address and mask configured on an interface to determine what network is attached to that interface. That network and mask then become part of the routing table as a directly connected network.

There are three ways a router learns about networks:

  1. Directly connected networks
  2. Statically configured networks
  3. Networks learned through a routing protocol

A host will use the mask to determine if a destination address is on the same network as the host. If it is on a different network, the host must send the traffic, at layer-2, to the configured gateway instead of directly to the destination.

  • I understand how routing works and why netmasks are used in a routing table. What I don't understand is why I need to specify a netmask when giving an address to my interface.
    – Gradient
    Sep 4, 2016 at 1:07
  • I explained that. It lets the router figure out what the directly connected network is. How else do you think the router, or a host for that matter, will know what the directly connected network is?
    – Ron Maupin
    Sep 4, 2016 at 1:08
  • I was under the impression that a router doesn't have any info about how I configure my interfaces on my machine. My router sets his own routing table, which routes everything in (for example) to the local network. It doesn't know anything about the machines connected to it. Isn't that right?
    – Gradient
    Sep 4, 2016 at 1:15
  • As I explained, you enter an IP address and mask on a router so that it can figure out the directly attached network, and it enters that in its routing table. A host needs to figure out its directly attached network so that it knows if it needs to send traffic directly to the destination, or if it needs to send the traffic to its configured gateway. The router doesn't know anything about the hosts at layer-3.
    – Ron Maupin
    Sep 4, 2016 at 1:18
  • I might have been unclear in my question. My personal machine (the host) has an interface as well as a routing table. I do understand that its routing table need to be configured correctly (with subnet masks) to route packets as expected. What I don't get is why the interface (which is unrelated to the routing table) needs to know the subnet mask.
    – Gradient
    Sep 4, 2016 at 1:25

Super simple answer...

It tells the computer what range of addresses it can talk to before sending the traffic to the gateway to decide where it goes next.

The traffic in that range never goes to the gateway. Only traffic not in that range because the computer can't "talk" to anyone but the gateway outside its subnet range.

Little more details...

If your computer is it can talk to - without use of the gateway.

If the subnet was the computer could talk to - without a gateway.

Fyi... gateway is a router or layer 3 device like a switch or firewall that will also have a range it can talk to before sending traffic to its own gateway or 'next hop'

I hope the simple nature of this helps.


While all three answers are correct, they don't answer the actual question, which is

Why is it [the netmask] specified on the interface if it is already specified in the routing table?

When specifying the subnet mask on the interface, e.g. through ifconfig on linux, an entry is added to the routing table, which states that all hosts which have the same network prefix as this host are reachable without going through a gateway.

However, one could specify a wrong netmask on the interface (just for fun), and afterwards correct the entry in the routing table by hand.


  • set up IP and netmask with ifconfig
  • delete the route that is created
  • set up a new route, for network with netmask
  • lo and behold, my host can now reach the whole network on the same link, although I have a /24 IP

While I don't want to say that this approach makes a lot of sense, it clearly shows that the network mask on the interface is rather useless, apart from automatically setting up the on-link routing table entry.

In case there is any information I missed, I would be eager to hear about it!

  • 1
    I'm not sure your purpose of a Linux lesson when host/server configurations are off-topic here. Could you frame your answer in the context of a business-grade router that is on-topic.
    – Ron Maupin
    May 16, 2018 at 14:40
  • 1
    No I can't, as I don't own a business-grade router to test it.
    – curilan
    May 17, 2018 at 14:56

If a host sends traffic to say (google.com) it must know if is in the same LAN or if it is outside the LAN.

If your computer has a netmask: and IP addr, then it knows that the IPs to are in its same network, and thus won't need a gateway to reach an other network.

This is very important. For example: if you want to PING some host on your network (e.g., your computer will first send an ARP request like this one: Who has ? and it will wait for the ARP reply containing the MAC address of After that it will send a PING to with the MAC address of at the link layer.

On the other hand if you want to PING and address outside your LAN (e.g. your computer will not make an ARP request for MAC address but will instead send a PING to with the MAC address of default gateway at the link layer (e.g.


When you're using that interface to connect to another IP address, the subnet mask for an interface defines what addresses are "on this LAN" (or local) vs. what addresses are "out there", via a router.

By way of example, if you're on a computer with an interface with an IP address of , with a subnet mask of "/24" (or , that mask says that "to find hosts with, ask the ARP table, because they're on my lan" ... for all other addresses (so, outside of the mask), you should send the packets to the host designated in my routing table (the router), or the default route.


I strongly concur with or1on's answer here. I forgot if I have done the same experiment. It looks to me that what subnet mask value the interface has does not matter at all. The kernel only consults with the route table to determine if another IP is reachable via gateway, on-link or neither.

Maybe the reason that you set IP and net mask at the same time on the interface is just a way for you to set up route entry. Afterwards, only routing table matters.

I will come back with more answers.

By the way, I think the routing table in Unix is the equivalent of forwarding table in the context of switches. How interesting!

  • "I think the routing table in Unix is the equivalent of forwarding table in the context of switches." Not at all. Routing is between networks, so it uses network-layer addressing, e.g. IP, but bridging (switching) uses the link-layer addressing, e.g. MAC, and flooding for unknown addresses that routers simply cannot do without chaos.
    – Ron Maupin
    May 12, 2021 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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