You can copy a public key to a remote server using the ssh-copy-id
command, which simplifies the process of securely transferring your public key for passwordless SSH login.
Here's how to do it:
-
Use the
ssh-copy-id
command: Open your terminal and use the following command, replacingremote_user
with your username on the remote server andhost
with the server's address (IP address or hostname):ssh-copy-id remote_user@host
-
Enter the password: The command will prompt you for the password of the
remote_user
on thehost
. Enter the correct password when prompted. -
Verification: After successful execution, the
ssh-copy-id
command appends your public key (typically found in~/.ssh/id_rsa.pub
) to the~/.ssh/authorized_keys
file on the remote server. You should now be able to SSH into the remote server without entering a password.
Explanation:
-
ssh-copy-id
: This is a utility that simplifies the process of copying your public key to the remote server'sauthorized_keys
file. -
remote_user@host
: This specifies the username and address of the remote server. Replaceremote_user
with the actual username you use to log in to the server, andhost
with the server's hostname or IP address. -
~/.ssh/id_rsa.pub
: This is the default location for your public key. If your public key is stored in a different location, you can specify it using the-i
option. For example:ssh-copy-id -i ~/.ssh/my_key.pub remote_user@host
-
~/.ssh/authorized_keys
: This file on the remote server stores the public keys that are authorized to log in to the account without a password.
Alternative Method (Manual):
If ssh-copy-id
is not available (rare), you can manually copy the key:
-
Copy the public key to the clipboard:
cat ~/.ssh/id_rsa.pub
Select and copy the output to your clipboard.
-
SSH into the remote server:
ssh remote_user@host
-
Append the public key to
authorized_keys
:-
If the
~/.ssh
directory or~/.ssh/authorized_keys
file does not exist, create them:mkdir -p ~/.ssh chmod 700 ~/.ssh touch ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys
-
Append the copied public key to the
~/.ssh/authorized_keys
file:echo "PASTE_YOUR_PUBLIC_KEY_HERE" >> ~/.ssh/authorized_keys
Replace
PASTE_YOUR_PUBLIC_KEY_HERE
with the content you copied in step 1. Make sure to paste the entire key on a single line.
-
-
Secure the SSH directory and authorized_keys file
-
The .ssh directory should have permissions of 700 and the authorized_keys file permissions of 600. Ensure this by running the following commands:
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
-
-
Test the connection: Exit the SSH session and try to SSH back into the server. You should now be able to log in without a password.
Security Considerations:
-
Ensure that the permissions on the
.ssh
directory andauthorized_keys
file are correctly set. Incorrect permissions can prevent SSH login or, worse, introduce security vulnerabilities. -
Avoid copying your private key (
id_rsa
). Only the public key (id_rsa.pub
) should be copied to remote servers.
By using ssh-copy-id
or the manual method outlined above, you can easily and securely copy your public key to a remote server and enable passwordless SSH login.