1

My script is something like this:

#!/bin/bash
sudo teamviewer --daemon enable
teamviewer
sudo teamviewer --daemon disable

It works fine on its own, and it requests my password with a terminal window. So, now I want to make a launcher for it. I have this in my .desktop file:

[Desktop Entry]
Version=1.0
Encoding=UTF-8
Type=Application
Categories=Network;
Name=TeamViewer 12
Comment=Remote control and meeting solution.
Exec=gksu /path/here/Start.sh
Icon=teamviewer

That way it requests my password with a pop-up instead of a terminal window, but doing this runs the whole script with sudo, which is a problem, because TeamViewer refuses to start with sudo. How can I solve this?

Zanna
  • 70,465

1 Answers1

3

You can change to script to:

#!/bin/bash

teamviewer --daemon enable

sudo -u USERNAME teamviewer

teamviewer --daemon disable

This way you can execute teamviewer as USERNAME while still running the others as the sudo user, as you are running the script with gksu.


If you want an easy way to test it, run the following with gksu:

#!/bin/bash

echo "Without 'sudo' -->" ; whoami

echo "With 'sudo -u USERNAME' -->" ; sudo -u USERNAME whoami
M. Becerra
  • 3,448
  • 1
    Yep. But you don't need the 1st and last sudo if the script is already being run via gksu (and it will have to be in order for the password to be requested once graphically). – terdon Jun 07 '17 at 19:09
  • That didn't work, not sure why

    "Error: Could not create /root/.local/share/teamviewer12/logfiles"

    "mkdir: cannot create directory '/root': Permission Denied"

    "Error: InitDirs failed. (2)"

    Accidentally pressed enter before finishing the reply

    – POPCORNS Jun 07 '17 at 19:15
  • Alright, it worked once I replaced my username with root. Thanks for helping! – POPCORNS Jun 07 '17 at 19:21
  • 2
    @UltimatePOPCORNS But sudo -u root teamviewer is the same as sudo teamviewer, which you claimed does not work (and which sounds like a horrible security problem to me). – Byte Commander Jun 07 '17 at 19:25
  • Unfortunately I can't get it to launch with my own username, as it writes to the /root directory for some reason. – POPCORNS Jun 07 '17 at 21:36
  • @UltimatePOPCORNS you need sudo -E so that your environment variables are kept. Which of the sudo calls need that will depend on which of the commands is trying to write to /root. – terdon Jun 07 '17 at 22:29