Error: Can't open display: (null) when using Xclip to copy ssh public key [closed]
Asked Answered
B

8

201

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

Baudoin answered 9/9, 2013 at 10:10 Comment(2)
What does echo $DISPLAY say?Cantabrigian
nothing. I'm running this command on a server via sshRenin
I
169

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

Indication answered 2/6, 2014 at 15:56 Comment(10)
This should be the number 1 answer for anyone who is trying to copy file contents via sshPeg
Except when you're trying to copy a long file.Kathikathiawar
@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.Vaginitis
what?! how this can solve the problem of xclip not functioning?Liquidate
Actually, this answer is extremely misleading.Gath
Answer -1. I found that this helped: 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"Frum
This answer is changing the question.Robtrobust
This answer does NOT help when, for example, copying a pub key (it'll multiline copy it)Reinhold
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.Demott
Absolutely a bad answer. this is copy how to copy terminal content without using a mouse & keyboard as soon as the command is sentUltrasound
R
137

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.

Roturier answered 4/7, 2017 at 20:53 Comment(11)
you can also use pipes cat ~/.ssh/id_rsa.pub | clip.ese just worksKarlkarla
@Karlkarla I think you mean "exe" not "ese"?Roturier
oh yeah, sorry that was just a typo, it's cat ~/.ssh/id_rsa.pub | clip.exe. Thanks for pointing that out.Karlkarla
Just what I was looking for. Thanks!Flagstaff
Super! I was having this issue on Linux Subsystem under Windows. It works fine now.Temikatemp
how do you paste it?!?Hey
@Moytaba CONTROL SHIFT V.Recency
You can also add an alias clip=clip.exe.Recency
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)Wean
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 @Karlkarla for pointing out pipe | works just fine.Lochner
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.Anthropomorphic
T
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

Thamos answered 18/9, 2016 at 10:13 Comment(1)
This isn't proper etiquette, but sheesh that is a phenomenal answer 🔥Uranium
G
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

Gravely answered 13/4, 2015 at 8:35 Comment(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.Sect
L
20

The following is also working for me:

ssh <user>@<host>  "cat <filepath>"|pbcopy 
Luminesce answered 15/2, 2016 at 9:45 Comment(3)
works also using a pem file: ssh -i /path/myapp.pem <user>@<host> "cat <filepath>" | pbcopyAppropriate
I thought pbcopy was a mac thing, not linuxCorporative
That's a mac specificCompetitor
L
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
Liquidate answered 18/6, 2015 at 18:1 Comment(2)
Very good idea! It does work indeed, even if $DISPLAY is not setSarver
If you're on a Mac, you can replace xclip -i with pbcopyLineolate
R
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

Region answered 9/9, 2013 at 10:15 Comment(8)
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 windows7Baudoin
I think so. But I'm not familiar with ubuntu. Could you give me some advice?Baudoin
Lol, I'm not payed at all. I use cat intead, but need to fomrat it manuallyBaudoin
I have same issue with @cqcn1991Apterygial
Suggestions: (1) Use the right mouse buttong to copy / paste it or (2) save to a file and scp that file to your local machineRegion
@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!Cryptonym
@cqcn1991 I've asked a question here: https://mcmap.net/q/22426/-how-can-i-copy-the-contents-of-a-file-directly-into-my-windows-clipboard-when-i-39-m-running-the-command-on-a-remote-linux-machine-via-ssh-in-git-bash/470749Cryptonym
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.Depict
M
1

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

user_to_acces = user hostName = hostname machine

Microsome answered 12/2, 2019 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.