- Globally Unique Identifier (GUID) Generator For Linux, Windows, Java, PHP, C#, Javascript, Python
- Example GUID
- Generate GUID In Linux
- Generate GUID In Windows
- Generate GUID In Java
- Generate GUID In PHP
- Generate GUID In C#
- Generate GUID In Python
- How to Generate UUID in Linux
- Generate UUID in Linux
- Conclusion
- Paise / guid.c
- Linux command to generate new GUID?
- 6 Answers 6
- Not the answer you’re looking for? Browse other questions tagged linux guid or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- How to create a UUID in bash?
- 14 Answers 14
Globally Unique Identifier (GUID) Generator For Linux, Windows, Java, PHP, C#, Javascript, Python
Globally Unique Identifier (GUID) is a pseudo-random string which consists of 32 letters, numbers (0-9), and 4 hyphens to separate letters. These letters are randomly generated. In probability theory, this value is unique and can be used as a secret and session cookie, etc.
Example GUID
The following line provides a GUID. as we can see there are 4 dashes that separate the values into 5 parts. The first part consist of 6 letters, second part 4, third part 4, forth part 4, and the last fifth part 12 letters. They are generated randomly.
Generate GUID In Linux
Linux provides a lot of different tools to generate GUID. But one of the most simple and default installed tool is uuidgen . We just issue the command like below.
Generate GUID In Windows
Windows operating systems like Server 2008, 20012, 2016, 7, 8, 10 have the ability to generate GUID in PowerShell, 3rd party tools, etc. But the most integrated and fast way is using PowerShell which actually uses .Net libraries we have already examined in Generating GUID with C# .
Generate GUID In Java
Java programming language and software development kit provides a lot of different types of random number generation and format functions. We can use util class randomUUID() function like below.
Generate GUID In PHP
PHP has different ways to generate GUID. PHP running on the Windows system can use com_create_guid() function but in Linux, we need some external help. We can create a function that will generate random numbers in the specified range and length. We will create a function named GUID() like below and inject com_create_guid() function too.
Another way is using openssl functions with some convert.
Generate GUID In C#
C# is a very feature-rich and reliable programming language with useful libraries. C# provides the NewGuid() method in order to create some GUID value. The following snippet will create a GUID value
Generate GUID In Python
Python provides uuid module which contains a lot of different functions about the Unique Identifier. We can use uuid1() to generate GUID easily in a single line.
Источник
How to Generate UUID in Linux
UUID (Universally Unique identifier) is a 128-bit unique number standardized by the Open Software Foundation. This number is universally unique and really impossible for a user to guess.
This tutorial shows how to generate UUID in Linux using uuidgen command line utility tool.
Generate UUID in Linux
Most of the Linux/Unix system supports the command line utility tool uuidgen to generate the UUID. If the package is unavailable install using apt install uuid-runtime command.
Uuidgen can generate random-based, time-based and hash-based UUIDs. Random based UUIDs is sufficient in most cases.
Simply executing uuidgen generates a random UUID.
You can use the -t flag along with uuidgen to generate a UUID based on system time. In the same way uuidgen command with option -r generates the random UUID based mostly on random bits.
Followings are the some example to generate UUIDs.
To create a random UUID, use the following command:
To generate time based UUID, run:
Use the command uuidgen with the option -r to generate random UUID.
To generate hash based UUID use —md5 or —shal with —namespace .
Conclusion
In this tutorial, we learned how to generate UUID in Linux. Finding UUID in Linux pretty easy, which is commonly required when it to comes to mount storage devices in fstab file.
Источник
Paise / guid.c
# include stdio.h > |
# include string.h > |
# ifdef _WIN32 |
# include objbase.h > |
# else |
# include uuid/uuid.h > |
# endif |
# ifndef _WIN32 |
/* This is the linux friendly implementation, but it could work on other |
* systems that have libuuid available |
*/ |
void create_guid ( char guid[]) |
< |
uuid_t out; |
uuid_generate (out); |
uuid_unparse_lower (out, guid); |
> |
# else |
void create_guid ( char guid[]) |
< |
GUID out = < 0 >; |
CoCreateGuid (&out); |
sprintf (guid, » %08X — %04X — %04X — %02X%02X — %02X%02X%02X%02X%02X%02X » , out. Data1 , out. Data2 , out. Data3 , out. Data4 [ 0 ], out. Data4 [ 1 ], out. Data4 [ 2 ], out. Data4 [ 3 ], out. Data4 [ 4 ], out. Data4 [ 5 ], out. Data4 [ 6 ], out. Data4 [ 7 ]); |
for ( ; *guid; ++guid) *guid = tolower (*guid); // To lower one-liner |
> |
# endif |
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Источник
Linux command to generate new GUID?
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
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 ?
6 Answers 6
Assuming you don’t have uuidgen , you don’t need a script:
You can use command uuidgen . Simply executing uuidgen will give you time-based UUID:
Since you wanted a random UUID, you want to use Type 4 instead of Type 1:
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:
If you prefer lowercase, change:
In Python 3, no cast to str is required:
If you just want to generate a pseudo-random string with some dashes at locations 8, 12, 16, and 20, you can use apg .
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:
Here M and N fields encode the version/format of the UUID.
Not the answer you’re looking for? Browse other questions tagged linux guid or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
How to create a UUID in bash?
In Java it is possible to create a random UUID:
How to do this in Bash?
14 Answers 14
See the uuidgen program which is part of the e2fsprogs package.
According to this, libuuid is now part of util-linux and the inclusion in e2fsprogs is being phased out. However, on new Ubuntu systems, uuidgen is now in the uuid-runtime package.
To create a uuid and save it in a variable:
On my Ubuntu system, the alpha characters are output as lower case and on my OS X system, they are output as upper case (thanks to David for pointing this out in a comment).
To switch to all upper case (after generating it as above):
To switch to all lower case:
If, for example, you have two UUIDs and you want to compare them in Bash, ignoring their case, you can do a tolower() style comparison like this:
To add variety without adding external dependencies, on Linux you can do:
To propagate bad practices, on FreeBSD, under the linux compatibility layer (linuxulator?),
Just for the sake of completeness. There’s also a UUID generator installed with the dbus package on Debian. I missed it looking around earlier. It’s probably the same algorithm as the e2fsprogs package, but it doesn’t add the dashes, so it might be a little cleaner for you:
Grawity adds a safety tip: «DBus UUIDs are not related to or compatible with RFC 4122. Besides, dbus-uuidgen always uses the Unix timestamp as the last 4 bytes. So they might be unsuitable for some uses.» (Thanks, Grawity, I should’ve spotted that in the manpage.)
If you do not want to depend on other executables, or you cannot use them, here is the pure bash version from here:
I have found this script «one-liner» useful where uuidgen is not available. This also bypasses any neccessity to install external modules for Perl or Python.
Tested on SnowLeopard, Red Hat Valhalla, Solaris 9 4/04 and newer successfully. I am curious if this is prone to non-uniqueness, but I have not been ‘bit’ten in the last 10 years. Of course, head -1 could be replaced with head -_other-value_ | tail -1 too.
/dev/random and /dev/urandom are kernel random generators.
od (octal dump) has a hex output switch (-x) producing 16 bytes per line.
head -n [| tail -1] (where n>0) extracts just one line of the previous output.
awk sets the OutputFieldSeparator to be a hyphen everywhere a comma occurs in the print statement. By specifying fields 2-9 independently, we control the hyphens and strip off the index/offset counter that ‘od’ prefixes each line of output with.
The result is a pattern of 8-4-4-4-12 lower case characters a-f0-9 .
Источник