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)?

1
  • 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" Mar 8, 2017 at 9:48

2 Answers 2

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
6
  • 1
    Well, that's obvious, I suppose. (I blame lack of sleep for not thinking that through.) Mar 8, 2017 at 10:49
  • 1
    @Agi what are you actually trying to achieve? Mar 8, 2017 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. Mar 8, 2017 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.) Mar 8, 2017 at 12:49
  • 2
    @AgiHammerthief take a look at generate consistent machine unique ID Mar 8, 2017 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.

4
  • 1
    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 2 at 2:00
  • Thank you. That could work too. I'm not sure why you suggest hashing it, since it is my understanding is that the contents of that file is already a hash of a prime number. Oct 29 at 9:22
  • 1
    Yes, ultimately it contains a hash, but it also has structure around it. It's a text file, and even if you just look at the base64-encoded part of it, you'll see it always begins with "AAAA". So you could base64 decode it, and then know enough about the binary structure to extract just the hash part. Or you could just hash the whole file, which is easier.
    – user31708
    Nov 7 at 19:56
  • Ah, that makes sense. I'm all for taking the easier approach if there is one. Nov 9 at 12:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .