Networking – Private IP vs MAC Address

ipmac addressrouterroutingswitch

What I know is that when we connect to another IP, our Public IP gets shared to the outside world, and when they address us they use our Public IP (gateway), and then the gateway directs the information to our devices using our MAC addresses, I have 2 questions:

1- If MAC addresses are used to find our devices in a network, what's the benefit of Private IP? if both of them are not shared with the outside world why do we need both of them?

2- If someone wants to address another person, they would only have the public IP of the receiver (which would be the router that contains multiple devices) how would the router knows where to direct these information? does the sender specify the local IP address of the receiver in a network or how do the sender tells the router which device to send the information to?

Best Answer

1- If MAC addresses are used to find our devices in a network, what's the benefit of Private IP? if both of them are not shared with the outside world why do we need both of them?

Private IP addresses (per RFC 1918) are a workaround to delay IPv4 address exhaustion.

MAC addresses are used to forward local traffic to the desired end node. While private IP addresses seemingly serve the same purpose, the difference is that MAC addresses are physical addresses used by the (usually) Ethernet hardware to direct traffic in the LAN, and IP addresses are logical addresses that allow you to build a larger network than a single LAN segment - public or private.

Put another way, MAC addresses are meaningful inside a local LAN segment while private addresses are meaningful inside a private, coordinated network that can even span the globe. Private IPs also allow you to communicate with the Internet through address translation (NAPT) or proxies.

Note that Ethernet and TCP/IP were developed independently of each other and serve different purposes. IP is a layer-3, routable network that uses the underlying Ethernet as a local, layer-2 transport vehicle. IP can also use other layer-2 networks, even when they don't use MAC addresses.

2- If someone wants to address another person, they would only have the public IP of the receiver (which would be the router that contains multiple devices) how would the router knows where to direct these information?

Public IP addresses are routed throughout the entire Internet. Usually, you submit the packet to your ISP and they know where to send it (by global exchange of routing prefixes).

Note that routing is a refining process where each router only knows a subset of all possible routes. A router near the source address knows the source network in detail but little about the destination network - in extreme it just uses the default route. From hop to hop, each router knows less about the source and more about the destination network until the final router knows the exact interface the destination is connect on (its local segment).

does the sender specify the local IP address of the receiver in a network or how do the sender tells the router which device to send the information to?

If the destination's IP address is private it is meaningless outside its private network. A sender on the public Internet needs to use the public IP as destination. At the (public) end, the public IP is translated by a router that knows how to reach the final destination.

Most often, port forwarding or destination NAT is used where the router translates a packet destined for e.g. TCP port 80 on public IP 11.22.33.44 to TCP port 8080 on private IP 192.168.33.44. Since that router is part of the public Internet as well as the private network it can route the packet to its private IP destination.