Execute Script as another user whilst not being root

前端 未结 2 1695
心在旅途
心在旅途 2020-12-21 06:50

Situation:

I\'d like to execute a shellscript directly from a web-gui. The shellscript belongs to the user \"tux\". Since my webserver is running as

2条回答
  •  难免孤独
    2020-12-21 07:06

    You can add your user tux to /etc/sudoers with NOPASSWD to allow it to run sudo without password prompt.

    E.g. add this to the end of /etc/sudoers to allow elevated execution of any command without password (note, there's a special tool for that - visudo):

    tux    ALL=(ALL) NOPASSWD:  ALL
    

    Or, a more restricted way - only allow this for your script:

    tux    ALL = NOPASSWD: /opt/tomcat/bin/shutdown.sh
    

    After that check that the changes are in effect by running any command from terminal, e.g.:

    sudo id
    

    and it should not prompt for root password.

    UPDATE:

    To make Apache run a script that belongs to another user (e.g. tux) add this line to sudoers:

    www-data ALL=(ALL) NOPASSWD: /bin/bash /opt/tomcat/bin/shutdown.sh
    

    Then you should be able to run it without password like so:

    sudo -u tux /opt/tomcat/bin/shutdown.sh
    

    Also, check these:

    • How to call shell script from php that requires SUDO?
    • sudo in php exec()
    • https://serverfault.com/questions/157272/allow-apache-to-run-a-command-as-a-different-user

提交回复
热议问题