LinuxQuestions.org
Review your favorite Linux distribution.
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 02-09-2005, 04:07 AM   #1
box_l
Member
 
Registered: Jul 2003
Location: Hereford,England
Distribution: mandrake 10 , Novell Desktop Linux (suse) , knoppix
Posts: 85

Rep: Reputation: 15
Question making a script run with root rights


i have this script that contains various commands that need root or su rights.

i do not want the user to open a root console to run the script, but they can know the root password to get the rights.

there are several 'su' commands, this will require a password to be entered every time.

how can i get them to enter the password only once?

all the ways i hve tried either leave the script running while opening a new shell as su so you have to exit that only to find the rest still waiting for input.

can i pass the rest of the script up to the shell which has more rights?

can i get the user to enter the password (invisibly) and retain it for the other commands?

can i pass password info to 'su' ?

and will any of the suggestions change the value of 'user'

TIA


BoX


#!/bin/bash
# Written by Dunstan Lawrence for HCT

validateScriptExecution()
{
id=`id | awk '{print $1}'|awk -F"=" '{print $2}'|awk -F"(" '{print $1}'`

if [ $id != 0 ]
then
echo ""
echo ""
echo "%% You will need the root password to execute this script. %%"
elif [ $id = 0 ]
then
echo ""
echo ""
echo "Please run as a User!"
exit
fi
}
validateScriptExecution

# this wiil give the id of the logged on user
user=`id | awk '{print $1}'|awk -F"(" '{print $2}'|awk -F")" '{print $1}'`
echo ""
echo "Please log in to Network server her-print with the novel client"
echo ""
echo "press any key to copy linux.2 file from server"
read $JUNK

echo "copying from server.."

# copies from server to local directory
cp -f /home/$user/HCT-TREE/HER-PRINT/sys/TFTP/linux.2 `pwd`/linux.2

echo ""
echo "Mounting Linux.2 as Directory 'looplinux2'"
echo "May reqire password"
echo ""

mv linux.2 linux.gz
gzip -d linux.gz
su -c "mount -o loop linux looplinux2"

echo ""
echo "Mounted"
echo ""
echo "Opening 'Menu' file for editing"
sleep 3
#cp `pwd`/looplinux2/bin/menu `pwd`
su -c "nano `pwd`/looplinux2/bin/menu"
#emacs `pwd`/menu

#echo "saving to looplinux2"
#echo ""
#echo "May reqire password"#

#su -c "cp `pwd`/menu `pwd`/looplinux2/bin"

echo "Tidying..May require password"

# this kills any processes that may be preventing the loop device from being unmounted
su - -c lsof | grep /home/$user/Documents/Zen/looplinux2 | awk -F\ '{ print $2; }' | xargs -r kill

echo "Unmounting looplinux2... May require password"

su -c "umount looplinux2"
gzip -9c linux > linux.gz
mv linux.gz linux.2
rm linux

echo "making backup of linux.2 and copying back to server"

# makes backup with timestamp
mv /home/$user/HCT-TREE/HER-PRINT/sys/TFTP/linux.2 /home/$user/HCT-TREE/HER-PRINT/sys/TFTP/linux.2.`date "+%m-%d-%y__%H:%M"`.old
# copies to server
cp -f `pwd`/linux.2 /home/$user/HCT-TREE/HER-PRINT/sys/TFTP/
#opens browser windows on relevant locations
pwd | xargs konqueror &
konqueror /home/$user/HCT-TREE/HER-PRINT/sys/TFTP/ &
 
Old 02-09-2005, 05:15 AM   #2
heema
Senior Member
 
Registered: Sep 2003
Location: Egypt
Distribution: Arch
Posts: 1,528

Rep: Reputation: 47
u could use sudo so that the user will enter his own password only .
the user will only type : sudo ./script.sh

but u must edit the file sudoers located in /etc to insert in it ur script
 
Old 02-09-2005, 08:20 AM   #3
box_l
Member
 
Registered: Jul 2003
Location: Hereford,England
Distribution: mandrake 10 , Novell Desktop Linux (suse) , knoppix
Posts: 85

Original Poster
Rep: Reputation: 15
ok sudo works.

i did think of this before but wanted 'any' user on 'any'

machine to be able to run it.

still it does the job for now.

except::

sudo - -c lsof | grep /home/$user/Documents/Zen/looplinux2 | awk -F\ '{ print $2; }' | xargs -r kill

can you give me the correct syntax for this line please.

BoX
 
Old 02-09-2005, 09:10 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
try :
Code:
lsof | grep /home/$user/Documents/Zen/looplinux2 | awk -F' ' '{ print $2; }' | xargs sudo kill
 
Old 02-09-2005, 09:43 AM   #5
box_l
Member
 
Registered: Jul 2003
Location: Hereford,England
Distribution: mandrake 10 , Novell Desktop Linux (suse) , knoppix
Posts: 85

Original Poster
Rep: Reputation: 15
mmmmm will try but,

its 'lsof' that needs the sudo

it also needs su path rights to run it
 
Old 02-09-2005, 10:25 AM   #6
KimVette
Senior Member
 
Registered: Dec 2004
Location: Lee, NH
Distribution: OpenSUSE, CentOS, RHEL
Posts: 1,794

Rep: Reputation: 46
setting the suid bit on the script might work.
 
Old 02-09-2005, 12:30 PM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
its 'lsof' that needs the sudo
In which distribution lsof require root privileges ? mandrake 10 ?
 
Old 02-09-2005, 10:11 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.3
Posts: 17,548

Rep: Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423
Write the script as root user, use full absolute paths for each external cmd, set user in sudoers file to be able to run that program. Remove all 'su' cmds.
 
Old 02-10-2005, 02:56 AM   #9
box_l
Member
 
Registered: Jul 2003
Location: Hereford,England
Distribution: mandrake 10 , Novell Desktop Linux (suse) , knoppix
Posts: 85

Original Poster
Rep: Reputation: 15
@ keefaz

yes mandrake 10 is my distro.

is this not the case in all?

@ chrism01

thanks, full paths,
i will give it a go.

BoX
 
Old 02-10-2005, 03:51 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
On my slackware box, lsof is available for all users
 
Old 02-10-2005, 04:47 AM   #11
box_l
Member
 
Registered: Jul 2003
Location: Hereford,England
Distribution: mandrake 10 , Novell Desktop Linux (suse) , knoppix
Posts: 85

Original Poster
Rep: Reputation: 15
maybe i should change distros!?

BoX
 
Old 02-10-2005, 05:01 AM   #12
box_l
Member
 
Registered: Jul 2003
Location: Hereford,England
Distribution: mandrake 10 , Novell Desktop Linux (suse) , knoppix
Posts: 85

Original Poster
Rep: Reputation: 15
full path working !

thanks people!
 
Old 02-10-2005, 09:44 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.3
Posts: 17,548

Rep: Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423Reputation: 2423
BTW, put the script in a root-only accessible dir ...
 
  


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
How to run a script as root upon login zugvogel Linux - Newbie 7 09-09-2005 11:10 AM
run script as root in KDE m_a_b Linux - Software 3 10-15-2004 07:00 PM
making sh script have root permmission demmylls Linux - General 10 08-21-2004 12:22 AM
Script with root rights on startup - swap space initialization TroelsSmit Linux - Newbie 4 06-30-2004 11:58 PM
Need to run shell script as root kaloyer Programming 4 06-27-2004 09:01 AM

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

All times are GMT -5. The time now is 09:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration