Get a unique computer ID in Python on windows and linux


I’d like to get an id unique to a computer with Python on Windows and Linux. It could be the CPU ID, the motherboard serial, … or anything else.

I looked at several modules (pycpuid, psi, …) without luck.

Any idea on how to do that?

Asked By: darkpotpot



How about using the MAC address as unique id?

The discussion here Obtain MAC Address from Devices using Python shows how to obtain the MAC address

Answered By: Jay Zhu

I don’t think there is a reliable, cross platform, way to do this. I know of one network device that changes its MAC address as a form of hardware error reporting, and there are a million other ways this could fail.

The only reliable solution is for your application to assign a unique key to each machine. Yes it can be spoofed, but you don’t have to worry about it completely breaking. If you are worried about spoofing you can apply some sort of heuristic (like a change in mac address) to try and determine if the key has been moved.

UPDATE: You can use bacterial fingerprinting.

Answered By: mikerobi

There seems to be no direct “python” way of doing this. On modern PC hardware, there usually is an UUID stored in the BIOS – on Linux there is a command line utility dmidecode that can read this; example from my desktop:

System Information
        Manufacturer: Dell Inc.
        Product Name: OptiPlex 755                 
        Version: Not Specified
        Serial Number: 5Y8YF3J
        UUID: 44454C4C-5900-1038-8059-B5C04F46334A
        Wake-up Type: Power Switch
        SKU Number: Not Specified
        Family: Not Specified

The problem with MAC addresses is that usually you can easily change them programmatically (at least if you run the OS in a VM)

On Windows, you can use this C API

Answered By: Kimvais

for Windows you need DmiDecode for Windows (link) :

subprocess.Popen('dmidecode.exe -s system-uuid'.split())

for Linux (non root):

subprocess.Popen('hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.hardware.uuid'.split())
Answered By: nazca

Or if you don’t want to use subprocess, (It’s slow) use ctypes. This is for Linux non root.

import ctypes
from ctypes.util import find_library
from ctypes import Structure

class DBusError(Structure):
    _fields_ = [("name", ctypes.c_char_p),
                ("message", ctypes.c_char_p),
                ("dummy1", ctypes.c_int),
                ("dummy2", ctypes.c_int),
                ("dummy3", ctypes.c_int),
                ("dummy4", ctypes.c_int),
                ("dummy5", ctypes.c_int),
                ("padding1", ctypes.c_void_p),]

class HardwareUuid(object):

    def __init__(self, dbus_error=DBusError):
        self._hal = ctypes.cdll.LoadLibrary(find_library('hal'))
        self._ctx = self._hal.libhal_ctx_new()
        self._dbus_error = dbus_error()
        self._conn = self._hal.dbus_bus_get(ctypes.c_int(1),
        self._hal.libhal_ctx_set_dbus_connection(self._ctx, self._conn)
        self._uuid_ = None

    def __call__(self):
        return self._uuid

    def _uuid(self):
        if not self._uuid_:
            udi = ctypes.c_char_p("/org/freedesktop/Hal/devices/computer")
            key = ctypes.c_char_p("system.hardware.uuid")
            self._hal.libhal_device_get_property_string.restype = 
            self._uuid_ = self._hal.libhal_device_get_property_string(
                                self._ctx, udi, key, self._dbus_error)
        return self._uuid_

You can use this like:

get_uuid = HardwareUuid()
print get_uuid()
Answered By: Six

I found something else that I’m using. Mac address for linux, MachineGuid for windows and there is also something for mac.

More details here:

Answered By: darkpotpot

Invoke one of these in the shell or through a pipe in Python to get the hardware serial number of Apple machines running OS X >= 10.5:

/usr/sbin/system_profiler SPHardwareDataType | fgrep 'Serial' | awk '{print $NF}'


ioreg -l | awk '/IOPlatformSerialNumber/ { print $4 }' | sed s/"//g

BTW: MAC addresses are not a good idea: there can be >1 network cards in a machine, and MAC addresses can be spoofed.

Answered By: András Aszódi

But uuid.getnode return the same value as dmidecode.exe.

subprocess.Popen('dmidecode.exe -s system-uuid'.split())


import uuid    

Answered By: Alexandr Dragunkin

This should work on windows:

import subprocess
current_machine_id = subprocess.check_output('wmic csproduct get uuid').split('n')[1].strip()
Answered By: Souvik

For python3.6 and windows
must be used decode

>>> import subprocess
... current_machine_id = subprocess.check_output('wmic csproduct get uuid').decode().split('n')[1].strip()
... print(current_machine_id)
Answered By: Alexandr Dragunkin

2019 Answer (for Windows):

from typing import Optional
import re
import subprocess
import uuid

def get_windows_uuid() -> Optional[uuid.UUID]:
        # Ask Windows for the device's permanent UUID. Throws if command missing/fails.
        txt = subprocess.check_output("wmic csproduct get uuid").decode()

        # Attempt to extract the UUID from the command's result.
        match ="bUUIDb[srn]+([^srn]+)", txt)
        if match is not None:
            txt =
            if txt is not None:
                # Remove the surrounding whitespace (newlines, space, etc)
                # and useless dashes etc, by only keeping hex (0-9 A-F) chars.
                txt = re.sub(r"[^0-9A-Fa-f]+", "", txt)

                # Ensure we have exactly 32 characters (16 bytes).
                if len(txt) == 32:
                    return uuid.UUID(txt)
        pass # Silence subprocess exception.

    return None


Uses Windows API to get the computer’s permanent UUID, then processes the string to ensure it’s a valid UUID, and lastly returns a Python object ( which gives you convenient ways to use the data (such as 128-bit integer, hex string, etc).

Good luck!

PS: The subprocess call could probably be replaced with ctypes directly calling Windows kernel/DLLs for the Win32_ComputerSystemProduct API (which is what wmic uses internally). But then you have to be very careful and ensure that you call it properly on all systems. For my purposes this wmic-based function is safer and is all I need. It does strong validation and produces correct results. And if the wmic output is wrong or if the command is missing, our function returns None to let you handle that any way you want (such as generating a random UUID and saving it in your app’s config file instead).

Answered By: Mitch McMabers

I believe HDD ID is usually more unique than UUID:

serials = subprocess.check_output('wmic diskdrive get Name, SerialNumber').decode().split('n')[1:]
for serial in serials:
    if 'DRIVE0' in serial:
        return serial.split('DRIVE0')[-1].strip()

For improved uniqueness, you can combine both UUID and HDD ID:

def get_uuid() -> str:
    return subprocess.check_output('wmic csproduct get uuid').decode().split('n')[1].strip()

def get_hdd_id() -> str:
    serials = subprocess.check_output('wmic diskdrive get Name, 
    for serial in serials:
        if 'DRIVE0' in serial:
            return serial.split('DRIVE0')[-1].strip()

unique_id = get_uuid() + '-' + get_hdd_id()
Answered By: Anthony

I’d recommend using the machine’s native GUID, assigned by the operating system during installation. Since this question comes up relatively often, I wrote a small, cross-platform PyPI package that queries a machine’s native GUID called machineid.

Essentially, it looks like this, but with some Windows-specific WMI registry queries for an even more accurate ID. The package also has support for anonymizing the GUID via HMAC-SHA256.

import subprocess
import sys

def run(cmd):
    return, shell=True, capture_output=True, check=True, encoding="utf-8") 
    return None

def guid():
  if sys.platform == 'darwin':
    return run(
      "ioreg -d2 -c IOPlatformExpertDevice | awk -F\" '/IOPlatformUUID/{print $(NF-1)}'",

  if sys.platform == 'win32' or sys.platform == 'cygwin' or sys.platform == 'msys':
    return run('wmic csproduct get uuid').split('n')[2] 

  if sys.platform.startswith('linux'):
    return run('cat /var/lib/dbus/machine-id') or 
           run('cat /etc/machine-id')

  if sys.platform.startswith('openbsd') or sys.platform.startswith('freebsd'):
    return run('cat /etc/hostid') or 
           run('kenv -q smbios.system.uuid')

(Open to feedback and PRs!)

Answered By: ezekg

This is a quick and easy solution for Windows:

import subprocess
computer_id = subprocess.check_output("wmic bios get serialnumber").decode().split()[1]
Answered By: Basj
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.