2

Besides using ssh-copy-id client side or manually pasting your key into authorized keys server side, is there a way once you have already logged into SSH with your password to automatically add your key?

Presumably your public key has already been sent when you tried to login, so is there a way to tell the SSHD daemon to add the key to the authorized keys?

4
  • why do you want to do that?
    – drum
    Nov 30, 2021 at 3:10
  • @drum I don't use ssh for security but only because it's the default remote access for most things i tinker with, if telnet was around I would still be using it, I don't have any one "main" machine that I use when I tinker and i'm constantly having to generate keys, copy keys, del known_hosts when they change (a seperate issue), and often I'm on old macs that don't even have ssh-copy-id . I want to make a script so on login if my key isn't installed it will install it so I don't have to use my password next time, among other things.
    – UserZer0
    Nov 30, 2021 at 3:19
  • If you can ssh then you can sftp.You can write a script that takes in the new host, accept new fingerprint, then sftp the file into the .ssh directory.
    – drum
    Nov 30, 2021 at 3:28
  • 1
    @drum except I'm looking to do that "server" side. See the clients are always changing as well but I'll always have the password for the server, I've also had issue trying to copy keys manually on some setups. Anyway I was really more interested in weather it was possible to retrieve this information from the sshd daemon since obviously the key does get sent when it trys to connect and then asks for the password after that. ssh-copy-id is it's self just a script I could download on new clients , but in certain setups I have no internet but can ssh locally.
    – UserZer0
    Nov 30, 2021 at 4:01

2 Answers 2

3

No.

When you log in with a password your private SSH key is not used (i.e.: you can log in with password authentication even if you don't have a private key).

Thus there is no public key for the server use.

0
0

To add your public key to a server without use of the ssh-copy-id command.

Method 1 (This overwrites the origial file but gets the job done):

scp $HOME/.ssh/id_rsa.pub [email protected]:~/.ssh/authorized_keys

Method 2 (this is basically what the copy-ssh-id command does anyway):

ssh [email protected] 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub

Edit: I missed the bit of the question where the op is already logged into the server, but the question asked about not using the ssh-copy-id command. Both methods can be used if you do not have the copy-ssh-id command available locally like an embedded system or router.

4
  • You misread this question. It states, “Besides using ssh-copy-id client side or manually pasting your key into authorized keys server side…” So what you are outlining is actually the equivalent of the “manually pasting” method. Jan 3 at 20:07
  • “…but the question asked about not using the ssh-copy-id command.” You completely missed the part where they don’t want to manually copy and paste anything: “…or manually pasting your key into authorized keys server side…” This is not a question simply about ssh-copy-id but is asking about a method that does not exist. Jan 3 at 20:34
  • 1
    @Giacomo Yes but when you read the comments from the op, you get the bigger picture as to why the question was asked. The op did not have access the the command locally and frequently logged in from different clients, hence needing another method perhaps server side. The initial question was misinterpreted as i had already stated in my edit, but this is the solution for the op. I don't post unless it is useful to the op, you got to see it from the op's point of view, how were your comments in anyway helpful to the op?
    – 5p0ng3b0b
    Jan 3 at 21:00
  • 1
    Good point. Fair enough. Jan 3 at 21:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .