How to generate a host unique ID?
Asked Answered
P

4

7

I have several applications which works together and need to communicate.

These applications can be installed on different host, which can be in different local networks, but all are connected together via a global network.

The host can be Linux (CenOS, Debian, Ubuntu) or Windows (7/10).

Now, I need to identify which host is running an application.

The requirements are : - every application running on the same host have the same "host ID" - every "host ID" is unique among all hosts

Since the host can be physical machines, virtual machines or even docker instances, I don't think I can use the hostname as a "global unique ID".

Since the host can be in different local networks, I don't think I can use local IP address as a "global unique ID".

So, which data can I use ? Maybe the Network adapter MAC address ? Is it guaranteed to be unique, even between multiple instances of the same docker ?

Thanks.

Pless answered 16/5, 2016 at 7:0 Comment(3)
Docker container ids are uuids. Sounds like you need a way to assign uuid as a VM hostname and hardware node hostname?Bute
Every host with network interface must be unique mac address. Include docker instances. If for any rason, two dockers hosts, you can modify mac address with docker run --lxc-conf="lxc.network.hwaddr=92:20:de:b0:6b:61" my_image ifconfigHeterogony
@Heterogony ... If two hosts are on different network segments (separated by two routers, not just by one router or by switches), duplicate MAC addresses won't cause any trouble for network traffic. Probably won't happen with physical devices, but I can see this happening in a massively-virtualized environment (where each host or datacenter uses a copy of the same configuration for a pool of internal MAC addresses and private IP addresses).Ether
M
5

On modern / recent Linux distro, linux generates for you a unique id in the /etc/machine-id file when the system is created the first time. Some distributed services such as etcd rely on this file to identify machines in a cluster.

Nothing prevents you from creating and persisting on disk something like a uuid. You can use uuidgen command on Linux for that.

Menton answered 16/5, 2016 at 9:30 Comment(2)
This is not correct, i did not find any such file on any distro (multiple) i have used so far.Romanov
On fedora 24 it is located in /etc/machine-id. On ubuntu 14.04 it is located on /var/lib/dbus/machine-id. Which distro are you using heemayl?Menton
A
4

You can say the MAC address is unique. However, if you are concerned about security, do not use the MAC address. The MAC address can be spoofed easily.

To generate a unique ID for the machine, you can use a combination of many items such as MAC Address, IP Address, hostname etc., throw in a random salt and take a sha256 hash of them. Since the host can run on many operating system, I would suggest against using any OS-specific parameters.

Without going into too much detail, the chances of finding a collision in sha256 is improbable to say the least.

You can see the SHA256 of different text here. http://www.xorbin.com/tools/sha256-hash-calculator

Almost every programming language, these days, has a function/API for creating a SHA256 hash.

Annabelleannabergite answered 16/5, 2016 at 7:38 Comment(2)
I'm not concerned into security (not in this part of the project...) I just need a host unique ID. Using a SHA256 of multiple data (MAC + IP + HostName ) is a good idea. I can't really use random nuber because all applications running on the same host need to have the same "host id"Pless
So something like (ip addr show scope global | grep -v lft; hostname --fqdn; hostid) | sha256sum | cut -f1 -d" " seems to be nice solution for such case.Extremely
F
2

Looks like /var/lib/dbus/machine-id contains what you need.

read more here: http://man7.org/linux/man-pages/man5/machine-id.5.html

Flagging answered 19/12, 2016 at 5:31 Comment(0)
A
0

On linux I solved my problem just by changing the contents of the /var/lib/dbus/machine-id file

Assay answered 2/6, 2022 at 12:50 Comment(1)
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.Friedland

© 2022 - 2024 — McMap. All rights reserved.