203

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

2
  • 2
    What does echo $DISPLAY say?
    – Fred Foo
    Sep 9, 2013 at 10:13
  • 3
    nothing. I'm running this command on a server via ssh May 8, 2019 at 12:39

8 Answers 8

170

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

10
  • 12
    This should be the number 1 answer for anyone who is trying to copy file contents via ssh
    – dval
    Oct 1, 2014 at 14:03
  • 45
    Except when you're trying to copy a long file. Dec 16, 2014 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. Jan 22, 2015 at 17:13
  • 182
    what?! how this can solve the problem of xclip not functioning?
    – VaTo
    Jun 18, 2015 at 17:31
  • 51
    Actually, this answer is extremely misleading. Jan 5, 2017 at 7:57
138

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.

11
  • 15
    you can also use pipes cat ~/.ssh/id_rsa.pub | clip.ese just works
    – chriz
    Aug 7, 2017 at 13:01
  • 5
    @chriz I think you mean "exe" not "ese"? Aug 16, 2017 at 0:39
  • 6
    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, 2017 at 15:22
  • 1
    @Moytaba CONTROL SHIFT V. Mar 29, 2019 at 10:26
  • 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, 2020 at 5:02
71

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

1
  • 1
    This isn't proper etiquette, but sheesh that is a phenomenal answer 🔥
    – cisco
    Feb 16, 2022 at 8:00
38

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

1
  • For some reason I still had issue like X Error of failed request: BadAccess (attempt to access private resource denied), until I found this and this ssh user@host -Y worked for me. Make sure ForwardX11 yes in local and X11Forwarding yes in server.
    – Ragavendra
    Mar 29 at 0:05
20

The following is also working for me:

ssh <user>@<host>  "cat <filepath>"|pbcopy 
3
  • works also using a pem file: ssh -i /path/myapp.pem <user>@<host> "cat <filepath>" | pbcopy
    – alemol
    Apr 14, 2016 at 20:10
  • 13
    I thought pbcopy was a mac thing, not linux Dec 8, 2016 at 23:47
  • 1
    That's a mac specific Jul 3, 2022 at 9:30
13

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
2
  • Very good idea! It does work indeed, even if $DISPLAY is not set Feb 19, 2023 at 10:37
  • If you're on a Mac, you can replace xclip -i with pbcopy Jun 7, 2023 at 21:09
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

8
  • 19
    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 9, 2013 at 10:19
  • I think so. But I'm not familiar with ubuntu. Could you give me some advice?
    – ZK Zhao
    Sep 9, 2013 at 10:29
  • Lol, I'm not payed at all. I use cat intead, but need to fomrat it manually
    – ZK Zhao
    Sep 9, 2013 at 10:45
  • 1
    I have same issue with @cqcn1991
    – Nam G VU
    Jul 21, 2018 at 13:30
  • 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. Aug 4, 2020 at 6:42
1

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

user_to_acces = user hostName = hostname machine

Not the answer you're looking for? Browse other questions tagged or ask your own question.