1

I want to get the unique identifier of the current host that is used as the license name in golang. How to do that ? For example, like C:

gethostid() //can get the host id
jww
  • 97,681
  • 90
  • 411
  • 885
LEo
  • 442
  • 5
  • 21
  • 1
    gethostid(3) just read /etc/hostid (and does some mumbo jumbo if not available). Just read /etc/hostid yourself. – Volker Mar 22 '16 at 07:55
  • Did you manage to find a solution by yourself? I'm trying to implement the same thing, without luck so far. – Bernardo Vale Jun 07 '16 at 02:10
  • I use the ip address, to convert hostid, for example: ` ip: 192.168.3.120 -> hostid:a8c07803` – LEo Jun 08 '16 at 05:25
  • Possible duplicate of [Getting a unique id from a unix-like system](https://stackoverflow.com/q/328936/608639) – jww May 28 '18 at 11:20

3 Answers3

4

You probably want the machine-id.

http://man7.org/linux/man-pages/man5/machine-id.5.html says:

The machine ID is usually generated from a random source during system installation and stays constant for all subsequent boots. Optionally, for stateless systems, it is generated during runtime at early boot if it is found to be empty.

The machine ID does not change based on local or network configuration or when hardware is replaced. Due to this and its greater length, it is a more useful replacement for the gethostid(3) call that POSIX specifies.

You can get the machine-id on (recent) Linux systems with:

cat /etc/machine-id
# or
cat /var/lib/dbus/machine-id

Most major OSs have a unique host identifier. Still, there may be non-unique host IDs (caused by imaging/cloning/backup-restore).

You can also check out my golang package machineid for implementation details, which works on BSD, Linux, OS X and Windows and requires no admin privileges.

1

gethostid(3) is a UNIX/BSD specific libc function. reading from /etc/hostid would not work on non UNIX systems and is not platform independent.

since go does not provide something like gethostid() why not implement it like other platform independent languages like JAVA do, answered here: How to get a unique computer identifier in Java (like disk id or motherboard id)

Community
  • 1
  • 1
0

Using this code you can generate a license for host using uuid of host

package main

import (
        "encoding/base64"
        "fmt"
        "github.com/google/uuid"
)

func main() {

        uuid := uuid.New()
        uuidBytes := uuid[:]
        licenseKeyBytes := append(uuidBytes)
        licenseKey := base64.StdEncoding.EncodeToString(licenseKeyBytes)
        fmt.Println("Generated license key:", licenseKey)
}
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Himanshu Bansal May 20 '23 at 05:17