当我通过 Git Bash 中的 ssh 在远程 Linux 机器上运行命令时,如何将文件的内容直接复制到我的 Windows 剪贴板中?

瑞安

我使用的是 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以及如何直接复制命令的输出到我的剪贴板?是类似的问题,但xclip会导致此错误:

xclip -sel c < /etc/php/7.4/cli/php.ini
Error: Can't open display: (null)

评论后更新

https://stackoverflow.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

也许在 Windows 上的 Git Bash 中使用 X 会话?将进一步帮助我。

红先生

您需要 Windows 主机上的 X 服务器和 ssh 连接中的 X 隧道。xclip将剪贴板发送到您的 X 服务器,服务器将把它提供给 Windows。

  1. 在您的 Windows 机器上安装 X 服务器。我用的是 VcXsrv,还有 XMing 等。X 的味道并不重要。
  2. 启动服务器
  3. 在 Git Bash 中使用命令 export DISPLAY=localhost:0.0
  4. 确保/etc/ssh/sshd.config远程节点上有线路X11Forwarding yes
  5. 在 ssh 命令中启用 X11 隧道:将-Y标志添加sshssh -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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章