9

I want to copy a long file into clipboard with xsel,in my local pc ,just input:

cat /usr/bin/mysql_secure_installation|xsel -b

The file mysql_secure_installation located in my local os was copied into clipboard.

Now login to my vps with ssh command,then input

DISPLAY=:0 cat /usr/bin/mysql_secure_installation|xsel -b

I come across the error info:

xsel: Can't open display: (null)
: Inappropriate ioctl for device

I know that the proper way to get remote file is scp command:

scp -P port username@tohostname:/remotefile /newlocalfile

I just wonder how to get file content copied into clipboard after loginning my vps,copy file from remote machine to the clipboard of my local computer.

Same error:

DISPLAY=:0 xsel -b <  /usr/bin/mysql_secure_installation
xsel: Can't open display: (null)
: Connection refused
showkey
  • 323
  • 1
    So you try to copy file from remote machine to the clipboard of your local computer, right? – Romeo Ninov Feb 06 '20 at 10:15
  • 1
    try running it as DISPLAY=:0 xsel -b <foo or ... | DISPLAY=:0 xsel -b instead of DISPLAY=:0 cat ... | xsel -b which will only set the DISPLAY envvar for the cat process. –  Feb 06 '20 at 11:39
  • I am getting same issue on remote server (CentOS 7) when working in screen. – Gagan Aug 01 '22 at 10:14

3 Answers3

6

Start your ssh connection with

ssh -X yourserver

then don't change the DISPLAY variable yourself. ssh should set it up automatically, so that xsel (and any other X11 clients) can use the display on your local computer -- including its clipboard. This is called X11 forwarding.

Manually setting DISPLAY=:0 in your ssh session will tell xsel to use the display on that remote server -- if one is even running.

JigglyNaga
  • 7,886
  • 1
    I couldn't get this working. xsel: Can't open display: (null) : Inappropriate ioctl for device – Ryan Feb 07 '20 at 16:01
  • 1
    @Ryan What is the value of DISPLAY when you do this? If you enable debugging messages with ssh -v, does it including anything about X11 forwarding? – JigglyNaga Feb 07 '20 at 16:24
  • Interesting. I see debug1: No xauth program. Warning: untrusted X11 forwarding setup failed: xauth key data not generated – Ryan Feb 07 '20 at 17:54
  • Maybe you are talking about: https://stackoverflow.com/a/39556508/470749 (I will look at my client and server configs.) I'd also asked a question here: https://stackoverflow.com/q/60117294/470749 – Ryan Feb 07 '20 at 17:56
  • Note: if you're running tmux, make sure you restart your tmux session after reconnecting to the remote host with ssh -X, otherwise this won't work. – Lane Rettig Jan 14 '24 at 21:16
1

As Lord Giles said here:

  • Client side you need to connect with ssh -X (per session, ~/.ssh/config to use it always)
  • Server side you need to have X11Forwarding yes in /etc/ssh/sshd_config (note the d). Also, some packages like xauth and libxdmcp6, installed by default. Remember to sudo systemctl restart sshd.service to changes to take effect.

For security reasons, I wouldn't enable it to use it always, and in general only use it on very trusted servers.

Other sources

Pablo A
  • 2,712
1

The other answers have shown how to forward your X session to achieve the O (outcome) desired. However, this is not very secure (as has been pointed out) or performant, and often needs additional setup.

The best way to achieve the same E (effect) in the SCROE model is to transfer the raw data bytestream to the host and locally shove it into the clipboard. You already almost had it. You had a pipe, and you had scp, but scp is only almost right.

ssh -p port username@tohostname cat /usr/bin/mysql_secure_installation | xsel -ib

The pipe ensures the stdout of the cat command on the remote host ends up as stdin of the xsel command on the local host. (I also added a -i; xsel is used safest when the mode of operation is explicitly specified.)

mirabilos
  • 1,733