4

I run an Ubuntu server, I have two gameservers on this which need semi-regular updates.

They are run under a gameserver-specific account named 'gs', in screens.

I have a 'runupdate.sh' script which contains the following line:

/var/gs/steamcmd/one.sh;/var/gs/steamcmd/two.sh; (one line because windows CR/LF was throwing errors)

one.sh/two.sh are very similar scripts, the contents of one.sh are:

screen -r tf2_1 -X quit;
/var/gs/steamcmd/steamcmd.sh +runscript /var/gs/steamcmd/update_tf2_1.txt;
screen -d -m -S tf2_1 /var/gs/tf2/1/srcds_run -game tf -port 27015 +sv_pure 2 +map cp_badlands +maxplayers 24;

these scripts work perfectly when run as the 'gs' user.

for access purposes I would like the gameservers to always be run in screens assigned to the 'gs' account.

currently, I would like a php script to run when I visit a webpage on www.mysite.com/admin/restartservers12345 which will execure the 'runupdate.sh' script and issue a restart of the servers while updating them, my updateservers12345.html document currently looks like:

<!DOCTYPE html>

<?php
echo exec('/var/gs/runupdate.sh')
?>

<html>
<style>
p {
    text-align: center;
    color:white;
    font: bold 60px arial, sans-serif;
}
</style>
<body style="background-color: rgb(60,60,60)">

<p>Server Updating...</p>

</body>
</html>

the issue is that this script seems to be run as 'www-data' or the otherwise default Apache account, which means it cannot access the gameserver screens running on 'gs'.

currently I would like to have a box onscreen into shich people need to type the password for the gs account, and then hit enter, after which a script like

exec(echo $textboxpassword | su -s - gs -c '/var/gs/runupdate.sh')

will run, effectively running that script as the 'gs' account.

currently when trying to run that command through ssh I get an error su: must be run from a terminal and I can't seem to confirm if any of that php code is being executed properly on my server.

any help would be much appreciated. thank you.

1 Answer 1

2

you should use "sudo" instead of "su"

Add user www-data to sudoers list, you even can allow only single command '/var/gs/runupdate.sh' to execute from www-data as sudoer.

/etc/sudoers.d/www-data

www-data ALL=(ALL) NOPASSWD:  /var/gs/runupdate.sh

Update 1:

You can use

sudo -u gs bash -c '/var/gs/runupdate.sh'

And it will execute runupdate.sh from gs user

You can allow execution without password by follow lines at /etc/sudoers.d/www-data

www-data ALL=(gs) NOPASSWD:  /var/gs/runupdate.sh
Sign up to request clarification or add additional context in comments.

5 Comments

I think you might have missed part of the question sorry, the screens are running under an account 'gs', running this command with sudo doesn't give me access to those screens which are tied to a user account.
yes I see, but I still believe that to prevent "pass password" you can do via sudoer. But in in general I think you selected not so safe way to update servers that requires "su"
I will try make addition to my answer and explain what I think will be better way
Sorry i'm not fantastic with linux commands, I didn't realise sudo has the option to use another user account and not only root, so would sudo -u gs bash -c '/var/gs/runupdate.sh' execute that script as the gs account? and where do I specify that www-data can use the gs account in sudoers?
managed to get that sudo command working, still having trouble running it from within PHP but i'll open a seperate question for that, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.