Skip to content
Go back

Using the Clipboard from the Linux Terminal

· Updated:
By KingPin 6 min read
Using the Clipboard from the Linux Terminal

You SSH into a server. You generate an SSH key. Now what? Ctrl+C doesn’t work. There’s no mouse. How do you get that key into your clipboard?

Welcome to terminal clipboard hell. Except it’s not hell, it’s actually straightforward once you know the tools.

The Players

X11 (older): Uses xclip or xsel. Still dominant on desktop Linux.

Wayland (newer): Uses wl-copy and wl-paste. Growing adoption.

macOS: Uses pbcopy and pbpaste. Not relevant here, but handy to know.

SSH/remote: Need to forward clipboard or use tmux integration tricks.

xclip (X11)

Terminal window
# Copy
echo "my secret key" | xclip -selection clipboard
# Paste (pointless but works)
xclip -selection clipboard -o
my secret key

The -selection clipboard is important. X11 has three clipboards:

For “normal” copy-paste, use clipboard. Most tools expect it.

Shorthand:

Terminal window
# Uses primary selection (not clipboard) — paste with middle-click
cat /etc/hosts | xclip

xsel (X11 Alternative)

xsel is older, sometimes pre-installed:

Terminal window
# Copy
echo "data" | xsel --clipboard --input
# Paste
xsel --clipboard --output
# Clear
xsel --clipboard --clear

Honestly, xclip is more intuitive. Use it if available.

wl-copy and wl-paste (Wayland)

Modern systems use Wayland. If xclip doesn’t work, try:

Terminal window
# Copy
echo "secret" | wl-copy
# Paste
wl-paste
secret

No flags needed. Clean and simple.

Which Do You Have?

Terminal window
# Check for Wayland
echo $WAYLAND_DISPLAY
/run/user/1000/wayland-0
# Check for X11
echo $DISPLAY
:0

Both? Try wl-copy first, fall back to xclip:

Terminal window
copy() {
if command -v wl-copy &> /dev/null; then
wl-copy
elif command -v xclip &> /dev/null; then
xclip -selection clipboard
else
echo "No clipboard tool found" >&2
return 1
fi
}
paste() {
if command -v wl-paste &> /dev/null; then
wl-paste
elif command -v xsel &> /dev/null; then
xsel --clipboard --output
else
echo "No clipboard tool found" >&2
return 1
fi
}
# Usage
echo "hello" | copy
paste
hello

Add to your .bashrc or .zshrc.

Copy Your SSH Key (The Classic Move)

You just generated a key on a server and need to paste it into GitHub:

Terminal window
cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard
# Or with wl-copy
cat ~/.ssh/id_ed25519.pub | wl-copy

Now Ctrl+V in your browser. Magic.

Remote SSH: Doesn’t Work Natively

SSH into a server:

Terminal window
ssh user@server
user@server$ echo "secret" | xclip
# ✗ Error: Can't open display

Why? The display/Wayland socket isn’t forwarded. Options:

1. Use SSH agent + local clipboard wrapper

~/.local/bin/remote-copy
# On your local machine, create a wrapper
#!/bin/bash
# Copies from stdin to local clipboard over SSH
xclip -selection clipboard

Then on the remote, pipe to:

Terminal window
remote$ echo "secret" | ssh -T local-user@local-machine 'cat | xclip -selection clipboard'

Awkward. Not recommended.

2. Use tmux (Better)

If you’re using tmux locally, tmux can access your clipboard:

Terminal window
# In tmux: bind to copy to local clipboard
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard"

Now when you select text in tmux and press y, it goes to your local clipboard. Works over SSH too!

3. Just don’t use clipboard

Honestly? Copy-pasting keys is overrated. Use ssh-copy-id:

Terminal window
# From your local machine, copy your public key to the server
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
# Now SSH without a password
ssh user@server

Much better than clipboard gymnastics.

pbcopy on Linux (Make It Work)

Some scripts expect pbcopy. Create an alias:

~/.bashrc
if command -v xclip &> /dev/null; then
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
elif command -v wl-copy &> /dev/null; then
alias pbcopy='wl-copy'
alias pbpaste='wl-paste'
fi

Now scripts expecting macOS pbcopy will work on Linux.

Install Missing Tools

Terminal window
# Debian/Ubuntu
sudo apt install xclip xsel wl-clipboard
# Fedora
sudo dnf install xclip xsel wl-clipboard
# Arch
sudo pacman -S xclip wl-clipboard

Bottom Line

Simple. No mouse required.

Clipboard in Scripts

You can use clipboard tools in scripts too:

Terminal window
# Copy command output to clipboard
ps aux | grep nginx | wl-copy # Wayland
ps aux | grep nginx | xclip -selection clipboard # X11
# Read from clipboard (pipe it into a command)
wl-paste | grep "error" | head -5
xclip -selection clipboard -o | wc -l

Useful for quick one-liners when you want to paste a result somewhere without scrolling through terminal output.

Common Questions

How do I copy my SSH public key to the clipboard from the terminal?

Pipe the public key file into your clipboard tool. On X11, run cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard. On Wayland, run cat ~/.ssh/id_ed25519.pub | wl-copy. Then press Ctrl+V in your browser.

Should I use wl-copy, xclip, or xsel?

Use wl-copy on Wayland and xclip on X11. The wl-copy command needs no flags, while xclip needs -selection clipboard for normal Ctrl+V pasting. The xsel tool also works on X11 and is sometimes pre-installed, but xclip is more intuitive. If a system runs both display servers, try wl-copy first and fall back to xclip.

Why does xclip say “Can’t open display” over SSH?

The remote session has no forwarded X11 display or Wayland socket, so xclip has nothing to connect to. Two workarounds exist. In local tmux, bind a copy-mode key to copy-pipe-and-cancel with xclip -selection clipboard, which sends the selection to your local clipboard. Selecting text in tmux and pressing y then reaches that local clipboard even over SSH. Or skip the clipboard and run ssh-copy-id from your local machine to install your public key.

What is the difference between primary and clipboard in xclip?

X11 keeps separate buffers. The primary selection holds whatever you selected with the mouse, and you paste it with middle-click. The clipboard selection holds what you copied with Ctrl+C. Plain xclip uses primary, so pass -selection clipboard for normal copy-paste.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Docker Container Labels: The Metadata You're Ignoring
Next Post
Why Docker Builds Are Slow: Layer Cache Explained

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts