Why do we need to give IP addresses to each interface? Wouldn't giving one to each device be enough?
Connecting an interface to a network makes it a part of that network. Therefore, the IP address is a property of the connection, not the host.
Likewise, a host can have many network connections and accordingly, many IP addresses. Different interfaces or addresses often have different functions, so it's important to distinguish between them (e.g. internal console, public services, iSCSI).
Routers require multiple IP addresses for their interfaces.
However, there are scenarios when you'd like to assign an address to the actual device behind its interfaces - e.g. when you've got redundant links/interfaces and you don't really care which path a connection takes. Then you can use a virtual loopback interface inside the device which is always linked and up.
Using addresses to generally identify hosts instead of interfaces creates the problem that you'd need to propagate information on which host is reachable from which network. How do you handle multi-home nodes like routers or other gateways? How about hosts moving between networks (e.g. mobile devices)? That very quickly causes problems that severely limit scalability.
It would not be enough.
Suppose I have a computer with three interfaces:
eth0 (wired Ethernet),
wlan0 (wifi), and
vboxnet0 (virtualbox). One of the interfaces is connected to an internal network, one is connected to the internet, and the last one is connected to a network of virtual computers. Let's say I have just one address, 10.1.2.3, and wish to send a whole lot of packets to 192.168.1.2, reachable on one of those networks - where do I send them to? Can't just send them Everywhere, such behavior would flood all the networks in short order.
But if the eth0 interface has 192.168.1.3, wlan0 has 10.1.2.3, and vboxnet0 has 184.108.40.206, then the default routing table will probably say "send it out eth0". (This can obviously get far more complicated with more complex routing rules).
And conversely, I may wish to run a service only on the interface that's open to the private network - so when a request comes in on a different interface, it's not handled at all.
A device that has an IP address exists within a network.
A Router is a device who's primary purpose is to pass traffic between networks.
For a Router to pass packets between two networks, it must exist within both networks.
The Router will have an "arm" inside each network -- or as we call it, an interface. And the way an interface exists within a network is by assigning it an IP address within the network.
Hence, when configuring a router, each interface receives an IP address to identify the networks which that Router belongs within.
Disclaimer: The link above is to my blog. My blog is not monetized. I make no profit from you reading it. I am providing a link simply for your (and any other reader's) benefit.
Case 1: Routers
Theoretically this would be possible.
However, normally an IP packet is sent "directly" to the destination when the IP address "matches" a certain network mask (e.g. 10.0.0.0/28); otherwise the packet is sent over a router.
- A router connects two networks; each of the two networks has a network mask
Packets being sent from one computer to another within one of the two networks do not pass a router.
This means that the IP addresses of each computer match the network mask of the network they are connected to.
Packets being sent to the router (this includes packets being routed by the router!) are not sent from a computer to the router via a second router.
This means that the IP address of the router must match the network masks of both networks.
IP packets being sent from one computer in one network to a computer in the other network however are sent over the router.
This means that the IP addresses of the computers in one network must not match the network mask of the other network.
It is nearly impossible to choose an IP address and two network masks in a way that one IP address matches both network masks but a lot of IP addresses only match one network mask.
Case 2: Different private networks
We could have the case that a computer is connected to two private networks which cannot exchange data between each other.
In this case a computer may have the same IP address in both networks.
In the case of IPv4 most OSs won't support this because the OSs use the network masks of the two networks to distinguish between them. The networks need to have different network masks...
In the case of IPv6 (using "link-local" addresses) a computer can have the same (link-local) IP address in two different networks - and therefore two network cards can have the same IP address!
I was wondering, why do we need to give IP addresses to each interface? Wouldn't giving to each device enough?
Let me start by challenging your assumption. Why do you say this isn't how machines behave? Say I assign 192.168.1.1/24 to eth1 and 192.168.2.1/24 to eth2. Other than installing a route for 192.168.1.0/24 out eth1 and 192.168.2.0/24 out eth2 and determining the preferred source IP address for packets sent along that route, how much does it really matter which interface I assign the IP address to? What really changes? In what sense does the machine not behave as if all IP addresses assigned to interfaces on the machine belong to the machine?
Both approaches are used. The most common approach is actually a hybrid of these two approaches.
On the "give a device an IP address" extreme, you could imagine a device that behaved as if all of its interfaces were connected to a filtering bridge with one IP address assigned to the bridge.
On the "give each interface an IP address" extreme, you could imagine a device that behaved as if each interface it had was like a separate machine. (See here if you think that's how devices currently work or think about someone who connects to an IP address assigned to one interface but the packets arrive on and are sent out another one.)
In practice, most machines operate somewhere in-between. They do act as if all IP addresses were owned by the machine. The assignment of an IP to a particular device doesn't really do all that much beyond telling the OS to install a default route out that interface and to set the default source IP address for packets sent out that interface where the source IP address isn't forced.
Otherwise, they do behave as if all IP addresses belonged to the machine. The way a packet is processed doesn't depend much on what interface it is received on -- packets with a source IP address assigned to one interface received on another are routine. Which interface an address is assigned to has no direct effect on which interface a packet is sent out, the routing table determines that.
In the general case, you do need one IP per local network you connect to, and this is just how TCP/IP was defined: each host on a given local network has an IP address, which allows:
- to route traffic to the appropriate local network, based on the destination IP address
- to direct traffic to the appopriate device on that local network (after an ARP lookup on 802.x local networks, for instance).
As long as you have local networks with multiple devices connected to it (most 802.x local networks, including Ethernet, Wi-Fi), it's quite difficult to go around that unless you fundamentally change the way TCP/IP works.
However, it is actually possible to avoid that, though it is quite specific.
Consider a network that only has point-to-point links (point-to-point Ethernet between two devices without a switch or hub, DSL links, SONET/SDH links, Frame Relay or ATM VCs...).
The usual convention is to use a /30 for each link, so the device at each end has an IP address on that link.
But you can use "IP unnumbered", and not associate any IP addresses to those links. Now, you assign an IP to the device's loopback interface (any interface really, but loopback is the easiest one for this purpose), and you use a dynamic routing protocol (IS-IS, OSPF, EIGRP...). This routing protocol will advertise how to route traffic to that IP address via the various unnumbered links.
IP addresses include routing information. An IP address is split up into two components, the network number and the host number, based on an assigned prefix length (which was originally expressed as a network mask, and this is still often how it's implemented internally in network stacks).
For routing to work properly, all the devices connected to a particular network must have addresses with the same network number. Senders determine whether the receiver is on the same or a different network by comparing their own network number with the receiver's address. If they're in the same network, they send directly; otherwise, they send to a router whose job is to get the message closer to the destination.
So if a device is connected to multiple networks, it needs an address on each of those networks so it can be reached by devices on that network.
That said, it isn't really necessary for the addresses to be assigned specifically to particular network interfaces. The device could just have a list of all the IPs that it owns in a single table. But the interfaces still need information about which network they're connected to. By assigning an IP and network mask to each interface we put this information in one place, simplifying the design. There's no need for extra code to keep the list of IPs consistent with the list of networks that interfaces are connected to.
It might be enough to have one IP in the standard case, but as highlighted by other answers, there are many cases where it's not.
Other answers have already mentioned the case of a router. One mentions virtualbox, which is the case of virtual machines: you may be running on a single physical device several virtual ones. Another case is VLANs, you might have actually, connected to a single network card, several different virtual LANs, with different IP addresses. And you could even for some reasons have an interface with multiple IPs, for instance because you want to run several web servers on TCP port 80. Such a configuration isn't uncommon for servers, the hardware you rent may have a single network interface card, but you will have several IP addresses configured.
Lets make this very simple:
A device needs an IP address to communicate with other hosts and forward packets.
Hosts know how to route traffic locally based on the mac/ip/interface binding database known as a arp table.
The arp table needs to resolve the destination IP to a mac address via an ARP request in order to populate this arp table on the local device.
Therefore logic would dictate that in order to resolve a ip address to a mac address, an IP must be present on an interface in order to communicate with it and populate the arp response to be sent back to origional hosts arp request. This is also essential for IP based internet connectivity on the internet.
Most of these bindings are all typically for communicating within the local network, if a NAND logical operation is done within the host, and the destination is found to be in a different network, it sends the traffic to the default gateway and has the upstream router handle routing the traffic to the foreign network.
Hope that helps.