Copy an SSH public key to the clipboard

Copy an SSH public key on macOS, Linux, Windows, or WSL, and preserve the complete key without exposing its private counterpart.

Best practices
Illustration of application modules working together

Copy the public key file, normally ending in .pub. On macOS, for example:

pbcopy < ~/.ssh/id_rsa.pub

Use your actual filename. An Ed25519 key commonly has the name id_ed25519.pub; the commands below work the same way after changing the path.

Choose the command for your desktop

On an X11 Linux desktop, use xclip or xsel:

# Debian or Ubuntu, if xclip is not installed.
sudo apt install xclip
xclip -selection clipboard < ~/.ssh/id_rsa.pub

# Alternative when xsel is installed.
xsel --clipboard < ~/.ssh/id_rsa.pub

On a Wayland desktop with wl-clipboard installed:

wl-copy < ~/.ssh/id_rsa.pub

On Windows, Git Bash can use the Windows clipboard utility:

clip < ~/.ssh/id_rsa.pub

In PowerShell:

Get-Content "$HOME/.ssh/id_rsa.pub" | Set-Clipboard

In WSL, when Windows interoperability is enabled:

cat ~/.ssh/id_rsa.pub | clip.exe

The Linux tools need a graphical session and access to its clipboard. Installing xclip on a headless server does not give it access to your laptop's clipboard.

Copy manually when necessary

Print the public key and select the entire line:

cat ~/.ssh/id_rsa.pub

Keep the key type, such as ssh-ed25519 or ssh-rsa, and the complete Base64 field. The trailing comment is optional. A terminal may visually wrap the line; do not introduce actual line breaks into the key when pasting it.

For GitHub, open your account's SSH and GPG keys settings and add a new SSH key. For a server, append the public key to the intended account's ~/.ssh/authorized_keys. Never paste the corresponding private file, such as id_rsa, into either destination.

Avoid unnecessary clipboard steps

If you already have authorized access to the server, ssh-copy-id can install a selected public key directly:

ssh-copy-id -i ~/.ssh/id_rsa.pub user@server.example.com

This still requires a working authentication method and permission to update the target account's authorized keys. It does not grant access to an account you cannot already administer.

If a clipboard command is missing, install the package appropriate to your distribution or copy the printed public key manually. Run the clipboard command on the computer whose clipboard you intend to use.

See GitHub's SSH key instructions.

Related reading

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting