3

Running the following command generates different random output on each execution (which is expected, considering that random is in the path):

cat /proc/sys/kernel/random/uuid

Is there any way to modify the path so that it results in a static (not random) UUID on each call (return the same thing on each request/execution instead of being random)?

schaiba
  • 7,493
  • 1
  • 33
  • 31
Agi Hammerthief
  • 572
  • 6
  • 21
  • 4
    This doesn't make sense much. A `uuid (Universal Unique IDentifier)` is meant to be unique among system's generated uuid's as well as global known uuid's. If you want to refer to the same uuid you generate, just use `mystaticuuid=$(uuidgen)` and then call it using `echo "$mystaticuuid"` – Valentin Bajrami Mar 08 '17 at 09:48

2 Answers2

6

You asked

Is there any way to modify the path so that it results in a static (not random) UUID on each call

And the answer to this is "yes, definitely".

If you prepare the environment like this:

cat /proc/sys/kernel/random/uuid >/tmp/uuid

You can modify the path to get a static UUID on each call, like this

cat /tmp/uuid
roaima
  • 107,089
  • 14
  • 139
  • 261
  • 1
    Well, that's obvious, I suppose. (I blame lack of sleep for not thinking that through.) – Agi Hammerthief Mar 08 '17 at 10:49
  • 1
    @Agi what are you _actually_ trying to achieve? – roaima Mar 08 '17 at 11:16
  • What I actually want to do is create a unique ID for a machine running GNU/Linux, thinking that running a slight adaptation of `/proc/sys/kernel/random/uuid` would be the way to go. On Windows, I can run `wmic DISKDRIVE get SerialNumber` and query the registry for the value of `HKLM\Software\Microsoft\Cryptography\MachineGuid` to get values I can work with. I was wondering if I could achieve something similar with GNU/Linux (other than getting the HWAddrs of all NICs in the machine and hashing them, as I have seen suggested). However, I'll ask a separate question for that. – Agi Hammerthief Mar 08 '17 at 12:39
  • I have some existing Java software that uses a Windows-specific DLL to retrieve hardware information and generate a hashed UID for the machine on which it runs (implementation details of which are known to me). I'm trying to port it to GNU/Linux and the licensing is what prevents me from porting it. (The output doesn't *have* to be the same on both platforms, but it would be advantageous if it is.) – Agi Hammerthief Mar 08 '17 at 12:49
  • 2
    @AgiHammerthief take a look at [generate consistent machine unique ID](http://unix.stackexchange.com/questions/144812/generate-consistent-machine-unique-id) – roaima Mar 08 '17 at 13:42
4

Having recently needed to build some licensing functionality into an application (the problem I was trying to solve when first posting this question), I found that it's possible to obtain a static UUID from one of the following files:

/etc/machine-id  # preferred
/var/lib/dbus/machine-id  # Usually a symlink to /etc/machine-id
/var/db/dbus/machine-id  # usually a symlink/alternative to /var/lib/dbus/machine-id
/proc/sys/kernel/random/uuid  # Will vary on each invocation, so save output to another file 
  # (as per roaima's answer).

In the above list of files, they're listed from most preferred to least preferred.

Agi Hammerthief
  • 572
  • 6
  • 21
  • If `machine_id` is not available, another fallback for obtaining a static, per-machine ID would be to hash `/etc/ssh/ssh_host_ecdsa_key.pub` – user31708 Jun 02 '23 at 02:00