How to copy SSH keys to a server, clipboard or new PC

Copy your public SSH key to a server with ssh-copy-id or PowerShell, to the clipboard for GitHub, and move a key pair to a new computer safely.

5–8 minutes
Hands typing on a laptop keyboard

To copy an SSHSSH A secure way to type commands on a server from your own computer, as if you were sitting at it. More about SSH → key you almost always copy the public half: to a server, to your clipboard for a site such as GitHub, or occasionally to a new computer. This guide is for anyone who has made a key and now needs to put it somewhere, and wants to do it without leaking the private half.

To put your key on a server, run **ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server** on macOS or Linux, or pipe the .pub file into ssh from PowerShell on Windows. To paste it into a website, copy the contents of the **.pub file** to your clipboard with pbcopy, Set-Clipboard or xclip. Only copy the private key when moving to a new computer, and then fix its permissions to **600**.

Which file you should be copying

An SSH key pair is two files that sit side by side, usually in the .ssh folder in your home directory. If you are new to them, the guide in Related below explains the idea in full.

FileWhat it isSafe to copy?
id_ed25519.pubPublic key, one line of text starting ssh-ed25519Yes, to any server or service
id_ed25519Private key, starts -----BEGIN OPENSSH PRIVATE KEY-----Only to another computer you own
id_rsa.pub / id_rsaThe same pair for an older RSA keySame rules as above

A quick check before you copy anything: list the folder and look for the .pub ending.

ls -l ~/.ssh

On Windows, the same folder is C:\Users\yourname\.ssh, and ls works in PowerShell too.

If a website, a colleague or a support form asks for your “SSH key”, they mean the .pub file. Nobody legitimate ever needs your private key. Pasting it anywhere gives the reader the same access you have.

Copy your public key to a server

This is the copy that lets you log in without a password. The server keeps a list of allowed public keys in ~/.ssh/authorized_keys inside the home folder of the account you log in as. Copying your key means adding one line to that file. The full setup, including switching password login off afterwards, is in how to set up SSH keys.

On macOS or Linux: ssh-copy-id

ssh-copy-id logs in with your password once, creates the .ssh folder and authorized_keys file if they are missing, sets their permissions and appends your key.

ssh-copy-id -i ~/.ssh/id_ed25519.pub exampleco@203.0.113.10

If the server listens on a portPort A numbered door on a server. Each service listens behind its own door: web pages on 443, email on others, remote login on 22. More about Port → other than 22, add -p:

ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 exampleco@203.0.113.10

You will be asked for the account password. Success looks like Number of key(s) added: 1. If it says the key already exists on the remote system, it was already there and nothing was changed.

Leaving out -i makes ssh-copy-id copy every default public key it finds in ~/.ssh, which is rarely what you want if you keep several keys.

On Windows: pipe the key over SSH

Windows includes the OpenSSH client but not ssh-copy-id. This single PowerShell command does the same job:

type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh exampleco@203.0.113.10 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

It reads your public key, sends it through the SSH connection, and on the server appends it to authorized_keys with the right permissions. The >> matters: a single > would replace the file and remove every other key already in it. If you use Git Bash, ssh-copy-id is usually included there, so the macOS and Linux command works as well.

By hand, if you cannot run commands from your computer

Some hosting control panelsControl panel A website you log into to manage your hosting with buttons and forms, instead of typing commands. cPanel and Plesk are the common ones. More about Control panel → and web consoles only give you a terminal on the server. In that case, copy the public key to your clipboard (next section), log in to the server, and run:

mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys

Paste the key on a new line at the end of the file, save, then set the permissions:

chmod 600 ~/.ssh/authorized_keys

Each key must stay on one single line. If your editor wraps or splits it, the key will not match.

Test the copy

Open a new terminal and connect:

ssh -i ~/.ssh/id_ed25519 exampleco@203.0.113.10

If you are logged straight in, or asked only for your key passphrase rather than the account password, the copy worked. Keep your existing session open until you have confirmed this. The first time you connect you may also see a host key prompt, which is normal.

Copy your public key to the clipboard

GitHub, GitLab, cloud provider dashboards and many control panels ask you to paste your public key into a box. Copy it with a command rather than through a word processor, which can add line breaks.

SystemCommand
macOSpbcopy < ~/.ssh/id_ed25519.pub
Windows PowerShellGet-Content $env:USERPROFILE\.ssh\id_ed25519.pub piped to Set-Clipboard
Windows Command Promptclip < %USERPROFILE%\.ssh\id_ed25519.pub
Linux desktop (X11)xclip -selection clipboard < ~/.ssh/id_ed25519.pub
Linux desktop (Wayland)wl-copy < ~/.ssh/id_ed25519.pub

The PowerShell version written out in full is:

Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboard

On Linux, xclip and wl-copy may need installing first (sudo apt install xclip or sudo apt install wl-clipboard on Ubuntu and Debian). If you are on a server with no desktop, simply print the key and select it with your mouse:

cat ~/.ssh/id_ed25519.pub

What you paste should be one line in three parts: the key type (ssh-ed25519), a long string of letters and numbers, and a comment such as your-laptop. For GitHub specifically, the steps after pasting are in the GitHub guide listed below.

Copy your keys to a new computer

When you replace a laptop, the cleaner choice is usually to create a new key pair on the new machine, add its public key to your servers and services, and remove the old one. Each device then has its own key, and losing one device means revoking one key, not all of them.

If you do need to move an existing pair, for example because it is registered with many servers you cannot easily reach, copy both files over a channel you trust: an encrypted USB drive, or scp from the old machine to the new one if the new one runs an SSH server.

scp ~/.ssh/id_ed25519 ~/.ssh/id_ed25519.pub newuser@198.51.100.20:~/.ssh/

Never send a private key by email, chat or a file sharing link.

Fix permissions on the new computer (macOS and Linux)

SSH refuses to use a private key that other users can read. After copying, run:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

On Windows, if you see UNPROTECTED PRIVATE KEY FILE, open the file’s Properties, then Security, Advanced, disable inheritance and leave only your own account with access.

Common problems

SymptomCauseFix
ssh-copy-id: command not foundYou are on Windows, or a minimal system without itUse the PowerShell pipe command above, or Git Bash
Still asked for the password after copyingWrong account, wrong key offered, or loose permissions on the serverCheck you copied to the same user you log in as, use -i to name the key, and set ~/.ssh to 700 and authorized_keys to 600
Permission denied (publickey)Password login is off and your key is not in authorized_keysAdd the key through your host’s web console or control panel
UNPROTECTED PRIVATE KEY FILEPrivate key readable by other userschmod 600 on macOS and Linux, or tighten file permissions on Windows
Website says the key is invalidYou pasted the private key, or the line was splitCopy the .pub file again with a clipboard command

Official documentation

Get the next guide

One email when there is something worth reading: new guides, errors going around, and hosting changes that affect your site.

No tracking pixels, and one click to leave.

Something out of date? Software changes. If a step no longer works, tell us and we will check it and update the page.