That’s all. I just found this in a random script. Generates a random UUID every time it’s called. I didn’t know.

Of course I can also use uuidgen or pipe /dev/(u)random into something to get a random alphanumeric string - but this is built right into the kernel!

In /proc/sys/kernel/random/, there’s also boot_id which seems to do the same is static, and some tweakable parameters.

❤️🐧

  • timhh@programming.dev
    link
    fedilink
    arrow-up
    11
    arrow-down
    1
    ·
    14 days ago

    Yeah but please don’t actually use this. Use a proper UUID library that works cross-platform and lets you choose the UUID type and can be seeded etc.

    • A_norny_mousse@feddit.orgOP
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      14 days ago

      Can you explain?
      Use for what?
      Also it is being seeded, according to the file urandom_min_reseed_secs which is also writeable. Here are the other files:

      -r--r--r-- 1 root root 0 25. 5. 11:13 boot_id
      -r--r--r-- 1 root root 0 25. 5. 11:19 entropy_avail
      -r--r--r-- 1 root root 0 25. 5. 11:19 poolsize
      -rw-r--r-- 1 root root 0 25. 5. 11:19 urandom_min_reseed_secs
      -r--r--r-- 1 root root 0 25. 5. 11:19 uuid
      -rw-r--r-- 1 root root 0 25. 5. 11:19 write_wakeup_threshold
      

      edit: the type is always DCE/random

      • timhh@programming.dev
        link
        fedilink
        arrow-up
        1
        ·
        5 days ago

        There are a few reasons you shouldn’t use this in proper programs. If you’re the sort of person that thinks hacky Bash scripts are acceptable then sure, use it there.

        1. It isn’t cross-platform. Not available on Mac/Windows.
        2. There are several types of UUID with different properties. This doesn’t let you choose which one to use.
        3. To make programs deterministic (really useful for testing!) you want to be able to seed all their randomness with a specific seed so that it generates the same UUID each time you run it. (Obviously in normal use you would use a random seed.)
  • lnxtx (xe/xem/xyr)@feddit.nl
    link
    fedilink
    English
    arrow-up
    5
    ·
    edit-2
    13 days ago

    Interesting,
    non-scientific speed test:

    kernel 6.1.0-37-amd64:

    $ time for i in $(seq 1 100000); do cat /proc/sys/kernel/random/uuid > /dev/null; done
    
    real    3m53,388s
    user    1m37,366s
    sys     2m13,847s
    
    $ for i in $(seq 1 100000); do cat /proc/sys/kernel/random/uuid ; done | wc -l
    100000
    

    vs. uuid 1.6.2-1.5+b11:

    $ time for i in $(seq 1 100000); do uuid -v4 > /dev/null; done
    
    real    4m44,854s
    user    1m37,867s
    sys     3m4,414s
    
    $ for i in $(seq 1 100000); do uuid -v4 ; done | wc -l
    100000
    

    EDIT: I’m blind (wrong result).

    • A_norny_mousse@feddit.orgOP
      link
      fedilink
      arrow-up
      3
      ·
      13 days ago

      When writing shell scripts that run in a loop I always look for files in /sys or /proc before using standard utilities. There’s a lot

      • /proc/mounts instead of the mount command
      • /sys/class/…/backlight to influence screen brightness
      • /sys/class/net to get info and stats about interfaces

      Someone should make a comprehensive list I guess.