Tutorial

How To Generate Universally Unique Identifiers (UUIDs) with uuidgen

Updated on October 16, 2020
Default avatar

By joshtronic

How To Generate Universally Unique Identifiers (UUIDs) with uuidgen

Introduction

Universally unique identifiers (UUIDs) are 128-bit numbers that are accepted as being unique on the local system they are created on as well as among the UUIDs created on other systems in the past as well as the future. Because of their uniqueness, they come in handy in situations where an auto incremented primary key can fall short.

Because of their uniqueness, UUIDs are well suited for generating test data. Need a random string? A UUID is fine. What about an email? UUID@UUID.com is great. Need a bunch of random string? UUIDs will be unique, making them easy to track down as they move through a system.

To generate universally unique identifiers from the command-line interface, you can use the uuidgen utility,

In this tutorial you’ll use uuidgen and some shell scripting to generate UUIDs and some sample data.

Generating a UUID

The uuidgen command is often already installed on Unix-like operating systems like Linux and macOS. If it’s not, you can install it through your package manager. On Ubuntu and Debian systems, install the uuid-runtime package.

First, update your system’s list of available packages using the apt update command:

  1. sudo apt update

Next install the uuid-runtime package using the apt package manager:

  1. sudo apt install uuid-runtime

To generate a single UUID, run the uuidgen command without any arguments. Out of the box, uuidgen will generate a random UUID each time it runs.

Execute the following command in your terminal:

  1. uuidgen

You’ll see output similar to the following, although your UUID will be different:

Output
a522f494-92ce-44e9-b1a3-f891baed8d60

Note: The macOS version of uuidgen does function a bit differently than that of the Linux version in that is returns UUIDs in all capital letters.

You can also generate time-based and hash-based UUIDs, but generally speaking, the random values are sufficient for most cases.

You may want to generate multiple UUIDs at once, which you’ll explore next.

Generating Multiple UUIDs

To generate a bunch of UUIDs at once, you’ll leverage a small bit of shell scripting using the for loop to execute the uuidgen command multiple times.

For example, to generate 10 UUIDs, execute the following command:

  1. for i in {1..10}; do uuidgen; done

You’ll see 10 UUIDs printed to the screen:

Output
834efdb6-6044-4b44-8fcb-560710936f37 e8fa8d54-641a-4d7b-9422-91474d713c62 dff59ac0-4d80-4b96-85c4-14f3a118e7fe 511fea83-9f5f-4606-85ec-3d769da4bf63 3bc82ef7-1138-4f97-945a-08626a42a648 a33abc11-264e-4bbb-82e8-b87226bb4383 2a38839e-3b0d-47f0-9e60-d6b19c0978ad 74dca5e8-c702-4e70-ad16-0a16a64d55fa cd13d088-21cf-4286-ae61-0643d321dd9e 9aec3d5a-a339-4f24-b5a3-8419ac8542f2

You can swap the 10 out for the number you’d like.

Based on the unique nature of UUIDs, you don’t have to worry about any duplicates in your generated data. Now let’s look at using UUIDs in different ways.

Using UUIDs in Test Data

If you wanted to generate a list of comma-separated values (CSV) with 2 UUIDs per line, you would use the echo command to print two UUIDs during each iteration of the for loop. Execute the following command:

  1. for i in {1..10}; do echo `uuidgen`,`uuidgen`; done

You’ll get this output:

Output
63b1146f-9e7c-4e1f-82eb-3fe378e203df,ed9d6201-e5b2-4410-9ab1-35c8ca037994 8d3981b6-f112-4f21-ac4b-44791e279b2a,eb63310e-d436-44fa-80c6-65721a300a2b 0eddfe24-1c2e-43a1-b2c2-9d3af6bad837,62ef1782-76a2-4b3c-ac69-1c2d02f65789 29f18766-fc9d-46a4-a1d0-e112738edb30,b6bd303d-1148-4f46-bec7-d7e4cb6e4f03 865bcf30-6a8b-49d6-8b27-8dc51620adf7,972b0959-4270-4683-b19b-360b2605f2d0 0d82d54b-566a-45d1-b3a8-5da1a88bceb3,1c67a802-9647-46b1-bde4-3053699b27f9 778b5415-3e1f-4bc5-a349-499459ac4ab7,7e1a2081-c742-4882-9154-e5d2a4af630c e6cc95bd-3ee1-43cb-bea1-51783de5fc57,5088d3a3-ab67-4684-8761-e48bb14596ec a7453bc0-b5e5-41a3-9ed4-cf4d8e0908a2,957ef50f-7889-4335-9f40-17878e3d20fe 3689362d-588a-409e-bd2c-d6fdaa361574,9ffe7c8d-9afb-4b24-a5b7-b29a06f6fac7

