I'm automating key generation with ssh-keygen (OpenSSH v8.9), on a headless server. This is straightforward if no password is required, but gets difficult if you do need a password. ssh-keygen needs 3 things on stdin when generating a new key: y, because the keyfile already exists, and the required password, repeated twice.
With some simplification I'm doing this as follows:
$ printf -v input "y\npassword\npassword\n"
$ echo -n "$input"
y
password
password
$ echo -n "$input" | ssh-keygen -t ed25519 -a100 -f tmpkey
When I initially did this I got this output:
Generating public/private ed25519 key pair.
tmpkey already exists.
Overwrite (y/n)? ssh_askpass: exec(/usr/bin/ssh-askpass): No such file or directory
The key pair was created, but the private key wasn't encrypted. So, I installed ssh-askpass, on a test system with X. When I run ssh-keygen now, I get an OpenSSH authentication popup, and stdin is now ignored. Obviously, I can't have a graphical popup on a headless server, even if I wanted it.
So what's going on? Can I persuade ssh-keygen to automate password entry? If not, can I perhaps use openssl to add a password, or even create the file? I do want to get an OpenSSH-format key out of this - I'm already automating PKCS#8 with openssl.
EDIT
I've found the SSH_ASKPASS_REQUIRE envvar for ssh (not ssh-keygen). If I set it to 'never', it does stop the ssh-askpass popup, but ssh-keygen now only reads the first line of stdin, and ignores the password on the next two lines. So, it doesn't get me anywhere.