0

This is my bash script into init.d:

#!/bin/bash
case "$1" in
  'start')
    # mount
    bindfs -n /home/my_user/.local/share/Cryptomator/mnt /home/my_user/drivefolder
 ;;
  'stop')
    fusermount -u /home/my_user/drivefolder
 ;;
  *)
    echo "Usage: $0 { start | stop }"
 ;;
esac

so i run it from root account:

/etc/init.d/drivemount.sh start
/etc/init.d/drivemount.sh stop

What I want is to mount/umount /home/my_user/drivefolder from root account to my_user account without privileges (so that the mounted folder can be manipulated without privileges).

is there any way to do this? (without having to run the script in my_user account without privileges and from another location)

Clarification: The script does not run automatically. What I want is to run the script manually, either from root or from my_user with sudo only from /etc/init.d location (for reasons unrelated to the question)

PD: I have read that with eval it can be done but I don't know.

10
  • So you want to run bindfs as my_user? Doesn't sudo -u my_user bindfs … work? Jul 10 at 17:27
  • yea. What you say is correct (I suggest you post the full answer), but I add something to it. If I run the script with sudo then I have to use eval sudo -u my_user bindfs bla bla. But it would be necessary to correct the command for umount
    – ajcg
    Jul 10 at 17:50
  • I thought the script is in /etc/init.d/ so it runs automatically.Therefore I don't understand "if I run the script with sudo". Why do you want to run it with sudo? Doesn't it run as root automatically? And I totally don't get this "eval" idea. As for unmounting, I think root can fusermount -u anything without sudo. Jul 10 at 17:55
  • It does not run automatically. I can program it in cron or run it manually from root or with sudo
    – ajcg
    Jul 10 at 18:01
  • 1
    Possibly relevant: Should I use sudo in a script or sudo an entire script? I assumed you know what you want. If you really want to run sudo ./script.sh and then drop privileges inside the script then you need sudo or something similar in the script. Jul 10 at 18:57
1

Instead of bindfs … use sudo -u my_user bindfs ….

If root runs the script then sudo should allow becoming my_user without password. If my_user runs the script then sudo should allow becoming the same user without password. If any user runs the script with sudo then it will be as if root run the script.

(Maybe some settings in sudoers can change this behavior regarding passwords. I don't know for sure. I expect the common sane configuration to behave as described though.)

My tests indicate root doesn't need sudo -u my_user before fusermount -u. However if another user simply runs the script (without sudo) and they are able to authenticate as my_user then they will be able to mount. For consistency it will be good to allow them to unmount in the same way, so sudo -u my_user before fusermount … may be a good thing.

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.