Ever wanted to copy and paste from a Linux machine you’re SSH’d into? Then read on!
If you haven’t done already, take a look at my previous post to use wsltty to view the local WSL installation, which is then used to ssh into a remote box.
For the purpose of this explanation the client is the machine you’re sitting at, and the server is the remote machine you’re connecting to.
First, we need to get SSH X11 Forwarding working. This would normally be used for running graphical applications, but we’re just using it for the clipboard feature.
You should have these lines uncommented in
X11Forwarding yes X11DisplayOffset 10 X11UseLocalhost no
When you’re done restart the ssh daemon, on my (Debian) system the command is this:
sudo systemctl restart ssh
If you’re running a firewall on the server you need to enable loopback. I’m using iptables, so this is how I’d do it:
sudo iptables -I INPUT -i lo -j ACCEPT sudo iptables -I OUTPUT -o lo -j ACCEPT su - iptables-save > /etc/iptables/rules.v4 exit
From within WSL (but not ssh’d into a remote system), Install xauth and xterm:
sudo apt install xauth xterm
A big thanks to this video for help with the next section: youtube.com/watch?v=_MgrjgQqDcE
In Windows, download and install VcXsrv https://sourceforge.net/projects/vcxsrv/
Find XLaunch in the start menu and run it.
On the first screen choose Multiple Windows and set the Display number to 0:
Choose Start no client on the next page:
Make sure Clipboard is turned on
Press Save configuration.
Type shell:startup in the address bar and hit enter – this will take you to a special folder – files saved here will run when a user logs in. The filename should be pre-populated as config.xlaunch. That’s OK so hit Save, and then Finish in the previous window.
Finally, go back to WSL. This command tells your local WSL (the client) where to find the display. SSH uses that information when you link to the remote computer (server).
You can also save this line into
~/.bashrc on the client to load it every time WSL starts.
Connect and Test
In WSL, connect to the server with a slightly modified command:
ssh -Y firstname.lastname@example.org
The -Y tells ssh you want to link the server to the VcXsrv software, so the server can display things on the client’s screen.
Let’s test with xeyes
sudo apt install xeyes xeyes
A pair of eyes should appear on your screen, and follow the mouse around.
Put it to use!
So, what we have now is that anything that goes onto the Linux system clipboard (xclip) should end up on the Windows clipboard too. Lets give it a try. Pipe some data to xclip:
echo "I'm copied, paste me!" | xclip -i
Now try pasting that into a text editor on windows.
Next, copy some text in windows, then go back to your ssh session and type:
The text you copied should appear on a new line in the terminal.
The real power of this comes when combined with terminal applications that are aware of the system clipboard. For me, I wanted to copy from
vim in an ssh session, and then paste into windows. If you’re using a version of vim with enough features (I use
vim-gtk), all you have to do is add this to your
" Yank goes to system clipboard, to aid with WSL copy/paste set clipboard=unnamedplus
Now, anything copied by the yank commands ends up in the windows clipboard too.