X11 forwarding on Windows
Contents
This allows you to ssh from Windows machine and get two major benefits:
- Make use of X11 apps on the ssh server
- Make (primarily) remote vim to access system clipboard
Here's how. This guide uses the following setup:
- No need to install full Cygwin or MSYS
- Use Mintty/ssh that comes with Git on Windows, aka, git bash.
I mainly followed this guide.
Server setup
Ensure /etc/ssh/sshd_config
:
AllowAgentForwarding yes
AllowTcpForwarding yes
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost no
Restart sshd
with service ssh restart
(Debian) or service sshd restart
(FreeBSD)
Ensure xauth
is installed. On Debian use dpkg -l | grep xauth
. On
FreeBSD use pkg info | grep xauth
.
FreeBSD specific setup
Install xauth
with pkg install xauth
. But this didn't properly setup everything. To complete the
configuration:
touch ~/.Xauthority # xauth complaints if it's absent
Note down your hostname
from /etc/rc.conf
, add that to your /etc/hosts
:
::1 <YOUR_HOST_HERE> localhost localhost.my.domain
127.0.0.1 <YOUR_HOST_HERE> localhost localhost.my.domain
This post inspired me.
Client setup
Install xming x server on Windows. Make sure the server is :0.0
. This can be told
by hovering mouse over the X icon in taskbar.
Fire up mintty,
export DISPLAY=localhost:0.0
ssh -Y <ssh server>
The original post omitted localhost
and it didn't work for me.
In ssh session, test with xclock
.
Vim clipboard
First check vim system clipboard support:
vim --version | grep clipboard
If for clipboard
and xterm_clipboard
there's a -
in front, then you are NOT good. For Ubuntu,
the base vim package is in this case. You'll need vim GUI packages like vim-gtk
for it to work:
apt-get install vim-gtk
Now in vim remote session, select some text type "+y
. Try to paste it in local notepad and make sure it works.
X11 with su
x11
won't work after su
- the cookies for X11 forwarding is stored in the user's ~/.Xauthority
. For X11
to continue work after su
, make a symbolic link to the user which logs in remotely:
ln -s /home/<user>/.Xauthority /root/.Xauthority
X11 Clipboard with Tmux
It's hard to select and copy text from tmux if there's vertical splits - using terminals copy utility would copy across panes. On the other hand, Tmux's copy does not integrate well with xclip (I found it works only intermittently).
The best solution I have so far is to rely on tmux's copy (not to x11). Then launch xclip
. While it's waiting
to take input from stdin, paste tmux's copy buffer by pressing ctrl-b ]
. Then press ctrl-D
(EOF) to commit
xclip
.
This works for both ctrl-b [
, and tmux's support for mouse selection.