How to Dynamically Generate an SSH Host Key for :ssh.daemon/2,3 in Erlang/Elixir?

Hi everyone,

I’m trying to start an SSH daemon in Elixir using :ssh.daemon/2,3, and I want to dynamically generate an SSH host key instead of relying on a pre-existing key file.

I initially tried to do in elixir

# generate keys
{pub, priv} = :crypto.generate_key(:rsa, {2048, 65537})
 port = 8989

    sftp_dir = Path.join(System.tmp_dir!(), "sftp_dir")
    File.mkdir(sftp_dir)

    opts = [
      {:system_dir, to_charlist("/tmp")},
      {:user_passwords, [{~c"user", ~c"password"}]},
      {:subsystems, [:ssh_sftpd.subsystem_spec([{:root, to_charlist(sftp_dir)}])]}
    ]

    :ssh.daemon(port, opts)

However, I am unsure how to pass the generated key to :ssh.daemon/2,3 as an option.

  1. Am I generating the key correctly for use with :ssh.daemon/2,3?
  2. How can I pass the generated key to :ssh.daemon/2,3 so that it does not require a file-based key?

I appreciate any guidance on this!

I am not very familiar with the ssh application, but it looks like you can provide a key callback function: ssh — ssh v5.2.8