Linux command to generate new GUID?
Asked Answered
C

6

28

Sometimes in bash scripting, i need to generate new GUID(Global Unique Identifier).

I already done that by a simple python script that generates a new guid: see here

#! /usr/bin/env python
import uuid
print str(uuid.uuid1())

But i need to copy this script into any new system that i works on.

My question is: can anybody introduce a command or package that contains similar command ?

Collage answered 4/5, 2013 at 8:51 Comment(2)
possible duplicate of Command line GUID for Unix and Windows?Silvio
random uuid? pseudo-random from a given seed?Traumatism
P
25

Assuming you don't have uuidgen, you don't need a script:

$ python -c 'import uuid; print(str(uuid.uuid4()))'
b7fedc9e-7f96-11e3-b431-f0def1223c18
Pelagic answered 17/1, 2014 at 16:47 Comment(2)
Nice, but it doesn't seem to be quite random: only the 8 first characters change on every call.Roscoeroscommon
@Roscoeroscommon maybe it's fixed in newer Pythons, but I cannot replicate your concernSchoolmate
U
38

You can use command uuidgen. Simply executing uuidgen will give you time-based UUID:

$ uuidgen
18b6f21d-86d0-486e-a2d8-09871e97714e
Upshot answered 4/7, 2020 at 18:54 Comment(2)
If you mean uuidgen for Linux, be careful, on Ubuntu 20.04, it is default to generate as random-based UUID. It mentions in man page By default uuidgen will generate a random-based UUID if a high-quality random number generator is present. Otherwise, it will choose a time-based UUID.Demonstrator
So let's help people to ensure they avoid that pitfall! uuidgen -h for help shows that uuidgen -r will enforce generating a random UUID. I don't know if all distros' versions default to time-based, but including -r should ensure randomness.Emigration
P
25

Assuming you don't have uuidgen, you don't need a script:

$ python -c 'import uuid; print(str(uuid.uuid4()))'
b7fedc9e-7f96-11e3-b431-f0def1223c18
Pelagic answered 17/1, 2014 at 16:47 Comment(2)
Nice, but it doesn't seem to be quite random: only the 8 first characters change on every call.Roscoeroscommon
@Roscoeroscommon maybe it's fixed in newer Pythons, but I cannot replicate your concernSchoolmate
A
10
cat /proc/sys/kernel/random/uuid
Accommodate answered 15/9, 2021 at 8:29 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Salespeople
Frankly, this is the best answer. It works in Linux even if you don't have uuidgen installed.Uptotheminute
S
5

Since you wanted a random UUID, you want to use Type 4 instead of Type 1:

python -c 'import uuid; print str(uuid.uuid4())'

This Wikipedia article explains the different types of UUIDs. You want "Type 4 (Random)".

I wrote a little Bash function using Python to generate an arbitrary number of Type 4 UUIDs in bulk:

# uuid [count]
#
# Generate type 4 (random) UUID, or [count] type 4 UUIDs.
function uuid()
{
    local count=1
    if [[ ! -z "$1" ]]; then
        if [[ "$1" =~ [^0-9] ]]; then
            echo "Usage: $FUNCNAME [count]" >&2
            return 1
        fi

        count="$1"
    fi

    python -c 'import uuid; print("\n".join([str(uuid.uuid4()).upper() for x in range('"$count"')]))'
}

If you prefer lowercase, change:

python -c 'import uuid; print("\n".join([str(uuid.uuid4()).upper() for x in range('"$count"')]))'

To:

python -c 'import uuid; print("\n".join([str(uuid.uuid4()) for x in range('"$count"')]))'
Strait answered 3/8, 2015 at 22:18 Comment(0)
S
3

In Python 3, no cast to str is required:

python -c 'import uuid; print(uuid.uuid4())'
Stan answered 20/11, 2018 at 16:46 Comment(0)
T
0

If you just want to generate a pseudo-random string with some dashes at locations 8, 12, 16, and 20, you can use apg.

apg -a 1 -M nl -m32 -n 1 -E ghijklmnopqrstuvwxyz | \
    sed -r -e 's/^.{20}/&-/' | sed -r -e 's/^.{16}/&-/' | \
    sed -r -e 's/^.{12}/&-/' | sed -r -e 's/^.{8}/&-/'

The apg clause generates 32 symbols from [0-9a-f] (lower-case). The series of sed commands add the - tokens and can very likely be shortened.

Note that often an UUID has a particular format:

xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

Here M and N fields encode the version/format of the UUID.

Tollmann answered 19/5, 2021 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.