当我通过 Git Bash 中的 ssh 在远程 Linux 机器上运行命令时,如何将文件的内容直接复制到我的 Windows 剪贴板中?
Posted
技术标签:
【中文标题】当我通过 Git Bash 中的 ssh 在远程 Linux 机器上运行命令时,如何将文件的内容直接复制到我的 Windows 剪贴板中?【英文标题】: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? 【发布时间】:2020-05-23 19:36:47 【问题描述】:我使用的是 Windows 10。我打开 Git Bash,然后 ssh 进入 Ubuntu 服务器。我经常想复制一个大文本文件的全部内容。
有时我宁愿快速将内容复制到剪贴板,而不是使用 scp 将文件下载到我的 Windows 机器。
使用cat
,然后滚动数千行,然后手动复制到剪贴板是可能的,但不实用。
我宁愿通过管道将cat
传递给将输出复制到我的Windows 剪贴板的命令。或者调用其他一些命令,例如xclip
。
https://unix.stackexchange.com/questions/211817/copy-the-contents-of-a-file-into-the-clipboard-without-displaying-its-contents 和How can I copy the output of a command directly into my clipboard? 是类似的问题,但xclip
会导致此错误:
xclip -sel c < /etc/php/7.4/cli/php.ini
Error: Can't open display: (null)
comment之后更新:
https://***.com/a/39556508/470749 很有趣,但X11Forwarding yes
已经在我的服务器配置中,当我将ForwardX11 yes
附加到~/.ssh/config
然后运行ssh -v -X -t -i ~/.ssh/id_rsa myuser@■■.■■■.■■■.■■
时,我仍然得到:
debug1: No xauth program.
Warning: untrusted X11 forwarding setup failed: xauth key data not generated
然后当我跑xsel -b < /etc/php/7.4/cli/php.ini
:
xsel: Can't open display: (null)
: Inappropriate ioctl for device
也许X session in Git Bash on Windows? 会进一步帮助我。
【问题讨论】:
这有帮助吗Error: Can't open display: (null) when using Xclip to copy ssh public key 【参考方案1】:您需要 Windows 主机上的 X 服务器和 ssh 连接中的 X 隧道。 xclip
会将剪贴板发送到您的 X 服务器,服务器会将其提供给 Windows。
-
在您的 Windows 机器上安装 X 服务器。我用的是 VcXsrv,还有 XMing 等。 X 的味道并不重要。
启动服务器
在 Git Bash 中使用命令
export DISPLAY=localhost:0.0
确保远程节点上的/etc/ssh/sshd.config
有X11Forwarding yes
行
在 ssh 命令中启用 X11 隧道:将-Y
标志添加到ssh
:ssh -Y <server_address>
虽然 Stack Overflow 上已经有一些食谱,但有一个小故障。注意DISPLAY=localhost:0.0
。如果省略localhost
,即export DISPLAY=:0.0
,那么xclip
将在远程节点上失败:
connect /tmp/.X11-unix/X0: No such file or directory
xterm: Xt error: Can't open display: localhost:10.0
【讨论】:
感谢您的这些步骤。我还没有尝试过,但它们听起来可能是正确的,所以我接受了答案。 他们确实有效。我在我的 Windows 10 - Ubuntu 18.04 对上进行了测试。在剪贴板上复制了 1 Mb 日志文件。以上是关于当我通过 Git Bash 中的 ssh 在远程 Linux 机器上运行命令时,如何将文件的内容直接复制到我的 Windows 剪贴板中?的主要内容,如果未能解决你的问题,请参考以下文章
通过 ssh 连接到远程并使用 expect 运行 git 命令
通过 ssh 会话执行 bash 别名 | Linux 中国
Bash 脚本使用 ssh 和日期在远程系统上设置坐标世界时