When working with Linux or Unix servers, SSH keys offer a secure and convenient way to connect to remote servers without having to enter a password each time. However, the common method to copy SSH keys to a remote server is by using the ssh-copy-id
command. But what to do if this command is not available? In this article, we will show you how to manually copy your SSH key to a remote server when ssh-copy-id
is not an option.
To copy your SSH key to a remote server using a single command (without using ssh-copy-id
), you can use the cat
command in combination with ssh
. This approach allows you to read your local public key and add it directly to the ~/.ssh/authorized_keys
file on the remote server in one step. Here's how to do it:
cat ~/.ssh/id_rsa.pub | ssh user@server-address "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
cat ~/.ssh/id_rsa.pub
: Read the contents of your local SSH public key.|
: Pass the output of the previous command to the next command.ssh user@server-address
: Connects to the remote server where you intend to copy your SSH key."mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
: This command string executed on the remote server does the following:mkdir -p ~/.ssh
: Creates the.ssh
directory in the user's home directory if it does not already exist.cat >> ~/.ssh/authorized_keys
: Adds the public key to theauthorized_keys
file. If the file does not exist, it creates it.
Be sure to replace user
with your username on the remote server and server-address
with the IP address or hostname of the server you are connecting to.
This command will allow you to do the entire process in one step, making SSH key transfer quick and hassle-free.