While developing applications or using services or mounting disks in Linux systems we may see the term UUID or 128-bit number which might seem interesting. UUID is a 128-bit number that is used to identify things like name, record, disk whatever we want.
在Linux系统中开发应用程序或使用服务或装入磁盘时,我们可能会看到UUID或128位数字一词,这似乎很有趣。 UUID是一个128位数字,用于标识我们想要的名称,记录,磁盘之类的东西。
UUID的唯一性 (Uniqueness Of UUID)
What makes UUID so special or interesting is its uniqueness. Yes it is right event two different and discrete systems creates UUID it is assumed both are unique and do not collide each other or with other UUID’s. Actually they are not perfectly unique but the probability of the collusion is very very very low to count.
使UUID如此特别或有趣的是它的独特性。 是的,如果两个不同的离散系统创建UUID是正确的,则假定两者都是唯一的,并且不会彼此冲突或与其他UUID冲突。 实际上,它们并不是完全独特的,但是合谋的可能性却非常低。
示例UUID (Example UUID)
Here are some UUID examples.
这是一些UUID示例。
a8098c1a-f86e-11da-bd1a-00112444be1e
We can see that fstab
content of the Linux systems contains UUID in order to identify the disk partition.
我们可以看到Linux系统的fstab
内容包含UUID,以便标识磁盘分区。
$ cat /etc/fstab
![Example UUID](https://i-blog.csdnimg.cn/blog_migrate/7af9378f9245f8a09a9c4881992364e4.png)
使用Python生成UUID (Generate UUID with Python)
There are different ways to generate UUID. There is a lot of web-based UUID generators but using Python is more attractive. We can use Python uuid
module to generate UUID with different options.
有多种生成UUID的方法。 有很多基于Web的UUID生成器,但使用Python更具吸引力。 我们可以使用Python uuid
模块生成具有不同选项的UUID。
生成基于UUID的主机ID (Generate UUID Based Host ID)
While generating UUID we can make some part of UUID bound to the host. We will import uuid
module and run uuid1()
function in order to generate host-based UUID.
在生成UUID时,我们可以将UUID的一部分绑定到主机。 我们将导入uuid
模块并运行uuid1()
函数以生成基于主机的UUID。
import uuid
uuid.uuid1()
![Generate UUID Based Host ID](https://i-blog.csdnimg.cn/blog_migrate/ede672d707db4c4cc761f9419ea7887c.png)
生成完全随机的UUID (Generate Fully Random UUID)
If we need to generate completely random UUID we need to call uuid4()
function. Every time we call this function will create completely different UUIDs.
如果需要生成完全随机的UUID,则需要调用uuid4()
函数。 每次调用此函数时,都会创建完全不同的UUID。
import uuid
uuid.uuid4()
![Generate Fully Random UUID](https://i-blog.csdnimg.cn/blog_migrate/99b2f8e7b0f332a6e40078f814d0a352.png)
翻译自: https://www.poftut.com/what-is-uuid-universally-unique-identifier/