Error: Can't Open Display: (Null) When Using Xclip to Copy Ssh Public Key

Error: Can't open display: (null) when using Xclip to copy ssh public key

DISPLAY=:0 xclip -sel clip < ~/.ssh/id_rsa.pub didn't work for me (ubuntu 14.04), but you can use :

cat ~/.ssh/id_rsa.pub

to get your public key

How can I copy the contents of a file directly into my Windows clipboard when I'm running the command on a remote Linux machine via ssh in Git Bash?

You need and X server on your Windows host and X-tunnelling in your ssh connection. xclip will send the clipboard to your X server, and the server will provide it to Windows.

  1. Install an X server to your Windows machine. I use VcXsrv, there are XMing and others. The flavor of X is not important.
  2. Launch the server
  3. in Git Bash use command export DISPLAY=localhost:0.0
  4. Make sure that /etc/ssh/sshd.config on the remote node has line X11Forwarding yes
  5. enable X11 tunneling in ssh command: add -Y flag to ssh: ssh -Y <server_address>

While there are some recipies on Stack Overflow already, there is one glitch. Note DISPLAY=localhost:0.0. If you omit localhost, that is export DISPLAY=:0.0, then xclip will fail on the remote node :

connect /tmp/.X11-unix/X0: No such file or directory
xterm: Xt error: Can't open display: localhost:10.0

Trying to to run xdotool but getting 'Can't open display: (null)'

According to official notice by apple

X11 is no longer included with Mac, but X11 server and client libraries are available from the XQuartz project.

why X11 matters in this case ?

xdotool - command-line X11 automation tool.

So Alongside setting export DISPLAY=:0

install xquartz.

What does DISPLAY means exactly?

according x manual

From the user's perspective, every X server has a display name of the form:

               hostname:displaynumber.screennumber

This information is used by the application to determine how it should
connect to the server and which screen it should use by default (on
displays with multiple monitors):

  1. hostname
    The hostname specifies the name of the machine to which the display is physically connected. If the hostname is not given, the
    most efficient way of communicating to a server on the same machine
    will be used.

  2. displaynumber
    The phrase "display" is usually used to refer to collection of monitors that share a common keyboard and pointer (mouse, tablet,
    etc.). Most workstations tend to only have one keyboard, and
    therefore, only one display. Larger, multi-user systems, however,
    frequently have several displays so that more than one person can be
    doing graphics work at once. To avoid confusion, each display on a
    machine is assigned a display number (beginning at 0) when the X
    server for that display is started. The display number must always be
    given in a display name.

  3. screennumber
    Some displays share a single keyboard and pointer among two or more monitors. Since each monitor has its own set of windows, each
    screen is assigned a screen number (beginning at 0) when the X server
    for that display is started. If the screen number is not given, screen
    0 will be used.

there is simpler description found here

A display consists (simplified) of:

  • a keyboard
  • a mouse
  • a screen

i.e. when you connect over ssh you are using different sets of these 3.

Ubuntu Clipboard copy, SSHK

If you are running the X Window System, you can use the xclip utility.

To install: sudo apt-get install xclip

To copy: xclip id_rsa.pub

To copy to clipboard: xclip -o | xclip -sel clip

To paste: xclip -o -sel clip > file.txt

Read the README file in the xclip source for more on how to use it.

How can I copy the output of a command directly into my clipboard?

I always wanted to do this and found a nice and easy way of doing it. I wrote down the complete procedure just in case anyone else needs it.

First install a 16 kB program called xclip:

sudo apt-get install xclip

You can then pipe the output into xclip to be copied into the clipboard:

cat file | xclip

To paste the text you just copied, you shall use:

xclip -o

To simplify life, you can set up an alias in your .bashrc file as I did:

alias "c=xclip"
alias "v=xclip -o"

To see how useful this is, imagine I want to open my current path in a new terminal window (there may be other ways of doing it like Ctrl+T on some systems, but this is just for illustration purposes):

Terminal 1:
pwd | c

Terminal 2:
cd `v`

Notice the ` ` around v. This executes v as a command first and then substitutes it in-place for cd to use.

Only copy the content to the X clipboard

cat file | xclip

If you want to paste somewhere else other than a X application, try this one:

cat file | xclip -selection clipboard

How to send data to local clipboard from a remote SSH session

I'm resurrecting this thread because I've been looking for the same kind of solution, and I've found one that works for me. It's a minor modification to a suggestion from OSX Daily.

In my case, I use Terminal on my local OSX machine to connect to a linux server via SSH. Like the OP, I wanted to be able to transfer small bits of text from terminal to my local clipboard, using only the keyboard.

The essence of the solution:

commandThatMakesOutput | ssh desktop pbcopy

When run in an ssh session to a remote computer, this command takes the output of commandThatMakesOutput (e.g. ls, pwd) and pipes the output to the clipboard of the local computer (the name or IP of "desktop"). In other words, it uses nested ssh: you're connected to the remote computer via one ssh session, you execute the command there, and the remote computer connects to your desktop via a different ssh session and puts the text to your clipboard.

It requires your desktop to be configured as an ssh server (which I leave to you and google). It's much easier if you've set up ssh keys to facilitate fast ssh usage, preferably using a per-session passphrase, or whatever your security needs require.

Other examples:

ls  | ssh desktopIpAddress pbcopy
pwd | ssh desktopIpAddress pbcopy

For convenience, I've created a bash file to shorten the text required after the pipe:

#!/bin/bash
ssh desktop pbcopy

In my case, i'm using a specially named key

I saved it with the file name cb (my mnemonic (ClipBoard). Put the script somewhere in your path, make it executable and voila:

ls | cb


Related Topics



Leave a reply



Submit