Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an X instance running on my server computer, and on occasion I connect to it remotely via TeamViewer. Once in a while TeamViewer crashes and I cannot restart it remotely. Is there a way to login to my running X instance through ssh, and restart TeamViewer remotely?

share|improve this question
up vote 1 down vote accepted

An X program needs two pieces of information in order to connect to an X display.

  • It needs the address of the display, which is typically :0 when you're logged in locally or :10, :11, etc. when you're logged in remotely (but the number can change depending on how many X connections are active). The address of the display is normally indicated in the DISPLAY environment variable.

  • It needs the password for the display. X display passwords are called magic cookies. Magic cookies are not specified directly: they are always stored in X authority files, which are a collection of records of the form “display :42 has cookie 123456”. The X authority file is normally indicated in the XAUTHORITY environment variable. If $XAUTHORITY is not set, programs use ~/.Xauthority.

You're trying to act on the windows that are displayed on your desktop. If you're the only person using your desktop machine, it's very likely that the display name is :0. Finding the location of the X authority file is harder, because with gdm as set up under Debian squeeze or Ubuntu 10.04, it's in a file with a randomly generated name. (You had no problem before because earlier versions of gdm used the default setting, i.e. cookies stored in ~/.Xauthority.)

If this is a one-shot, you can detect the values of DISPLAY and XAUTHORITY from a running process. This is awkward to automate. You have to figure out the PID of a process that's connected to the display you want to work on, then get the environment variables from /proc/$pid/environ (eval export $(</proc/$pid/environ tr \\0 \\n | grep -E '^(DISPLAY|XAUTHORITY)=')).

A long-term, automatic solution is to copy cookies when you log into your desktop X session. Add the following lines to ~/.profile (or some other script that is read when you log in):

    # DISPLAY is set and points to a local display, and XAUTHORITY is
    # set, so merge the contents of `$XAUTHORITY` into ~/.Xauthority.
    XAUTHORITY=~/.Xauthority xauth merge "$XAUTHORITY";;

Then you can run programs on your remote X display simply by setting DISPLAY:

ssh 'DISPLAY=:0 restart teamviewer'

This answer is adapted from a more complete answer to a similar question on Unix Stack Exchange.

share|improve this answer

Probably the easiest way is to remotely (via SSH) start a vncserver on the relevant display. After that use SSH tunneling to connect a vnc client from your remote machine through the SSH connection to the VNC server.

share|improve this answer
I actually have been trying that, but VNC is not as reliable as TeamViewer and also it is much slower. For some reason, every time I started using VNC for actual work, it ended up crashing and I ended up losing all my work. – Can Bal Oct 11 '11 at 23:36
Well, it should be enough to restart TeamViewer? – Sascha Vogt Oct 12 '11 at 8:03
I tried that as well. For some reason running TeamViewer in VNC crashes VNC right away. I don't have enough knowledge in VNC, so I thought figuring that out would be a harder task. – Can Bal Oct 16 '11 at 20:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.