196

I’m following in Generating SSH Keys, it says

sudo apt-get install xclip

Downloads and installs xclip. If you don't have apt-get, you might need to use another installer (like yum)

xclip -sel clip < ~/.ssh/id_rsa.pub

Copies the contents of the id_rsa.pub file to your clipboard

But after I runxclip -sel clip < ~/.ssh/id_rsa.pub I get Error: Can't open display: (null) What is the problem? I googled around but found nothing about it

miken32
  • 42,008
  • 16
  • 111
  • 154
ZK Zhao
  • 19,885
  • 47
  • 132
  • 206

8 Answers8

166

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

Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
Jahdere
  • 2,375
  • 1
  • 13
  • 8
  • 12
    This should be the number 1 answer for anyone who is trying to copy file contents via ssh – dval Oct 01 '14 at 14:03
  • 43
    Except when you're trying to copy a long file. – Flavian Hautbois Dec 16 '14 at 16:29
  • 35
    @dval, I don't agree it shall be number 1 answer, because the question mentions `xclip` usage not just displaying SSH key in a bash with `cat ~/.ssh/id_rsa.pub`. However, this helps to solve the problem, because you can copy the value when it's displayed. – Nik Sumeiko Jan 22 '15 at 17:13
  • 177
    what?! how this can solve the problem of xclip not functioning? – VaTo Jun 18 '15 at 17:31
  • 51
    Actually, this answer is extremely misleading. – Hasan Can Saral Jan 05 '17 at 07:57
  • 1
    Answer -1. I found that this helped: https://madebynathan.com/2011/10/04/a-nicer-way-to-use-xclip/ It's a function and handy aliases for your .bash_profile `# Copy contents of a file` `function cbf() { cat "$1" | cb; }` `# Copy SSH public key` `alias cbssh="cbf ~/.ssh/id_rsa.pub"` `# Copy current working directory` `alias cbwd="pwd | cb"` `# Copy most recent command in bash history` `alias cbhs="cat $HISTFILE | tail -n 1 | cb"` – flith Oct 30 '17 at 08:28
  • 1
    This answer is changing the question. – msoutopico Jan 10 '20 at 20:26
  • This answer does NOT help when, for example, copying a pub key (it'll multiline copy it) – dylanh724 Feb 27 '21 at 14:14
  • This solves the specific problem by working around the actual question, which is about xclip. It could be a comment, or part of the answer, but not the whole answer. I don't know why it was accepted. – alezvic Aug 07 '23 at 15:22
133

Based on the date of this question the original poster wouldn't have been using Windows Subsystem for Linux. But if you are, and you get the same error, the following alternative works:

clip.exe < ~/.ssh/id_rsa.pub

Thanks to this page for pointing out Windows' clip.exe (and you have to type the ".exe") can be run from the bash shell.

Marc Stober
  • 10,237
  • 3
  • 26
  • 31
  • 14
    you can also use pipes `cat ~/.ssh/id_rsa.pub | clip.ese` just works – chriz Aug 07 '17 at 13:01
  • 5
    @chriz I think you mean "exe" not "ese"? – Marc Stober Aug 16 '17 at 00:39
  • 5
    oh yeah, sorry that was just a typo, it's `cat ~/.ssh/id_rsa.pub | clip.exe`. Thanks for pointing that out. – chriz Aug 22 '17 at 15:22
  • Just what I was looking for. Thanks! – Xunnamius Jan 29 '18 at 20:07
  • Super! I was having this issue on Linux Subsystem under Windows. It works fine now. – Tarik Oct 16 '18 at 19:56
  • how do you paste it?!? – aderchox Nov 01 '18 at 12:42
  • 1
    @Moytaba CONTROL SHIFT V. – JoanComasFdz Mar 29 '19 at 10:26
  • 1
    You can also add an alias clip=clip.exe. – JoanComasFdz Mar 29 '19 at 10:27
  • Using Ubuntu over here installed from the Windows Store on a Windows 10 PC. This answer helped (ie using clip.exe instead of simple clip) – Jérôme Oudoul Feb 25 '20 at 15:47
  • 2
    and for those who preferred a single command on all platforms: add this to your `~/.bashrc` or `~/.bash_profile` : `alias pbcopy="clip.exe"`. and thanks to @chriz for pointing out pipe `|` works just fine. – Dio Phung Mar 18 '20 at 05:02
  • The documentation found at `CLIP.EXE /?` states that it is only for copying. How do you paste so you can pipe the contents of the clipboard into other commands? I'd like to be able to casually juggle large, transient chunks of text around in one-liners. For example, something close to `xclip -o | xmllint --format - | vim -` (if that’s right) with an arbitrary website’s source. – atimholt Mar 26 '20 at 17:50
68

This was too good of an answer not to post it here. It's from Gilles, an askubuntu fellow:

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.

By Gilles from askubuntu

Neithan Max
  • 11,004
  • 5
  • 40
  • 58
36

In case you are trying to use xclip on remote host just add -X to your ssh command

ssh user@host -X

More detailed information can be found here : https://askubuntu.com/a/305681

Community
  • 1
  • 1
18

The following is also working for me:

ssh <user>@<host>  "cat <filepath>"|pbcopy 
Dario
  • 6,152
  • 9
  • 39
  • 50
11

Try this and it will work like a charm. I was having the same error but this approach did the trick for me:

ssh USER@REMOTE "cat file"|xclip -i
VaTo
  • 2,936
  • 7
  • 38
  • 77
10

Have read the documentation you've linked. That's totally silly! xclip is just a clipboard. You'll find other ways to copy paste the key... (I'm sure)


If you aren't working from inside a graphical X session you need to pass the $DISPLAY environment var to the command. Run it like this:

DISPLAY=:0 xclip -sel clip < ~/.ssh/id_rsa.pub

Of course :0 depends on the display you are using. If you have a typical desktop machine it is likely that it is :0

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 18
    I get `Error: Can't open display: :0`. By the way, I'm accessing a VPS through Git Bash. VPS is ubuntu, local machine is windows7 – ZK Zhao Sep 09 '13 at 10:19
  • I think so. But I'm not familiar with ubuntu. Could you give me some advice? – ZK Zhao Sep 09 '13 at 10:29
  • Lol, I'm not payed at all. I use cat intead, but need to fomrat it manually – ZK Zhao Sep 09 '13 at 10:45
  • 1
    I have same issue with @cqcn1991 – Nam G VU Jul 21 '18 at 13:30
  • Suggestions: (1) Use the right mouse buttong to copy / paste it or (2) save to a file and scp that file to your local machine – hek2mgl Feb 06 '19 at 21:04
  • @cqcn1991 Did you ever figure out how to use xclip or xsel when accessing a remote server via Git Bash on Windows? I'm getting `Error: Can't open display: (null)`. Thanks! – Ryan Feb 07 '20 at 16:07
  • @cqcn1991 I've asked a question here: https://stackoverflow.com/q/60117294/470749 – Ryan Feb 07 '20 at 16:18
  • 1
    Using `DISPLAY=:0 xclip` also works to clear the clipboard from a job. In my case, I'm using `at` to clear the clipboard two minutes after copying. – Michael Allan Jackson Aug 04 '20 at 06:42
1

add by user root this command : ssh user_to_acces@hostName -X

user_to_acces = user hostName = hostname machine

Fadid
  • 1,250
  • 15
  • 16