简体   繁体   中英

How to create a function that generates a unique password for each machine in linux?

I need to program a code that generate a unique password such that every time the machine (beaglebone black) runs the output be always the same.

In addition, if someone compiles his own linux in the beagle and runs the program, the output be different.

In summary, the password will be unique for each machine and operating system.

I thought to do that using as argument the ethernet MAC address, but I need something else that identifies the OS.

Drawing on Sami Laine's suggestion and taking into account that you will want to automate it, you need two more functions: (1) one to get the OS (+release number) and (2) one to get the first mac address.

(1) is easy enough. (Get Kernel and kernel release number:)

uname -sr

(2) is more tricky as the device will have different names depending on what linux you have, what kind of network devices you have on the machine etc etc (eth0, wlan0, ath0, eth6, enp3s0, wlp5s0, ...). To keep it simple, let's just use the first one. The network devices are all listed (as directories) in

/sys/class/net/

in each of which (directories) you will find the mac address of the respective device in a file named "address". We list all of them and only use the first one:

cat /sys/class/net/*/address|head -1

Now, we just integrate this into the command proposed by Sami Laine (which has echo repeat a line with a unique secret, eg a phrase borrowed from your favorite novel, the kernel/version and the mac address and pipes it into openssl to digest and encode it into a password that is unique for the string piped into it by echo but does not allow to easily guess the string from which it is generated.)

echo "Your favorite quote from your favorite novel:$(uname -sr):$(cat /sys/class/net/*/address|head -1)"|openssl ripemd160 -binary|openssl base64

Note that you can include commands into echo's argument-string in brackets and preceded by $; echo will then include the output of these commands in the position in the string.

Here, I assumed that you only want the passwords to differ between OS's and machines (ie to be the same if you recompile/reinstall the same OS on the same machine) ... otherwise you could perhaps include the inode (that will likely be unique depending on filesystem, installation procedure etc etc) of a file that will likely not change over the course of an OS's existence, say /etc/hostname. Like so

ls -i /etc/hostname

... and included into the command above:

echo "Your favorite quote from your favorite novel:$(uname -sr):$(cat /sys/class/net/*/address|head -1):$(ls -i /etc/hostname)"|openssl ripemd160 -binary|openssl base64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM