How do I run a shell script as root (sudo)?
Asked Answered
T

6

15

I have a SVN repository server that runs under the repository user. I want to run a script after every post-commit action. I wrote a shell script that runs from the hook after every commit. It needs to be run as root. This is why I used sudo in the script, but it didn't work. Is there any way to run the script as root?

sudo su
echo "password"
svn export --force file:///home/repository/trunk/ /home/memarexweb/public_html/devel/
chmod -R 777 /home/memarexweb/public_html/devel/
Towbin answered 21/9, 2011 at 11:50 Comment(3)
better to move the question to superuser.comWashbowl
one option could be to disable password but isnt really a good solution maestric.com/doc/unix/ubuntu_sudo_without_passwordBibelot
It might not be a good idea to make files mode 777, they will be world writeable.Springhead
T
15

I was searching around and found this useful solution:

Edit your sudoers file to allow running certain commands without a password.

It's best to split your post-commit script into two parts, one of which will be run through sudo.

  • entry in /etc/sudoers:

    loqman    ALL=(root) NOPASSWD: /usr/local/bin/svn-postcommit-export
    
  • Your post-commit hook:

    #!/bin/sh
    sudo /usr/local/bin/svn-postcommit-export
    
  • Script /usr/local/bin/svn-postcommit-export:

    #!/bin/sh
    svn export --force file:///home/repository/trunk/ /home/memarexweb/public_html/devel/
    chmod -R 777 /home/memarexweb/public_html/devel/
    

    (You can choose any name and put the script anywhere; I just suggested svn-postcommit-export as an example, and /usr/local/bin as a common location.)

Towbin answered 15/11, 2011 at 23:13 Comment(0)
L
7
sudo su

starts a new process, owned by the root user. After that process is terminated or stopped, the next line is executed, again as the user that executes the script.

A possible solution is to run the whole script using sudo, and to give that use sudo rights to exectute the scripts. In order to do that, you need to edit the /etc/sudoers file using the visudo command.

Lengel answered 22/9, 2011 at 9:34 Comment(0)
P
6

Run a script as root:

sudo sh your-script.sh
Pavier answered 6/4, 2020 at 20:40 Comment(0)
W
1

This will not work, the best thing is to put only the requires commands in the shell script. And then setuid the script itself, like this (using root):

chmod u+s myscript.sh

Like this, executing this script will give you the permissions of the owner of the script (root).

EDIT: As mentioned in comments, stuid is not allowed for shell script. This solution works for executable files only.

Washbowl answered 21/9, 2011 at 11:57 Comment(3)
Setting suid flags on shell script is DANGEROUS to the extend, that Linux will ignore it. See the Wikipedia article linked in the answer for details.Waldheim
sorry A.H., I didn't fully understand you. You mean Linux will ignore setuid? And why it is DANGEROUS?Washbowl
Linux will ignore suid on scripts, executables are ok. Many UNIX derivates do so in these days. Some reasons are explained hereWaldheim
B
1

In the last line of your script, you're changing the mode of /home/memarexweb/public_html/devel/ to 777, so user "repository" should be able to copy files to that directory without root privileges. In that case, you don't need to use sudo or su.

However, changing the permissions of the directory to 777 is dangerous, as it allows anyone to write to that directory and create or delete files. It would be better to change the ownership of the directory to user "repository" and change the mode to 755. If that's not feasible, you may be able to add a POSIX ACL allowing "repository" to write to the directory. You can Google "POSIX ACL" for more information, or read the man pages for getfacl and setfacl.

Boorman answered 21/9, 2011 at 12:21 Comment(0)
P
0

Google just coughed up this nice tidbit from Geeks for geeks for me:

    #!/usr/bin/sudo bash
    
    echo "Do your sudo script stuff here"
Patrizius answered 17/9, 2023 at 12:32 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Blithe

© 2022 - 2024 — McMap. All rights reserved.