using X as root
I have this problem with using X when I'm running linux as root. I normally login as a normal user then use 'su' to change to the root user. Once I try to use X, I get the following error.
Xlib: connection to ":0.0" refused by the server
Xlib: Cannot connect X to Server
Check the DISPLAY environment or use `-d'.
Also use the `xhost' program to verify that it is set to permit connections from your machine.
How do you mean? You're logged in to X as a normal user, then su in a shell, and try to run an X program, is that right?
In that case, it's a rather strange error. It would be far more understandable if it said something like no protocol specified, which would mean that you had no xauth for root.
I've never seen this with local connections before. I would strace it.
Yes, I log in as a normal user, change to root and when I try to open something like emacs, the error message appears. It's funny you say that you've never had this problem since I have been having this problem in Redhat and Debian. The error message is exactly the same in both distros. As for strace, do I just run it right after this error message appears?
No, if you want to strace emacs, for example, you just run "strace emacs". strace gives you a list of all syscalls used by emacs, so I'd check for the one where Xlib connect()s to the X server.
After running 'strace emacs', the only lines that deal with Xlib states that the client (in this case, the root user) is not allowed to connect to ":0.0" This error starts from the read() along with two write() calls.
Try setting your XAUTHORITY env var to the path of the user's .Xauthority file.
Thanks man, that took care of the problem. Just out of curiosity, would you mind explaining the theory behind this?
I really hate posting things time after time but I've got no choice. BTW, I copied my normal user's .Xauthority over to root. Seems like that works too.
O.K, so using X as root has been taken care of. Now I can launch anything that I want without seeing that nasty message. However, I've encountered another weird problem. If I use 'emacs -nw <file>', the rest of the emacs session (as long as konsole stays open) now will open with the '-nw' option even if I don't specify it.
The .Xauthority file is simply used as a means of keeping intruders from your X server.
Normally, the X server starts as an "open" server, in that anyone can connect to it. The servers started under gdm (or xdm, for that part), are told to use a authorization file (which you can create and manage yourself with the xauth command), so that only programs which can prove to also have access to the information in that file can use the X server. Pretty practical if you're connected to the internet so that not just anyone can open windows on your display.
I don't know exactly how the X authorization scheme works, but the .Xauthority file probably holds some cryptocraphic info (I've seen common references to MIT-MAGIC-COOKIE, but I have no insight currently as to what it means) that the X server can use to challenge the client's Xlib.
The strange thing is that usually, there is a pam_xauth module that automatically copies the relevant cookies from your .Xauthority when you use su. Maybe you just don't have it installed.
To allow anyone to connect to your X server, see the "xhost" command.