Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

My firm has some Ubuntu 12.04 running where there is no display attached and that are behind different firewalls.

The visitor on local websites on that servers should be able to start a GUI program like Teamviewer there to give graphical access to me (or other support admins).

I guess I have to give the PHP user www-data somehow sudo access to start the Teamviewer GUI in a virtual X-Session via gksu.

How would I have to configure sudoers to achieve this?

And how could I call that from within a PHP-script, that is running on that server?

Sure, I for myself could easily login via ssh or pipe the X-session to my machine, but I need such a Teamviewer-login because this is the easiest way to get past that differing firewalls on all servers.

share|improve this question
up vote 1 down vote accepted

Disclaimer: I don't know anything about the Teamviewer product, but, I'll treat this as any old x-windows app that you want to run.

If you just want a gui app to start, look into using .xinitrc or (in some desktop environments) ~/.config/autostart/

Buuut... Looks like you want to start this program interactively? That is, you want to run it on request, and it needs to run from your desktop environment.

Firstly -- I would not recommend that you actually give your www-data user any special privileges via sudoers, especially so if you're using this user for your webserver which has public facing services.

I recommend creating a user "sysadmin" or something along those lines, and give them the proper privileges.

Then have a PHP script do something very low impact, like drop a flag file in /tmp/ and have another user operate on that file.

So I'd have this PHP script:

   // Trigger for the team view launcher.

Then I'd have a cron job like:

* * * * * /home/sysadmin/.jobs/teamstarter.sh

(Caveat: This runs only once a minute. So you'll have to wait up to a minute. You could have this script run in a loop.)

And then I'd have /home/sysadmin/.jobs/teamstarter.sh

if [ -f /tmp/teamviewflag ]
    env DISPLAY=:1 XAUTHORITY=/home/sysadmin/.Xauthority /path/to/teamviewer
    rm /tmp/teamviewflag

This will get it running on demand from a PHP script. There's a few rough edges you'll probably want to sand off here:

  • This will run teamviewer every time you run the PHP script.
  • Line 4 of the bash script assumes you'll know the DISPLAY number.
  • You might also want to have a kind of "lock file" that says "hey team viewer is running" and have your scripts operate depending on if that's there or not. And have a job that follows up and cleans up the lock file when teamviewer is done.

Here's an example of the above script running in a loop:

while :
  if [ -f /tmp/teamviewflag ]
      env DISPLAY=:1 XAUTHORITY=/home/sysadmin/.Xauthority /path/to/teamviewer
      rm /tmp/teamviewflag
  # Sleep for a half second.
  usleep 500000  

You can run it "nicely" (so it has a lower scheduling priority) like:

nice -n 19 /home/sysadmin/.jobs/teamstarter.sh

And if you want to start it at boot, you could put it on a cron tab like so:

@reboot /home/sysadmin/.jobs/teamstarter.sh

share|improve this answer
How would I create this script as a loop? Wouldn't that use my cpu a lot? – rubo77 Jun 6 '13 at 9:43
I edited my response to show an example use of it running in a loop. You'll also want to background that process probably. It used 0.3% of one core on a Intel Core i7 when I tested it, and then I showed an example to nice the process so you can give other processes priority if you'd like. – dougBTV Jun 6 '13 at 13:49

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.