uuid.getnode

Depends on the machine, so this page has no live demo. On Unix the first call may run an external program (ip or ifconfig on Linux; ifconfig, arp or netstat on macOS) and can be slow; on Windows a system call always succeeds. The result is cached for the rest of the process.

uuid functionPython 2.5+
Common call
uuid.getnode()
Returns
an int below 2**48, e.g. 0x112444be1e for 00:11:24:44:be:1e
Replaces
Parsing ipconfig / ifconfig output yourself
Watch out
May be a random number, not a real address: check the multicast bit
uuid.getnode()
→ int

Common patterns

MAC address as text
Six colon-separated bytes.
import uuid
mac = ':'.join(f'{b:02x}' for b in uuid.getnode().to_bytes(6))
Was it a real address?
The random fallback sets the multicast bit.
import uuid
is_random = bool(uuid.getnode() & (1 << 40))

Examples

1. An int below 2**48
import uuid x = uuid.getnode() (type(x).__name__, 0 <= x < 2**48)
Returns
('int', True)
2. Cached: the same every call
import uuid uuid.getnode() == uuid.getnode()
Returns
True
3. Format as a MAC address
import uuid node = 0x00112444be1e ':'.join(f'{b:02x}' for b in node.to_bytes(6))
Returns
'00:11:24:44:be:1e'
4. Random fallback? Test the multicast bit
import uuid node = 0x00112444be1e bool(node & (1 << 40))
Returns
False
5. Universal or local address
import uuid node = 0x00112444be1e bool(node & (1 << 41))
Returns
False

Pitfalls

1. Formatting with hex() and losing leading zeros
hex() drops leading zero bytes, so the text is shorter than 12 digits. Format with a fixed width.
hex(node)
node = 0x00112444be1e
hex(node)
'0x112444be1e'
f'{node:012x}'
node = 0x00112444be1e
f'{node:012x}'
'00112444be1e'
2. Treating getnode() as a real MAC address
If no address is found, getnode() returns a random number with the multicast bit (1 << 40) set, as RFC 4122 recommends. Real unicast addresses have that bit clear.
trust it
random_node = 0x0300deadbeef
f"{random_node:012x}"
'0300deadbeef'
check the bit
random_node = 0x0300deadbeef
bool(random_node & (1 << 40))
True

When to use

Use it
  • Default node for uuid1 (it is called for you)
  • A quick machine identifier for logs on a single host
Reach for something else
  • Reliable network interface information → psutil or the platform tools
  • Anything privacy-sensitive: it is the network card address

Notes

CPython impl
getnode() in Lib/uuid.py tries platform getters in order (on Windows _windll_getnode via the _uuid extension, which always succeeds; on Linux the _uuid extension when built with libuuid, then ip and ifconfig output; on macOS ifconfig, arp and netstat), prefers universally administered addresses (bit 1 << 41 clear), and falls back to _random_getnode(): random.getrandbits(48) | (1 << 40)
Cache
The result is stored in the module global _node, so later calls return the same value
Availability
All platforms; the value is machine-specific and can change when network hardware changes
3.7
Since 3.7 universally administered MAC addresses are preferred over locally administered ones (docs: "Changed in version 3.7")

FAQ

uuid.getnode() returns it as a 48-bit int; ':'.join(f'{b:02x}' for b in uuid.getnode().to_bytes(6)) formats it. With several interfaces, a universally administered one is preferred but there is no other ordering guarantee.