Using the same approach, you can generate data that looks like email addresses by making a small tweak to the echo statement:

  1. for i in {1..10}; do echo `uuidgen`@`uuidgen`.com; done

You’ll receive this output:

Output
7dd44050-9df4-43aa-b3b4-3b47eff8fc31@3052e93c-95d1-40f5-b468-3d4e06dd208b.com cca71187-f666-46ff-81c6-eb3b72ff6972@30f4c9a8-712e-4f4c-ad3a-4b55ef85eee0.com 6ff086ad-493d-4b3a-8ed1-970239d7125b@8302d772-4deb-43d1-8901-0a3b4f747b55.com f9813daa-6a8e-4543-8708-d42cefdda20a@d586854c-7df9-4046-89f8-51f960973afb.com a7e9e43b-d2b1-4415-b73d-ff72b713e45f@a7c56c2c-df25-44bc-872d-a893f750b54d.com 0d1d13fe-777d-44d8-b1b2-302ca1e48aa1@7c2d8e6a-fa8b-4fa3-a0ef-8360aa42e730.com f85d0772-22d2-43d0-8d71-4e6714c2bb20@fb4f74fe-f9f9-4e86-b31d-f148344a97e0.com f46eb868-0a99-4291-98f2-19d95f1e9fbb@37ef072d-c515-4145-8b8a-edf32ec18bd2.com eaa4a63e-2646-427a-a892-f8027c2791ed@33daf102-2b5b-4070-88c5-261fe5d96cfa.com d75f6720-b249-4395-bcc7-9ffe2b67cabb@457b04b4-3c15-4b77-aae2-9afd6803bcfe.com

These aren’t real email addresses that you can validate, but you can tweak the output one more time, and swap the second uuidgen for a disposable email address domain, like [mailinator.com](https://mailinator.com], you will not only have a list of realistic-looking data, but it will be a list email addresses you could actually use or monitor in tests. Try the following command:

  1. for i in {1..10}; do echo `uuidgen`@mailinator.com; done

This time you’ll see output like this:

Output
4ba50929-520b-49f7-996d-e369be5d6232@mailinator.com 16deaeae-64bd-45f0-9f73-b32d41ca1bfb@mailinator.com 743701e8-0dc5-4851-8fc4-24d155755bdc@mailinator.com adff0015-c535-431a-970f-98ffd1fc21eb@mailinator.com 6516fcb3-e54f-4800-a6cc-11d50d756f28@mailinator.com 8a9c5252-bd0c-4c3b-a7c9-4b60ebcc4294@mailinator.com eed94fd6-b075-493c-8d8e-3acae90d5629@mailinator.com f4ab80d2-85ca-4722-a260-0f84c37051fd@mailinator.com 53ead1d0-cc70-410f-a91a-4a79b339fba2@mailinator.com b208e103-d7f1-4f6d-838d-530d6339dce7@mailinator.com

Finally, to save the output of this command to a file, you can append > /path/to/some/file to pipe the output:

  1. for i in {1..10}; do echo `uuidgen`@mailinator.com; done > /tmp/emails.txt

Then use the cat command to view the file you just created:

  1. cat /tmp/emails.txt

The file is displayed on your screen:

Output
826119d2-f590-4fa3-ba7e-0717869d40b1@mailinator.com 795fec1a-76fe-4fed-8a06-ed517c1a5e7d@mailinator.com 14a502ad-0aa9-40e5-a46f-5806264b5316@mailinator.com c6c2a588-7cce-4675-a490-0101d7bcc614@mailinator.com 7346c15b-0c92-44c4-a854-5de18c0c202d@mailinator.com c67a535a-e28d-43b1-b553-c203bc22a821@mailinator.com 76d22d18-0f09-405d-9903-eb44ec93b605@mailinator.com 2b631756-21e6-4d95-873b-3245797f9028@mailinator.com aab686e8-540e-43e9-9e24-ca04fbf4d414@mailinator.com a577e9c9-0ad1-4934-b5f1-17b68938fff8@mailinator.com

You can use the same approach to save any of the other examples in this tutorial to files.

Conclusion

Universally unique identifiers are more robust than a random number. Their uniqueness makes them quite powerful. Combined with some light shell scripting on the command-line interface, you’re able to generate useful data, without needing to load up your favorite programming language’s package repository.

Next time you’re in need of a UUID, save yourself the search for “online UUID generator” and use your system’s uuidgen command. To learn more about your system’s particular implementation of uuidgen, type man uuidgen in your terminal to view its documentation…

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
joshtronic

author



Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel