{"id":13466542,"url":"https://github.com/denisbrodbeck/machineid","last_synced_at":"2025-03-25T21:32:28.873Z","repository":{"id":41176254,"uuid":"107385967","full_name":"denisbrodbeck/machineid","owner":"denisbrodbeck","description":"Get the unique machine id of any host (without admin privileges)","archived":false,"fork":false,"pushed_at":"2024-08-20T07:27:25.000Z","size":47,"stargazers_count":991,"open_issues_count":10,"forks_count":142,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-10-29T19:19:16.413Z","etag":null,"topics":["cmdline","cross-platform","go","golang","hostid","library","machineid","mit-license"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/denisbrodbeck.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-10-18T09:21:17.000Z","updated_at":"2024-10-29T06:43:35.000Z","dependencies_parsed_at":"2024-01-13T23:01:47.560Z","dependency_job_id":"699c9abd-5ac3-4a4a-a0c6-e2bf83a3fdca","html_url":"https://github.com/denisbrodbeck/machineid","commit_stats":{"total_commits":40,"total_committers":5,"mean_commits":8.0,"dds":0.09999999999999998,"last_synced_commit":"f07179bb15a0bec252453468aa607c670f5c12b5"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denisbrodbeck%2Fmachineid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denisbrodbeck%2Fmachineid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denisbrodbeck%2Fmachineid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denisbrodbeck%2Fmachineid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/denisbrodbeck","download_url":"https://codeload.github.com/denisbrodbeck/machineid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245548549,"owners_count":20633608,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cmdline","cross-platform","go","golang","hostid","library","machineid","mit-license"],"created_at":"2024-07-31T15:00:45.873Z","updated_at":"2025-03-25T21:32:28.562Z","avatar_url":"https://github.com/denisbrodbeck.png","language":"Go","funding_links":[],"categories":["Go","开源类库"],"sub_categories":["系统信息"],"readme":"# machineid provides support for reading the unique machine id of most host OS's (without admin privileges)\n\n![Image of Gopher 47](logo.png)\n\n… because sometimes you just need to reliably identify your machines.\n\n[![GoDoc](https://godoc.org/github.com/denisbrodbeck/machineid?status.svg)](https://godoc.org/github.com/denisbrodbeck/machineid) [![Go Report Card](https://goreportcard.com/badge/github.com/denisbrodbeck/machineid)](https://goreportcard.com/report/github.com/denisbrodbeck/machineid)\n\n## Main Features\n\n* Cross-Platform (tested on Win7+, Debian 8+, Ubuntu 14.04+, OS X 10.6+, FreeBSD 11+)\n* No admin privileges required\n* Hardware independent (no usage of MAC, BIOS or CPU — those are too unreliable, especially in a VM environment)\n* IDs are unique\u003csup\u003e[1](#unique-key-reliability)\u003c/sup\u003e to the installed OS\n\n## Installation\n\nGet the library with\n\n```bash\ngo get github.com/denisbrodbeck/machineid\n```\n\nYou can also add the cli app directly to your `$GOPATH/bin` with\n\n```bash\ngo get github.com/denisbrodbeck/machineid/cmd/machineid\n```\n\n## Usage\n\n```golang\npackage main\n\nimport (\n  \"fmt\"\n  \"log\"\n  \"github.com/denisbrodbeck/machineid\"\n)\n\nfunc main() {\n  id, err := machineid.ID()\n  if err != nil {\n    log.Fatal(err)\n  }\n  fmt.Println(id)\n}\n```\n\nOr even better, use securely hashed machine IDs:\n\n```golang\npackage main\n\nimport (\n  \"fmt\"\n  \"log\"\n  \"github.com/denisbrodbeck/machineid\"\n)\n\nfunc main() {\n  id, err := machineid.ProtectedID(\"myAppName\")\n  if err != nil {\n    log.Fatal(err)\n  }\n  fmt.Println(id)\n}\n```\n\n### Function: ID() (string, error)\n\nReturns original machine id as a `string`.\n\n### Function: ProtectedID(appID string) (string, error)\n\nReturns hashed version of the machine ID as a `string`. The hash is generated in a cryptographically secure way, using a fixed, application-specific key (calculates HMAC-SHA256 of the app ID, keyed by the machine ID).\n\n## What you get\n\nThis package returns the OS native machine UUID/GUID, which the OS uses for internal needs.\n\nAll machine IDs are usually generated during system installation and stay constant for all subsequent boots.\n\nThe following sources are used:\n\n* **BSD** uses `/etc/hostid` and `smbios.system.uuid` as a fallback\n* **Linux** uses `/var/lib/dbus/machine-id` ([man](http://man7.org/linux/man-pages/man5/machine-id.5.html))\n* **OS X** uses `IOPlatformUUID`\n* **Windows** uses the `MachineGuid` from `HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography`\n\n## Unique Key Reliability\n\nDo note, that `machine-id` and `MachineGuid` can be changed by root/admin, although that may not come without cost (broken system services and more).\nMost IDs won't be regenerated by the OS, when you clone/image/restore a particular OS installation. This is a well known issue with cloned windows installs (not using the official sysprep tools).\n\n**Linux** users can generate a new id with `dbus-uuidgen` and put the id into `/var/lib/dbus/machine-id` and `/etc/machine-id`.\n**Windows** users can use the `sysprep` [toolchain](https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/sysprep--generalize--a-windows-installation) to create images, which produce valid images ready for distribution. Such images produce a new unique machine ID on each deployment.\n\n## Security Considerations\n\nA machine ID uniquely identifies the host. Therefore it should be considered \"confidential\", and must not be exposed in untrusted environments. If you need a stable unique identifier for your app, do not use the machine ID directly.\n\n\u003e A reliable solution is to hash the machine ID in a cryptographically secure way, using a fixed, application-specific key.\n\nThat way the ID will be properly unique, and derived in a constant way from the machine ID but there will be no way to retrieve the original machine ID from the application-specific one.\n\nDo something along these lines:\n\n```golang\npackage main\n\nimport (\n  \"crypto/hmac\"\n  \"crypto/sha256\"\n  \"fmt\"\n  \"github.com/denisbrodbeck/machineid\"\n)\n\nconst appKey = \"WowSuchNiceApp\"\n\nfunc main() {\n  id, _ := machineid.ID()\n  fmt.Println(protect(appKey, id))\n  // Output: dbabdb7baa54845f9bec96e2e8a87be2d01794c66fdebac3df7edd857f3d9f97\n}\n\nfunc protect(appID, id string) string {\n  mac := hmac.New(sha256.New, []byte(id))\n  mac.Write([]byte(appID))\n  return fmt.Sprintf(\"%x\", mac.Sum(nil))\n}\n```\n\nOr simply use the convenience API call:\n\n```golang\nhashedID, err := machineid.ProtectedID(\"myAppName\")\n```\n\n## Snippets\n\nDon't want to download code, and just need a way to get the data by yourself?\n\nBSD:\n\n```bash\ncat /etc/hostid\n# or (might be empty)\nkenv -q smbios.system.uuid\n```\n\nLinux:\n\n```bash\ncat /var/lib/dbus/machine-id\n# or when not found (e.g. Fedora 20)\ncat /etc/machine-id\n```\n\nOS X:\n\n```bash\nioreg -rd1 -c IOPlatformExpertDevice | grep IOPlatformUUID\n```\n\nWindows:\n\n```batch\nreg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid\n```\nor\n* Open Windows Registry via `regedit`\n* Navigate to `HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography`\n* Take value of key `MachineGuid`\n\n## Credits\n\nThe Go gopher was created by [Denis Brodbeck](https://github.com/denisbrodbeck) with [gopherize.me](https://gopherize.me/), based on original artwork from [Renee French](http://reneefrench.blogspot.com/).\n\n## License\n\nThe MIT License (MIT) — [Denis Brodbeck](https://github.com/denisbrodbeck). Please have a look at the [LICENSE.md](LICENSE.md) for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenisbrodbeck%2Fmachineid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdenisbrodbeck%2Fmachineid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenisbrodbeck%2Fmachineid/lists"}