Is there a single line command to tell SSH to add a provided public key to the local machine's authorized_keys
file? A local version of ssh-copy-id
?
I am writing a chef recipe and want to ensure a specific ssh public key is set for a certain user. I could overwrite the ~/.ssh/authorized_keys
file each time, or attempt to some hacky way to add the line, but if there's an official command, it'll be more robust and prevent duplication.
Something like:
ssh-add-local-key "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDeblahdeblah user@somwhere"
For apt systems like Ubuntu or Debian there is an apt-add-repository
command, so I wonder if there's a SSH equivalent.
I know I could use "echo blah >> authorized_keys", but I want something idempotent, which I can run regularly. With >>
the file will grow in size every time.
cat
or similar to append the provided key to the keyfile. I agree that overwriting would be very bad, but you can avoid a specialist command without overwriting.ssh-copy-id you@localhost
. Or you build some script (possiblygrep -v
the new key in the file and then append it to the output or similar), but be careful not to lock out yourself while testing it.