关于linux:错误:使用Xclip复制ssh公钥时无法打开显示:(空)

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

我关注"生成SSH密钥",它说

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

但是运行xclip -sel clip < ~/.ssh/id_rsa.pub之后,我得到Error: Can't open display: (null)
问题是什么? 我四处搜寻,但一无所获


DISPLAY=:0 xclip -sel clip < ~/.ssh/id_rsa.pub不适用于我(ubuntu 14.04),但是您可以使用:

1
cat ~/.ssh/id_rsa.pub

获取您的公钥


基于此问题的日期,原始发布者将不会使用Windows Subsystem for Linux。但是,如果您遇到相同的错误,则可以使用以下替代方法:

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

感谢此页面指出Windows的clip.exe(并且您必须键入" .exe")可以从bash shell运行。


这是一个很好的答案,不能在此处发布。来自askubuntu的一位用户Gilles:

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


如果您想在远程主机上使用xclip,只需在-ssh命令中添加-X

1
ssh user@host -X

可以在这里找到更多详细信息:https://askubuntu.com/a/305681


试试这个,它会像魅力一样工作。我遇到了同样的错误,但是这种方法对我有用:

1
ssh USER@REMOTE"cat file"|xclip -i

阅读了您链接的文档。太傻了! xclip只是一个剪贴板。您会发现其他复制粘贴键的方法...(我确定)

如果您不是从图形X会话内部进行工作,则需要将$DISPLAY环境var传递给命令。像这样运行它:

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

当然:0取决于您使用的显示器。如果您有一台典型的台式机,则可能是:0


以下内容也对我有用:

1
ssh <user>@<host> "cat <filepath>"|pbcopy


由用户root添加此命令:
ssh user_to_acces @ hostName -X

user_to_acces =用户
hostName =主机名机器