SSH Clipboard Sync Between Macs: Real Methods
In this article
Yes, you can transfer clipboard text between Macs over SSH, but there is no built-in macOS command that does it directly. The standard approach is to pipe the clipboard through SSH using pbcopy and pbpaste, which are macOS utilities that read and write the system clipboard. This article explains the practical methods, their limitations, and how to choose the right one.
Understanding pbcopy and pbpaste
macOS includes two command-line tools that interact with the clipboard: pbcopy takes standard input and places it on the clipboard, and pbpaste outputs the current clipboard contents to standard output. These tools are the foundation for any SSH-based clipboard transfer because they let you treat the clipboard as a text stream.
pbcopy— reads from stdin, writes to clipboardpbpaste— reads clipboard, writes to stdout
When you combine these with SSH, you can send text from one Mac to another. For example, to copy the local clipboard to a remote Mac's clipboard:
pbpaste | ssh user@remote-mac "pbcopy"And to pull the remote clipboard to your local Mac:
ssh user@remote-mac "pbpaste" | pbcopyThese commands work over any SSH connection, including over the internet or a local network, as long as SSH access is configured.
Setting Up SSH Access Between Macs
Before you can transfer clipboards, both Macs need SSH configured. On macOS, SSH is built in but often disabled by default. To enable it on the remote Mac, go to System Settings → General → Sharing and turn on Remote Login. This starts the SSH server.
On the local Mac, you need the remote Mac's IP address or hostname and a valid username. You can find the IP address on the remote Mac in System Settings → Network. For a more permanent setup, consider configuring SSH keys so you don't have to type a password each time.
- Generate an SSH key on the local Mac:
ssh-keygen -t ed25519 - Copy the public key to the remote Mac:
ssh-copy-id user@remote-mac - Test the connection:
ssh user@remote-mac 'echo ok'
Once SSH works without a password, the clipboard commands become seamless.
Using SSH Aliases for Convenience
Typing the full SSH command every time is tedious. You can create shell aliases or functions in your ~/.zshrc (macOS uses zsh by default) to simplify the process.
alias clip-to-remote='pbpaste | ssh user@remote-mac "pbcopy"'alias clip-from-remote='ssh user@remote-mac "pbpaste" | pbcopy'After adding these, restart your terminal or run source ~/.zshrc. Now you can type clip-to-remote to send the current clipboard, or clip-from-remote to fetch the remote clipboard.
If you use multiple remote Macs, you can create a function that takes a hostname as an argument:
function clip-send() { pbpaste | ssh "$1" "pbcopy"; }function clip-get() { ssh "$1" "pbpaste" | pbcopy; }Then use clip-send user@mac2 or clip-get user@mac2.
Handling Binary or Large Clipboard Data
The methods above work only for plain text. If your clipboard contains images, files, or rich content, pbpaste may output binary data or nothing useful. For text, it's fine, but for images you need a different approach.
For images, you can use pngpaste (a third-party tool) to extract a PNG from the clipboard, then transfer the file over SSH with scp or rsync. Similarly, to set an image on the remote clipboard, you'd need to copy the file and use a tool like osascript to set it, which is complex.
For large text (e.g., log files), the command still works, but be aware that the entire clipboard is transferred in one go. If the clipboard is enormous (hundreds of MB), you might hit memory limits. In practice, text clipboards are usually small.
Security Considerations
SSH encrypts all data in transit, so your clipboard contents are protected from eavesdropping on the network. However, be mindful of what you copy—if you send a password or sensitive data to a remote machine, it will sit on that machine's clipboard, which could be accessed by other users if the machine is shared.
Also, ensure that your SSH keys are protected with a passphrase and that you don't share them. If someone gains access to your local Mac, they could potentially use your SSH connection to read the remote clipboard.
When using SSH over the internet, always use key-based authentication and disable password authentication if possible. This reduces the risk of brute-force attacks.
Alternatives to Manual SSH Commands
If you need clipboard sync frequently, a manual SSH command might not be efficient. Several tools automate this with a single keystroke or menu bar item:
- Synergy — shares clipboard and mouse/keyboard across multiple computers on the same network.
- Barrier — a fork of Synergy that works similarly.
- KDE Connect — primarily for mobile-to-desktop, but also works between desktops on the same network.
- Unison — file sync, but not clipboard-specific.
These tools are more user-friendly but require installation on both Macs and may need network configuration. They also handle images and other clipboard formats better than SSH.
For a lightweight solution, you could write a small script that listens for clipboard changes and automatically sends them via SSH, but that's more complex and may introduce latency or conflicts.
Frequently Asked Questions
Can I transfer clipboard without SSH?
Yes, you can use third-party apps like Synergy or Barrier, or cloud-based clipboard managers that sync across devices. However, SSH is free, built-in, and works over any network, making it a reliable choice for text.
Does this work over the internet?
Yes, as long as the remote Mac is reachable via SSH (e.g., port 22 forwarded or using a VPN). You'll need the public IP or a dynamic DNS hostname.
What if the clipboard contains formatting?
Plain text only. For rich text or images, you'd need to convert to a plain format first or use a different tool.
Why does pbpaste output nothing?
This usually means the clipboard is empty or contains non-text data. Try copying plain text first. Also, ensure the SSH command is correct and the remote session has access to the clipboard.
Is there a way to sync clipboard automatically?
Yes, you can use a tool like autossh with a script that monitors clipboard changes, but it's not trivial. Third-party apps offer this out of the box.