SSH: How to copy my SSH key to a remote host

To copy your SSH key to a remote host, you can use the ssh-copy-id command, which is a convenient way to install your public key on a remote server. Here are the steps to do this:

  1. Generate SSH Key (if you haven’t already): If you don’t have an SSH key pair (public and private keys) already, you can generate one using the ssh-keygen command. Open a terminal on your local machine and run: ssh-keygen -t rsa -b 4096 -C "your_email@example.com" Replace "your_email@example.com" with your actual email address. This command will generate a new SSH key pair and store it in the default location (~/.ssh/id_rsa for the private key and ~/.ssh/id_rsa.pub for the public key).
  2. Copy the Public Key to the Remote Host: Use the ssh-copy-id command to copy your public key to the remote host. Replace your_username and remote_host with the appropriate values:bashCopy codessh-copy-id your_username@remote_host You’ll be prompted to enter your password on the remote host for authentication.Note: If you’re using a custom SSH port, you can specify it with the -p option, like this: ssh-copy-id -p custom_port your_username@remote_host
  3. Authenticate with Your SSH Key: After successfully copying the SSH key to the remote host, you can now log in without entering a password. The SSH key authentication will be used instead.Test this by trying to SSH into the remote host: ssh your_username@remote_host You should be logged in without being prompted for a password.

By following these steps, you’ve securely copied your SSH key to the remote host, allowing you to log in without needing to enter a password each time. This is a more secure and convenient way to access remote servers.