簡體   English   中英

當我通過 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?

我使用的是 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 隧道:向ssh添加-Y標志: 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM