xclip can't talk to your X server. Check that $DISPLAY is set correctly. Do other X clients work with the same $DISPLAY setting?
$ echo 'hello' | xclip
works just fine for me.
You get the error after
su'ing to another user because that user doesn't have appropriate permissions to connect to your X server. X uses a permission checking protocol known as MIT magic cookie, which works by creating a random string to use as a password. That password is stored in the file
~/.Xauthority if the environment variable is not set. It is manipulated by the
xauth tool. For example,
$ xauth list
Feynman/unix:0 MIT-MAGIC-COOKIE-1 <<redacted>>
I'm not sure what you're doing the su for, but:
- if it's so you can read the file, then just run the cat as the other user, and pipe the output of su to xclip. This way, xclip runs as your user, and works.
- if you're trying to set the clipboard for another user's X session, then you'll need to set
$DISPLAY to that user's display, and
$XAUTHORITY to that user's X authority file. Look into ConsoleKit (if you're using it) and
/var/lib/gdm (if you're using gdm) to find said things.
If none of the above, then you'll have to use xauth to set up the authorization, and keep in mind that anything connecting to your X server can do all sorts of evil, such as watch all your keystrokes.
$DISPLAY refers to which X server they're logged in to. Unless you've set up multiple servers running (so multiple users may log in at once), it will be the same for all users.