IP Address vs MAC Address
Two Identities Every Packet Needs
LinkedIn Hook
Every developer thinks they know the difference between an IP address and a MAC address.
Most of them are wrong about at least one thing.
Here's what actually trips people up in networking interviews:
Your IP address is like your home address — it tells the post office where to deliver the package. But your home address can change when you move.
Your MAC address is like your passport number — it's tied to YOU, not to where you live. It never changes (in theory).
But here's what most people miss:
When you send a request to google.com, your IP address travels the entire route across the internet. Your MAC address? It never leaves your local network. It gets swapped at every router hop.
That's what ARP is for — and it's a dead-common interview topic.
I broke down exactly how both work, where ARP fits in, and why you need both to send a single packet — in Lesson 1.3 of my Networking Interview Prep course.
Read the full lesson → [link]
#Networking #WebDevelopment #InterviewPrep #ComputerNetworks #BackendDevelopment #SoftwareEngineering
What You'll Learn
- The difference between IP addresses (logical, changeable) and MAC addresses (physical, permanent) and which OSI layer each belongs to
- How IPv4 addresses are formatted and what public vs. private IP ranges mean
- How MAC addresses are structured and why they never leave your local network
- How ARP (Address Resolution Protocol) bridges IP and MAC to deliver packets on a local network
The Two Identities of a Device on a Network
Think about how mail delivery works.
When you order a package, the courier needs two things: your home address (so the package can be routed through the postal system to the right city, street, and building) and your name or apartment number (so the building attendant knows exactly whose hands to put it in once it arrives).
Your home address can change — you move to a new city, you get a new address. But your name doesn't change just because you moved.
This is exactly how IP addresses and MAC addresses work in networking:
- IP Address = your home address. Logical, assigned by the network, can change every time you connect.
- MAC Address = your passport number. Physical, burned into the hardware, permanently tied to your network card.
Both are required to deliver data. They just operate at different stages of the journey.
IP Address — The Logical Address
What It Is
An IP address (Internet Protocol address) is a logical address assigned to a device on a network. It identifies where a device is located in the network topology — not what the device is, but where it currently lives.
IP addresses operate at Layer 3 (Network Layer) of the OSI model. Routers use IP addresses to forward packets from one network to another across the internet.
IPv4 Format
The most common version in use today is IPv4, which uses a 32-bit address written in dotted decimal notation — four groups of numbers from 0 to 255, separated by dots:
192.168.1.10
Each group is called an octet (8 bits). Four octets = 32 bits total. That gives us roughly 4.3 billion possible addresses — a number that turned out to be far too small, which is why IPv6 was created (128-bit addresses), but that is a topic for a later lesson.
Why IP Addresses Change (DHCP)
IP addresses are typically assigned dynamically by a DHCP server (Dynamic Host Configuration Protocol). When your laptop connects to a Wi-Fi network, the router's DHCP server hands your device an available IP address on a lease. When you disconnect, that IP may be given to a different device.
This is why your IP address is considered logical and temporary — it reflects your current position in the network, not a fixed identity.
Public vs. Private IP Ranges
Not all IP addresses are the same kind:
Private IP ranges (used inside local networks — your home, office, data center):
| Range | Example |
|---|---|
10.0.0.0 – 10.255.255.255 | 10.0.0.5 |
172.16.0.0 – 172.31.255.255 | 172.16.0.1 |
192.168.0.0 – 192.168.255.255 | 192.168.1.100 |
Public IP addresses are assigned by your ISP and are globally routable across the internet. Your home router has a single public IP facing the internet, and all your devices share it through a process called NAT (Network Address Translation).
When you check "what is my IP" on Google, you see your public IP — your router's address, not the private IP of your laptop.
MAC Address — The Physical Address
What It Is
A MAC address (Media Access Control address) is a hardware address permanently assigned to a device's network interface card (NIC) by the manufacturer. It identifies the device itself — not where it is on a network, but what it physically is.
MAC addresses operate at Layer 2 (Data Link Layer) of the OSI model. Switches use MAC addresses to forward frames within a local network (LAN).
Format
A MAC address is 48 bits written as six pairs of hexadecimal digits, separated by colons (or dashes or no separator depending on the OS):
AA:BB:CC:DD:EE:FF
Example of a real MAC address:
3C:22:FB:A3:1D:09
The first three pairs (3C:22:FB) identify the manufacturer (this is called the OUI — Organizationally Unique Identifier). The last three pairs are the unique device identifier assigned by the manufacturer.
Burned into Hardware
MAC addresses are stored in firmware on the network card and are meant to be globally unique. They are assigned at the factory — hence the term "burned-in address" (BIA). Unlike IP addresses, a MAC address does not change just because you join a different network.
However, modern operating systems allow MAC address spoofing — software can override the MAC address the OS reports. This is used for privacy (iOS and Android randomize MAC addresses on Wi-Fi scans), but the hardware BIA still exists underneath.
MAC Addresses Stay Local
Here is the critical point most people miss: MAC addresses never travel beyond your local network.
When a packet leaves your router and hops across the internet to a server in another country, your laptop's MAC address is not on that packet anymore. At each router hop, the packet's MAC address is stripped off and replaced with the MAC of the next hop. Only the IP address stays constant across the full route.
This is why MAC addresses cannot be used to route traffic globally — they have no meaning outside the local network segment they belong to.
ARP — The Glue Between IP and MAC
The Problem ARP Solves
Imagine your laptop wants to send data to another device on your local network — say, your printer at 192.168.1.50. Your laptop knows the printer's IP address. But to actually deliver the data frame on the local network, your laptop needs the printer's MAC address.
How does it find out? This is where ARP (Address Resolution Protocol) comes in.
How ARP Works
-
Your laptop broadcasts an ARP request to every device on the local network: "Who has IP address 192.168.1.50? Tell 192.168.1.10."
-
Every device on the network receives this broadcast. Only the device with IP
192.168.1.50responds: "That's me. My MAC address is AA:BB:CC:DD:EE:FF." -
Your laptop caches this IP-to-MAC mapping in its ARP table (also called ARP cache) so it doesn't have to ask again for a while.
-
Now your laptop can build the full frame with the correct destination MAC address and send it.
ARP operates at the boundary between Layer 2 and Layer 3. It is what makes IP addressing and MAC addressing work together seamlessly on a local network.
Code Examples
Code Example 1 — Finding Your IP and MAC Address
You can inspect your network interfaces directly from the terminal or from a Node.js script. Here is how to do both.
Terminal commands (works on most systems):
# On Linux / macOS — show all network interfaces with IP and MAC
ip addr show
# On macOS — alternative
ifconfig
# On Windows (Command Prompt or PowerShell)
ipconfig /all
# View the ARP cache — see IP-to-MAC mappings your machine has learned
arp -a
Node.js — read network interfaces programmatically:
const os = require('os');
// os.networkInterfaces() returns an object keyed by interface name
// Each interface has an array of address objects (IPv4, IPv6, etc.)
const interfaces = os.networkInterfaces();
for (const [name, addresses] of Object.entries(interfaces)) {
console.log(`\nInterface: ${name}`);
for (const addr of addresses) {
// Skip internal loopback addresses (127.0.0.1, ::1)
if (addr.internal) continue;
if (addr.family === 'IPv4') {
// This is the device's local IP address on this interface
console.log(` IP Address (IPv4): ${addr.address}`);
// The MAC address is stored in addr.mac
// Format: six hex pairs separated by colons e.g. 3c:22:fb:a3:1d:09
console.log(` MAC Address: ${addr.mac}`);
}
}
}
Sample output:
Interface: eth0
IP Address (IPv4): 192.168.1.42
MAC Address: 3c:22:fb:a3:1d:09
Interface: wlan0
IP Address (IPv4): 192.168.1.87
MAC Address: a4:c3:f0:11:22:33
Notice that your IP address is in the private 192.168.x.x range — this is the address your router assigned via DHCP. Your MAC address is the hardware identifier of your Wi-Fi or Ethernet card.
Code Example 2 — Why Both IP and MAC Are Needed to Send a Packet
This example illustrates the conceptual two-stage addressing that happens every time you send data on a local network.
// Conceptual model — not actual network code, but shows the logic clearly
// Scenario: Your laptop wants to send an HTTP request to your local API server
const myDevice = {
ip: '192.168.1.10', // Assigned by DHCP — logical, can change
mac: '3c:22:fb:a3:1d:09', // Hardware address — permanent
};
const apiServer = {
ip: '192.168.1.50', // The destination IP we know (from DNS or config)
mac: null, // We don't know this yet — ARP will tell us
};
// Step 1: Check if destination IP is on the same local network
// (same subnet — simplified check here)
const sameSubnet = (ip1, ip2) => ip1.split('.').slice(0, 3).join('.') === ip2.split('.').slice(0, 3).join('.');
if (sameSubnet(myDevice.ip, apiServer.ip)) {
console.log('Destination is on the local network.');
console.log('Step 1: ARP — broadcast to find MAC address for', apiServer.ip);
// Simulated ARP reply — in reality this comes from the target device
apiServer.mac = 'aa:bb:cc:dd:ee:ff';
console.log('Step 2: ARP reply received — MAC is', apiServer.mac);
// Now we can build a complete Layer 2 frame
const frame = {
// Layer 3 (IP header) — stays the same across the whole internet route
sourceIP: myDevice.ip,
destinationIP: apiServer.ip,
// Layer 2 (Ethernet header) — only relevant on THIS local network segment
sourceMAC: myDevice.mac,
destinationMAC: apiServer.mac, // <-- This is what ARP gave us
payload: 'GET /api/users HTTP/1.1',
};
console.log('\nComplete frame ready to send:');
console.log(frame);
} else {
// If the destination is outside the local network (e.g., google.com)
// we send the frame to the router's MAC address instead
// The IP header still has the final destination IP (Google's server)
// The router will handle forwarding and will swap MAC addresses at each hop
console.log('Destination is outside local network.');
console.log('Send frame to router MAC, but keep destination IP as', apiServer.ip);
}
Key insight from this example:
- The IP address (Layer 3) identifies the final destination and stays on the packet for the entire journey across the internet.
- The MAC address (Layer 2) only identifies the next hop on the local segment. It gets replaced at every router along the route.
- Without ARP resolving the MAC, the packet cannot be delivered even on a local network — you would know the building address but not which door to knock on.
Common Mistakes
-
"Your MAC address is globally unique on the internet." This is half-true but misleading. MAC addresses are meant to be globally unique in theory, but they are completely invisible outside your local network. NAT hides all private devices behind a single public IP, and routers replace MAC addresses at each hop. A server in another country sees your router's public IP — it never sees your laptop's MAC address.
-
"My private IP address is my MAC address, or they are related." These are completely separate identifiers. Your IP address (
192.168.1.10) is assigned by your router's DHCP server and can change. Your MAC address (3c:22:fb:a3:1d:09) is burned into your network card at the factory. One is logical and network-assigned; the other is physical and hardware-assigned. They happen to coexist on the same device, but one does not determine the other. -
"ARP is only for the internet." ARP operates exclusively on local networks (LAN). It is a Layer 2 protocol and cannot cross routers. When you send a request to a server on the internet, your machine uses ARP only to find your router's MAC address (the gateway) — not the destination server's MAC. The destination server's MAC never appears in your ARP table.
Interview Questions
Q: What is the difference between an IP address and a MAC address?
An IP address is a logical address at Layer 3 (Network Layer) that identifies a device's location in a network. It is assigned by DHCP and can change. A MAC address is a physical hardware address at Layer 2 (Data Link Layer) that is burned into the NIC and permanently identifies the device itself. IP addresses are used for routing across networks; MAC addresses are used for delivering frames within a local network segment.
Q: What is ARP and why is it needed?
ARP (Address Resolution Protocol) resolves an IP address to its corresponding MAC address on a local network. When a device knows the destination IP but needs the MAC to build a Layer 2 frame, it broadcasts an ARP request. The device with that IP replies with its MAC address. Without ARP, a device could not deliver frames on the local network even when the destination IP is known.
Q: Does your MAC address travel across the internet when you visit a website?
No. MAC addresses are local to each network segment. When your frame leaves your local network through your router, the router strips the original MAC addresses and replaces them with its own MAC (as source) and the next router's MAC (as destination). This continues at every hop. The destination server only ever sees the last router's MAC address, never your device's MAC. What travels the full route is the IP address in the Layer 3 header.
Q: What are the private IP address ranges, and why do they exist?
The private IP ranges are 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16. They exist because IPv4 has only ~4.3 billion addresses — not enough to give every device a unique public IP. Private ranges allow organizations and homes to use any addresses they like internally, with NAT translating outbound traffic to a single public IP. Private IPs are not routable on the public internet.
Q: Can a MAC address be changed?
The hardware MAC address (burned-in address) is set by the manufacturer and stored in the NIC firmware. However, operating systems allow MAC spoofing — the OS can present a different MAC to the network without changing the hardware. Android and iOS use MAC randomization for Wi-Fi scanning to prevent tracking. This means the MAC your device advertises may not be its true hardware address, but the original BIA still exists in the firmware.
Quick Reference — Cheat Sheet
| Aspect | IP Address | MAC Address |
|---|---|---|
| Full name | Internet Protocol Address | Media Access Control Address |
| OSI Layer | Layer 3 — Network Layer | Layer 2 — Data Link Layer |
| Type | Logical / Virtual | Physical / Hardware |
| Assigned by | DHCP server (or manually) | Manufacturer (burned into NIC) |
| Changes? | Yes — changes per network/lease | No — permanent (spoofing is software-only) |
| Format | Dotted decimal: 192.168.1.10 | Hex pairs: AA:BB:CC:DD:EE:FF |
| Bit length | 32 bits (IPv4) / 128 bits (IPv6) | 48 bits |
| Scope | Global (public) or local (private) | Local network only |
| Used by | Routers — to forward packets | Switches — to forward frames |
| Travels across internet? | Yes — stays in IP header end-to-end | No — replaced at every router hop |
| Protocol that links them | ARP (on local network) | ARP (responds to IP lookups) |
| View command (Linux/Mac) | ip addr / ifconfig | ip addr / ifconfig |
| View command (Windows) | ipconfig | ipconfig /all |
| ARP cache command | arp -a | arp -a |
Previous: Lesson 1.2 — Network Types (LAN, WAN, MAN) → Next: Lesson 2.1 — The OSI Model Overview →
This is Lesson 1.3 of the Networking Interview Prep Course — 8 chapters, 32 lessons.