You never need to copy the entire ~/.ssh
directory, just the contents of the public part of your key(s). Easiest to do assuming password authentication is enabled would be
user@darkstar ~ $ ssh-copy-id remoteusername@remotehostname
And let ssh worry about getting everything in the right spot.
If you need to do it manually, or if you don't have password auth and you must have the sysadmin put the key in place, etc then you need to copy your ~/.ssh/id_rsa.pub
file. Once that is on the remote machine, simply add the content to whatever is already in the ~/.ssh/authorized_keys
file under the appropriate user account. A simple cat id_rsa.pub >> ~/.ssh/authorized_keys
will work.
Finally, you want to ensure the permissions on the private key, the authorized_keys
file, and the ~/.ssh
directories on both machines are set appropriately - the ~/.ssh
directory should be set to 0700 and the files inside of it should be 600.
To fix your issue, log in to the remote machine, rename the existing ~/.ssh
directory to something else, re-create it and set permissions properly, log out, and use the ssh-copy-id
from above. At that point, you should be able to ssh using your key and whatever passphrase is associated with it.
Edit for updated question You are missing the key pair concept I think. Think about house door locks - in order for one key to open them all, they have to have the same pin combo on the inside.
The id_rsa.pub file for any given combo goes into the authorized_keys file on the remote machine. In order for that key to work, you must have the corresponding private key on the machine you are connecting from. You can deal with multiple keys by having them in different files and using the -i
option for ssh (et al) to specify which private key to connect to.
So, using your information you need to get the private key file from the laptop (probably ~/.ssh/id_rsa
) and put it on Server B, call it something like ~/.ssh/laptop_id_rsa
). When you ssh from Server B to Server A you can do with ssh -i ~/.ssh/laptop_id_rsa user@serverA
. If you want to ssh from A to B, repeat the process putting the private key on A. Alternatively you can use server B as a jump host/tunnel so once you connect there with the correct options you can then from your laptop ssh directly to Server A. This keeps your private key file only on your laptop (good thing for security, you don't want copies floating around, especially with a passwordless key!)
You'll also likely want to set up a ~/.ssh/config
file so you can simply do ssh serverA
and have it use the appropriate hostname, port, username, identity file, etc.
ssh-keygen -t rsa 4096
. 2) Copy the contents of/root/.ssh/id_rsa.pub
key e.g from server A to/root/.ssh/authorized_keys
on server B and vice versa. Also, there isssh-copy-id
and might be useful in your case.