38

I've been using xclip to copy the output of bash commands like so:

pwd | tr -d "\n" | xclip -selection c

So that I can then paste the output into another terminal session.

I was trying to use it on my headless server (ubuntu 13.04), which is started without x. It doesn't work with the error:

Error: Can't open display: (null)

Is there a way around this. I realise that seeing as the program is called xclip it may not be possible.

Anake
  • 1,905

2 Answers2

44

The clipboard is provided by the X server. It doesn't matter whether the server is headless or not, what matters is that your local graphical session is available to programs running on the remote machine. Thanks to X's network-transparent design, this is possible.

I assume that you're connecting to the remote server with SSH from a machine running Linux. Make sure that X11 forwarding is enabled both in the client configuration and in the server configuration. In the client configuration, you need to have the line ForwardX11 yes in ~/.ssh/config to have it on by default, or pass the option -X to the ssh command just for that session. In the server configuration, you need to have the line X11Forwarding yes in /etc/ssh/sshd_config (it is present by default on Ubuntu).

To check whether X11 forwarding is enabled, look at the value of the DISPLAY environment variable: echo $DISPLAY. You should see a value like localhost:10 (applications running on the remote machine are told to connect to a display running on the same machine, but that display connection is in fact forwarded by SSH to your client-side display). Note that if DISPLAY isn't set, it's no use setting it manually: the environment variable is always set correctly if the forwarding is in place. If you need to diagnose SSH connection issues, pass the option -vvv to ssh to get a detailed trace of what's happening.

If you're connecting through some other means, you may or may not be able to achieve X11 forwarding. If your client is running Windows, PuTTY supports X11 forwarding; you'll have to run an X server on the Windows machine such as Xming.

  • +1 brilliant answer, but as I mentioned this server is running without x so Nykakins answer is more relevant. – Anake Jun 09 '13 at 08:56
  • @Anake I'm afraid you misread my answer. A remote display doesn't involve any X server running on the remote machine. You do need to have the xclip program installed and the supporting libraries, but that doesn't involve installing an X server. – Gilles 'SO- stop being evil' Jun 09 '13 at 17:23
  • 2
    Just wondering ... is this approach valid when connecting from an OSX client to a headless Ubuntu server? – ken Nov 18 '13 at 09:20
  • @ken Yes, it's valid when connecting from any machine running an X server to any remote machine where you run an X application. Remember that for X, the server is on the local machine and the client is the application; for the remote protocol, usually the local machine is the client and the remote machine is the server. – Gilles 'SO- stop being evil' Nov 18 '13 at 10:15
  • 6
    This doesn't work for me. I'm going from mac -> linux server. X11Forwarding yes is in the sshd_config on the server and I passed -X when I ssh'd to the server. Error: Can't open display: (null) on cat ~/.ssh/id_rsa.pub | xclip -selection clipboard – chovy Dec 11 '16 at 20:27
  • @chovy You must have an X server running on the client, and you must have the DISPLAY environment variable set in the SSH client process. SSH can't forward a connection that it doesn't see. – Gilles 'SO- stop being evil' Dec 11 '16 at 21:22
  • 1
    ok i figured it would work since pbcopy already works on my mac locally. How do I start an x server on a mac and what do I set $DISPLAY to? – chovy Dec 11 '16 at 23:40
  • 1
    @chovy you have to install XQuartz on macOS to enable X Server. (don't forget to log out and log into your mac after installing it). – elquimista Oct 18 '18 at 09:19
7

Bash itself does not have a clipboard. It's a X feature. xclip is just a command line interface and as you see, it requires $DISPLAY. A way around is to just use a temporary file (for eg created with tempfile):

$ echo 123 > /tmp/fileDy7Dgv
$ tr -d '2' < /tmp/fileDy7Dgv
13
Pablo Bianchi
  • 15,657
Nykakin
  • 3,744