6

I am writing a build script. This script has to copy a file in the end. This file will be placed in another users home directory and it is also necessary that the file belongs to this second user. My first attempt was to use su like this:

su fbgs -c "cp fbgs.jar /home/fbgs/server/plugins/"

This works in the command line but when I put this in the script I need the password to be entered automatically. So I tried the following command:

echo Password | su -S fbgs -c "cp fbgs.jar /home/fbgs/server/plugins/"

But then I get this output:

su: must be run from a terminal

How do I get this to work?

  • Have you considered copying the file as root, and then chowning it afterwards? that way, there is no need to assume the identity of the target user. – steeldriver Oct 12 '14 at 21:36
  • No sudo, no root. The danger of abuse is simply to high (everybody can write build scripts) – BrainStone Oct 12 '14 at 21:40
  • I don't understand, a properly configured sudo does not allow everybody to ruin the system. Please describe your situation further. – mdpc Oct 12 '14 at 21:47
  • I didn't really know sudo's full potential. Now I know even more. – BrainStone Oct 12 '14 at 21:54
7

Consider setting up sudo for this particular process and perhaps set the specific command NOPASSWD: so that you do not have to echo a cleartext password (very bad in any event). Also, there are sudo flags that can allow you to either require or not require a TTY.

Example.

/etc/sudoers.d/mycmd:

theuser ALL=(fgbs) NOPASSWD: cp fbgs.jar /home/fbgs/server/plugins

my-build-script.sh:

#!/bin/sh
/usr/bin/sudo -H -u fbgs cp fbgs.jar /home/fbgs/server/plugins
  • No sudo. This is simply to dangerous. Since everybody on my system then could use sudo. – BrainStone Oct 12 '14 at 21:44
  • 3
    You can restrict the command via standard UNIX permissioning. Check out sudo, you can restrict which commands go to which users with proper configuration. – mdpc Oct 12 '14 at 21:46
  • I didn't know that you could do this for specific commands. Thank you. – BrainStone Oct 12 '14 at 21:51
  • @BrainStone Putting the password in your script would be more dangerous! – Gilles 'SO- stop being evil' Oct 12 '14 at 22:44
  • @Gilles Agreed! Like i said I didn't know you could have such a specific setting in sudo. To be quite honest I forgot that you could log in to other users than root. – BrainStone Oct 12 '14 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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