39

I am working on automating the creation of subversion repositories and associated websites as described in this blog post I wrote. I am running into issues right around the part where I su to the www-data user to run the following command:

svnadmin create /svn/repository

There is a check at the beginning of the script that makes sure it is running as root or sudo, and everything after that one command needs to be run as root. Is there a good way to run that one command as www-data and then switch back to root to finish up?

4 Answers 4

45

Just use su - www-data -c 'svnadmin create /svn/repository' in your script run by root. So that only this command is run by www-data user.


Update for future viewers:

In case you receive a "This account is currently not available" error, consider using:

su - www-data -s /bin/bash -c 'svnadmin create /svn/repository'

( @Petr 's valuable mention about -s flag to accommodate www-data user's no login policy )

4
  • 1
    Yeah, it appears I forgot a basic rule of scripting... RTFM. Thanks! May 10, 2012 at 18:18
  • 17
    Trying this, I get the error This account is currently not available.
    – rubo77
    Nov 21, 2015 at 12:10
  • I also get this error, but futbolsalas15's answer below works fine.
    – Andrew
    Feb 5, 2016 at 4:00
  • 21
    Use su - www-data -s /bin/bash -c 'your_command' to make this work. User www-data has shell /usr/sbin/nologin so without the -s parameter it leads to the error message.
    – Petr
    Feb 19, 2016 at 11:53
77

With 'su' is probably that request a password, and www-data doesn't have password. I recommend the command sudo:

 sudo -u www-data command

The condition is that your user must be root, or configurated in the sudoers file

2
  • 6
    Wow, why isn't this the selected answer? Took me way too long to find this. Dec 12, 2017 at 13:52
  • Yes, this is the easiest approach to running a command as www-data user. Thanks!
    – Michael J
    Mar 5, 2022 at 19:56
5

2 Possible approaches:


1) sudo command:

In most cases you will have access to the sudo command and so the solution is simply:

sudo -u target_user target_command


2) su command (if sudo not installed. Ex. alpine-linux images):

su - target_user -c 'target_command'

In case you receive a 'This account is currently not available' error, the user has a no login (shell access) policy in effect. If so, consider using:

su - target_user -s /bin/bash -c 'target_command'

(Based on @Petr 's valuable comment about -s flag to accommodate www-data user's no login policy)

1
  • the sudo -u www-data target_command version worked for me, thanks!
    – Jared
    Dec 3, 2020 at 17:06
3

Use su:

   su [options] [username]

   The options which apply to the su command are:

   -c, --command COMMAND
       Specify a command that will be invoked by the shell using its -c.
1
  • 3
    It should be noted that su commands don't work with accounts that have disabled logins (such as www-data). You'll have to use sudo.
    – Frantumn
    Jun 2, 2017 at 14:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .