23

What is the best method for editing the /root/.ssh/authorized_keys file?

I mean if I need to append a new key to my authorized_keys file, than what are the best methods to do that?

n0pe
  • 9,411
  • 13
  • 60
  • 108
LanceBaynes
  • 39,295
  • 97
  • 250
  • 349

2 Answers2

15

Try just to edit the file and paste key on the end.
You want automated? Try from server where you want to add key to do:

ssh-copy-id -i id_rsa.pub login@hostname

You can always try ssh-add on the server where you have an authorized_keys file stored.

Plenty of opportunities :D

Mat
  • 51,578
  • 10
  • 158
  • 140
Sebastian Szary
  • 350
  • 1
  • 4
0

Each line is an independent entry. You can grep them to filter, sed -i~ /pattern/d authorized_keys to delete lines, say all those from some server, or cat new-entries >> authorized_keys to add lines to the end.

Charles Stewart
  • 502
  • 1
  • 5
  • 18
  • there is two problem with "cat new-entries >> authk. – LanceBaynes Dec 18 '11 at 15:34
  • 1
    1) people could mistype ">>" to ">" so the file is gone.. – LanceBaynes Dec 18 '11 at 15:35
  • 1
    2) if there isn't any newline char in the authorized_keys file, then cat/echo will append the new public key to the end of the last line... :\ – LanceBaynes Dec 18 '11 at 15:35
  • 2
    The `cat` method is the one most frequently used and suggested. If it doesn't end in a newline, [it's not a well-formed text file](http://unix.stackexchange.com/questions/18743/whats-the-point-in-adding-a-new-line-to-the-end-of-a-file). – Kevin Dec 18 '11 at 17:53
  • @Lance: Wrt. 1, I can't say I've ever done that, but if you are prone to this kind of thing, try using `sed -i~ 'r$ new-entries'`, since that creates a backup file. Wrt. 2., this is easily enough fixed with a text editor, and as Kevin says, you should pay a bit of attention to the semantics of what you are doing. – Charles Stewart Dec 18 '11 at 18:55