As far as my understanding, machine(A) ssh-keygen generates a key, and ssh-copy-id gives this key to another machine (B), is it?

But when this is done, to test whether this is done or not is using the ssh like: root@A: ssh root@B if login on B without password, then this works.

I am confused about this. A gives a key to B, why can A login on B without password? It should go like this, login on B, B ssh A without password.

Is my understanding wrong?

  • I also realise that if the server B is under the SELinux mode, you can't make it, you still need enter the password when you login on the B.
    – cdhit
    Apr 30 '16 at 21:03
  • ssh-copy-id does more or less cat ~/.ssh/id_rsa.pub | ssh user@some.domain "cat >> .ssh/authorized_keys".
    – TNT
    Feb 12 '18 at 17:36

An ssh key has two parts, a private part and a public part. That's why it's often called a 'key pair', a pair of keys that work together.

ssh-copy-id copies the PUBLIC portion of the private/public key-pair into ~/.ssh/authorized_keys on the remote host.

Anyone who has the private key (and knows the passphrase) can login to that remote host without a password.

To avoid having to retype the pass-phrase all the time, you can use a program like ssh-agent to hold the private keys for you (in simple terms, it caches the private key after you've unlocked it with the passphrase). It will keep the private key until you tell it not to, or you kill the agent, or logout. see man ssh-agent.

The PUBLIC portion of the key pair is not particularly sensitive, security wise. It can be distributed and disclosed without risk. Without the matching private key (and the private key's passphrase), it's pretty much useless.

The PRIVATE portion of the key-pair, of course, should be kept secret.

  • Sorry, I do not quite get your point? So in my case, A gave a public key to B, A hold the private key. Then it means A hold a key to the machine B?
    – cdhit
    Apr 29 '16 at 4:22
  • not quite. It means that machine B's root account is configured to accept key-based authentication (i.e. password not required) from anyone that has the private key. This is why you should be careful about keeping your private key private and secure.
    – cas
    Apr 29 '16 at 4:26
  • Thanks very much. I am not able to vote up now. When I am able to do this, I will be glad to vote your answer up.
    – cdhit
    Apr 29 '16 at 4:31

What you're missing is that ssh-copy-id doesn't just copy the public key to B: it adds the public key to the list of keys that allow access to the account (~/.ssh/authorized_keys). After running ssh-copy-id, the key is not just stored on B somewhere, it's registered on B as an authorized login method.

The private key is on A, and needs to be passed to the SSH client (this can be done by letting it search ~/.ssh/id_*, by passing a -i command line option, by using IdentityFile in ~/.ssh/config, or via ssh-agent). The public key is in ~/.ssh/authorized_keys on B. When you log into B from A, the SSH client sends a proof of possession of the private key to B; B links this proof of possession with a public key found in ~/.ssh/authorized_keys so it authorizes the login attempt.


A gives a key to B, why can A login on B without password?

You're right.

When SSH receives an incoming connection, it can authenticate the user in multiple ways. This is configurable. A typical way is to check whether the user has a public key in an ~/.ssh/authorized_keys* file, and to accept a matching private key (assuming that all details are accepted, such as a file having acceptable permissions). If no key is set up, or if the SSH client fails to provide details that acceptably prove that the SSH client has a matching private key, then the SSH server will prompt for a "password" (or "passphrase).

ssh-copy-id is basically a command that does a task which is not very difficult to do manually. It uses SSH to connect to the remote system, and gets the public key into the ~/.ssh/authorized_keys* file.

After I learned how to use ssh-copy-id, I quickly forgot, because the program just wasn't very useful for me, since I already learned how to manually do the equivalent tasks. Using ssh-copy-id was, admittedly, slightly easier/faster. However, it was very non-essential. I believe that whether the extra convenience is worth the effort to learn yet another command, is questionable.

ssh-copy-id may require that the user enters a password, just like what the user would need to do (using any other SSH client) if the user was doing this manually. It doesn't circumvent normal authentication requirements.

(Of course, after ssh-copy-id does its thing, then the SSH key is installed and can be used easily.)

It should go like this, login on B, B ssh A without password.

You can log onto B (using a password), and then use SSH (as a shell, or a protocol that uses SSH, like SCP/SFTP) to grab a key from A. Yes, that works as well. Whether you need a password, or not, would depend on A's setup. If A has a public key, and B has a corresponding private key, then B may not need to type a password.

cas's answer mentions ssh-agent (which was not specified in the question). That is basically another approach. The ssh-agent stores private keys. They may be encrypted, so you need to type a passphrase to gain access to the decrypted private keys. When you SSH, the SSH server will do something which the ssh-agent recognizes, and then the ssh-agent can provide the required details to the SSH server. The idea is that once ssh-agent is set up, after you type your passphrase once (to decrypt access to the keys), then you can make different connections all day long and don't need to keep re-typing your passphrase as long as ssh-agent is running. This is primarily about the experience of the end user who uses the SSH client, so there isn't a lot of work you need to do on the SSH server (other than setting up a public key, just like any of the other methods that have already been discussed).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.