How to run a shell script as root (when user is not root)
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to run a shell script as root (when user is not root)
Ubuntu 8.04 - Gnome - here is the the issue:
I wrote a shell script to copy the data under /home to a USB drive for backup. I intended to schedule the job to run every day at 1 AM. However, while the PC will go into standby as I want it to, it will not wake up to run the script. I have another PC of the same model and I can wake it up by using the auto power on in BIOS - but not this one(?)
The script will copy data for 2 user accounts, mine and the wife's thus it must be run with root permissions. Her account is not a sudoer and it should not be. Her account is generally logged onto the PC and she would be the one to execute the script manually until I figure out how to resolve the power on issue. What I need to do is create a launcher which will run the script with root permissions without her having to logon with my credentials in order to sudo the script.
I believe I have come across a way to make a program or script run as root without having to be root or know the root password. How to do it escapes me at the moment.
Why not add your wife's user account to sudoers and configure it so that only your script can be run (and then prevent her from modifying the script)? For the time being.
That is certainly a possibility. However, she is not happy about having to push the power button and type in her password to get back in after the PC goes to standby (as opposed to just bumping the mouse in XP - until service pack 3 destroyed my finely crafted install). She uses the PC for Web and email and that is about as far as her interest goes.
Still, this is more of an intellectual challenge for me. I recall that early versions of Red Hat required root permission to connect to the Internet with a modem or to burn a CD with XCDRoast. There was a way to fix those although I do not remember how. I guess I need to see if I can find any of my old notes.
I dont know anything like what u tring to do but in order to write data to disk just set permission on usb drive to allow her to write to the disk.
you dont need to run script as root if its just copy /home paste /usb.
Thanks darthaxul. I just reviewed permissions on the various directories and files under /home and see that they are 755 and 644. Umask is 022. This is my first experience with setting up a Linux box with multiple accounts where another user will actually logon and use the PC (other than for testing or to share a printer or folder with Samba). I guess I was expecting a default Umask of 066 where each user's files would be private.
Regardless, my script checks to see if the USB drive is mounted, if not it mounts it, does the cp then umounts the USB drive. At a minimum the account running the script must be able to execute mount and umount. I have tried granting these permissions in /etc/sudoers with NOPASSWD: however, I must have a syntax error as I do not get the result I expect. I will keep working on it.
Ken
Last edited by taylorkh; 09-10-2008 at 09:47 AM.
Reason: typo :-(
One thing to keep in mind with the sudoers file is that the order of the rules matters. So to make sure the sudoers line that you add isn't overridden by some other rule, be sure to add your rule to the end of the file. Here is an example of how to use the NOPASSWD syntax in case you haven't gotten it working all ready:
Code:
john ALL = NOPASSWD: /path/to/cmd1, /path/to/cmd2, path/to/cmd3
As they say in the movies "belay my last" - I have explored the permissions under /home a little more and am back to my original position that my script should be run as root. For example:
Quote:
ken@taylor9:~$ sudo ls -l /home/ken/.config
total 16
drwx------ 3 ken ken 4096 2008-08-29 12:52 compiz
drwx------ 2 ken ken 4096 2008-08-29 13:23 tracker
-rw------- 1 ken ken 630 2008-08-29 12:24 user-dirs.dirs
-rw-r--r-- 1 ken ken 5 2008-08-29 12:24 user-dirs.locale
It would appear that only the account owner or root can copy all files under their personal /home/user directory. Only root can copy all files for ALL users so I guess I am back to my original question of how to run a script as root when the user is not root does not have the root password.
Back at the beginning of the thread I indicated that I had planned to run the job as with the scheduler - and it will work if I schedule it with root credentials. The problem is that this particular PC will not wake from standby to run the job.
Windoze will let me wake the PC from within the scheduler. I do not know if/how that can be done with cron.
The PCs are Dell Dimension 4600s. The "newer" one with a 2.8 GHz processor seems to wake up from standby when set in BIOS to "automatic power on" at appointed time (about a minute before the cron job) just fine. The older PC with a 2.66 GHz processor will not wake from standby using the same method. I have upgraded the bios on both to the latest A12 from Dell.
I think I will try putting the offending PC in hibernate and see if BIOS can awaken it.
Last edited by taylorkh; 09-12-2008 at 06:13 PM.
Reason: correct BIOS rev
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.