LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-27-2005, 02:48 PM   #1
caminoix
Member
 
Registered: Mar 2005
Location: cracow, poland
Distribution: Linnex, Gentoo, KateOS, PLD
Posts: 53

Rep: Reputation: 15
running su from a bash script


hello

i'd like to write a script that would perform something as root but could be run by any user.
i've tried simply putting "su" into it but the script exits immediatel after i enter the password. "sudo" says i don't have the permission required.
how can i prompt for the root password, execute a root-only command and come back to the normal user from inside one script?
 
Old 12-27-2005, 03:17 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
Use the "-c" option with "su" to run a command as root. See "man su".

The script below runs "whoami" three times. First as a normal user, then asks for root password, and runs "whoami" as root. And finally it runs "whoami" as the normal user again.
Code:
#!/bin/bash

whoami
su -c whoami
whoami

Last edited by Hko; 12-27-2005 at 03:20 PM.
 
Old 12-27-2005, 03:18 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
use sudo, that's what it's there for.. just read up on sudo and /etc/sudoers and you'll be a lot happier...
 
Old 12-27-2005, 06:14 PM   #4
caminoix
Member
 
Registered: Mar 2005
Location: cracow, poland
Distribution: Linnex, Gentoo, KateOS, PLD
Posts: 53

Original Poster
Rep: Reputation: 15
i've tried reading man but must have either missed or misunderstood this part.

thank you
 
Old 12-28-2005, 05:08 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,487

Rep: Reputation: 224Reputation: 224Reputation: 224
google: sudo sudoers
 
Old 12-28-2005, 06:14 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
Quote:
Originally Posted by caminoix
i've tried reading man but must have either missed or misunderstood this part.
Just type "man su" (without those double quotes) in a terminal, and you will see su's manual, where you can read how to use su, what command line options it takes, etc..

Very useful.
 
Old 12-28-2005, 06:33 AM   #7
caminoix
Member
 
Registered: Mar 2005
Location: cracow, poland
Distribution: Linnex, Gentoo, KateOS, PLD
Posts: 53

Original Poster
Rep: Reputation: 15
erm, this is what i did and still failed to find the answer... but thanks anyway
 
Old 12-28-2005, 03:41 PM   #8
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Quote:
Originally Posted by caminoix
i've tried reading man but must have either missed or misunderstood this part.

thank you
One of the things you can configure sudo to do is to let a particular user or users run a particular command or commands as root. So the idea would be to do something like configure the /etc/sudoers file so that anyone you ran your command could do so as root.

I don't recall the exact syntax for the file, but as others have pointed out, it is in the man pages, on search engines, etc. One thing to be aware of is that it is typically advisable to edit the /etc/sudoers file with the "visudo" command.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script running as daemon Satriani Programming 12 03-29-2011 05:23 PM
Bash Script - How do I make sure my program is still running? nro Linux - Newbie 4 08-28-2005 01:57 PM
Running two copy's of the same bash script AndiOliver Programming 2 10-06-2004 03:46 AM
bash script, monitoring running PID? thebover Programming 4 08-26-2004 02:42 PM
Running bash script files in C++ (Kylix) Acherion Programming 4 07-25-2003 11:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration