Bastianon Massimo
Bastianon Massimo

Reputation: 1742

How to get hard disk serial number compatible with linux/windows

Is there a way to get primary hard disk serial number independently from the OS? In windows is quite easy with WMI or DllImport but I can't find any documentation about linux.. Alternatively is there a way to get a cross platform unique machine id? P.S. I don't care about virual machine cloning

Upvotes: 3

Views: 1932

Answers (2)

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30625

In linux terminal you can use the command below in order to get SERIAL of the HDD.

udevadm info --query=all --name=/dev/sda | grep ID_SERIAL 

where /dev/sda is generally primary HDD.

You can use static function like below to call Bash

public static string Bash(this string cmd)
{
    var escapedArgs = cmd.Replace("\"", "\\\"");

    var process = new Process()
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "/bin/bash",
            Arguments = $"-c \"{escapedArgs}\"",
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true,
        }
    };
    process.Start();
    string result = process.StandardOutput.ReadToEnd();
    process.WaitForExit();
    return result;
}

then execute

var output = "udevadm info --query=all --name=/dev/sda | grep ID_SERIAL".Bash();

Upvotes: 3

Gunjan Raval
Gunjan Raval

Reputation: 151

Try this :

udevadm info --query=all --name=/dev/sda | grep ID_SERIAL

There are other methods but this one doesn't require root permissions.

Upvotes: 2

Related Questions