Howto run a shellscript in Ubuntu with root priviledges


Results 1 to 5 of 5

Thread: Howto run a shellscript in Ubuntu with root priviledges

Hybrid View

  1. #1
    Join Date
    Jan 2003
    Location
    Zurich, Switzerland
    Posts
    2,657

    Howto run a shellscript in Ubuntu with root priviledges

    Okay, shellscript newbie here, but I want to learn it after my big disaster with the bad blocks on my last harddrive.

    I have two 120GB harddrives in my case. The first one holds three partitions holding my working system:

    /dev/hdb1 is mounted as /
    /dev/hdb2 is swap
    /dev/hdb3 is mounted as /home

    Now for the second 120GB drive, it contains the same partitioning:

    /dev/hdd1 is mounted as /backup/system
    /dev/hdd3 is mounted as /backup/daten


    My ultimate goal is to have a cron job run rsync to sync my files on the "working harddrive" to the "backup harddrive". This works like a charm:

    Code:
    sudo rsync -Cavz --delete /home/ /backup/daten/
    Hence I made a little script which is supposed to mount the partition on the backup drive, run the rsync, and unmount the drive again:

    Code:
    #!/bin/bash
    mount /backup/daten
    rsync -Cavz --delete /home/ /backup/daten/
    umount /backup/daten
    This fails because Ubuntu doesn't mount the drive - only root can do that so Ubuntu exspects sudo to precede mount. However, I don't want to use sudo to ask me for my password since the whole thing should be non-interactive as it's supposed to be run as a cronjob later. BTW, root is the owner of the script. So, how can I make this script mount my harddrive without being prompted for a password?

    (Yes, I could mount the partitions by default making the necessary changes to /etc/fstab, but I want the additional security by only having root mount the partitions)

    "What can be said at all can be said clearly, and what we cannot talk about we must pass over in silence."

    Tractatus Logico-Philosophicus by Ludwig Wittgenstein (1889-1951)

  2. #2
    Join Date
    Jan 2003
    Posts
    1,012
    Well I would think if the owner of your little script there is root.root then it shouldn't have a problem mounting the partition.

  3. #3
    Join Date
    Oct 2002
    Location
    Illinois
    Posts
    3,281
    couple things you can do is add user as a mount option in your fstab, which will allow regular users to mount that partitions, secondly you can edit your sudoers file so that you dont have to use passwords

    fstab example
    /dev/hdb1 /home/dkeav/storage jfs noauto,user 0 0

    sudoers example
    dkeav ALL=(ALL) NOPASSWD: ALL

    you could designate a group a users instead of just one, if you wanted
    %admins ALL=(ALL) NOPASSWD: ALL

    hope that helps good luck

  4. #4
    Join Date
    Mar 2003
    Location
    Tampa, FL USA
    Posts
    2,193
    Simple solution:
    Set the script as a cron job for root to run.

    Root will run the script, and YOU will not be prompted.

    (Man, I thought bwkaz was the only mod who couldn't script. heheh)

  5. #5
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    Hey, wait a minute...

    Anyway, on the actual question here -- just having root own the script is not enough. Root also has to run the script (oh -- and suid bits on scripts are ignored, because there'd be some inherent race condition otherwise). The easiest way to do that is probably to stick it into /etc/cron.daily (or whatever cron directory you want); that'll run it once per day. Otherwise there's usually cron.hourly or cron.weekly or cron.monthly.

    (But depending on your distro, these directories may not exist. If they don't, you'll have to set up a cron job as root to run the script -- "sudo crontab -e" should let you do that.)

    Or just put "user" or "users" into the fstab line. ("user" is a bit more secure, because with "user", the user that mounted the FS has to unmount it. With "users", anyone can unmount it.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •