OESF Portables Forum
Everything Else => General Support and Discussion => Zaurus General Forums => Archived Forums => Software => Topic started by: bam on August 14, 2005, 02:04:28 am
-
ive got a script, it uses ntpdate, but I want to run it as not root. So how in the script do I switch to root user, run ntpdate, let it complete and switch back?
Second question, I have a list of servers how would I use one(say out of a *.conf file) and if it doesnt return anything[server not there], go to the next one in the "list".
-
There are 2 ways (actually there are more but these are the easy ways) to allow a normal user to run a script as root
1) make the script suid (chmod u+s script_name.sh)
2) call sudo or su within the script (su -c ntpdate or sudo ntpdate)
2a) sudo the script, ie it runs as root, but doesn't need to be set suid
Read up on the chmod, su or sudo to understand what they do and the dangers of using them
Stu
-
found this script but getting error msg's about root doesnt own qpe/qtpalmtop data directory, any ideas, it does what I mentioned above, except with these errors.
#!/bin/sh
#
# TimeAdjust.sh by Makio Tsukamoto
# Version : 1.3.0
#
# Time adjust by ntpdate.
# Usage : timeadjust.sh [-c] [NTPDATE PATH] [TIME SERVER]
#
# --- Default Setting ---
NTPDATE=/usr/bin/ntpdate
ICONPICT=/home/QtPalmtop/pics144/timeadjust.png
EXECMODE=do
# --- Check user "zaurus" existance ---
USER=`cut -d : -f 1,3,4 /etc/passwd | grep zaurus:500:500`
if [ "$USER" = "" ]; then
USER=root:0:0
fi
USERHOME=`cut -d : -f 1,3,4,6 /etc/passwd | grep $USER | cut -d : -f 4`
CONFFILE=${USERHOME}/Settings/timeadjust.conf
LOGFILE=${USERHOME}/Documents/TimeAdjust.txt
TMPFILE=/tmp/TimeAdjust.txt
USER=`echo $USER | cut -d : -f 1`
# ---- Main Process ----
case "$EXECMODE" in
'do')
# --- read and rotate NTP server list ---
TIMESERVER=`grep -v "#" -i $CONFFILE | head -1 | cut -f1`
(
grep -v "$TIMESERVER" $CONFFILE
grep "$TIMESERVER" $CONFFILE
) >$TMPFILE
mv $TMPFILE $CONFFILE
chown $USER $CONFFILE
#--- Time Adjust ---
qcop QPE/TrayIconCtl "add(QString,QString)" timeadjust $ICONPICT
qcop QPE/TrayIconCtl "removeWith(QString,int)" timeadjust $$
(
echo "--- TimeAdjust ---"
echo "NTPDATE : $NTPDATE"
echo "USER : $USER"
echo "LOGFILE : $LOGFILE"
echo "TIMESERVER : $TIMESERVER"
echo "START : "`date`
) >>$LOGFILE
$NTPDATE $TIMESERVER >>$LOGFILE 2>&1
echo "FINISH : "`date` >>$LOGFILE
qcop QPE/TrayIconCtl "remove(QString)" timeadjust
# ---- rotate log file ----
tail -20 $LOGFILE>$TMPFILE
mv $TMPFILE $LOGFILE
chown $USER $LOGFILE
;;
'clear')
# --- Process clear ----
if [ "$EXECMODE" = "clear" ]; then
killall ntpdate
qcop QPE/TrayIconCtl "remove(QString)" timeadjust
fi
;;
esac
-
ive got a script, it uses ntpdate, but I want to run it as not root. So how in the script do I switch to root user, run ntpdate, let it complete and switch back?
How fancy do you need running ntpdate to be? Would just running it once to set your clock on each network connection be sufficient for your purposes?
John
-
yea, I guess so, just learning stuff thru this, btw I am using nethelper, it is supposed to detect a net connection(like eth0) but it doesnt when I place a script to run under Run_All or Run_ETH0 or Run_eth0, any ideas?
Also, figure I just need a ntpdate theat can be run under just zaurus user and not su, so I need to read on that.
-
yea, I guess so, just learning stuff thru this, btw I am using nethelper, it is supposed to detect a net connection(like eth0) but it doesnt when I place a script to run under Run_All or Run_ETH0 or Run_eth0, any ideas?
Also, figure I just need a ntpdate theat can be run under just zaurus user and not su, so I need to read on that.
Well the nethelper stuff is out of my element but I've posted elsewhere how to configure internal and external wireless cards to run ntpdate when the interface is brought up.
ntpdate should run fine as zaurus with the exception of actually setting the time. I'd recommend not changing that behavior as it is there for a reason. On the zaurus I suppose it isn't so critical, but it is a bad habit to develop. Typing sudo ntpdate isn't that much harder and neither is using su to change your privilege level prior to running commands like ntpdate. I don't think a non-privileged user should really need to run ntpdate anyway. Keeping the clock in sync should be done by the system without any intervention from normal users.
But it sounds like an exercise from which you can learn some new tricks to add to your arsenal for future use.
John
-
found this script but getting error msg's about root doesnt own qpe/qtpalmtop data directory, any ideas, it does what I mentioned above, except with these errors.
[div align=\"right\"][a href=\"index.php?act=findpost&pid=91808\"][{POST_SNAPBACK}][/a][/div]
This script needs to be run as user zaurus or as root using "su -" rather than just "su"
Stu
-
ive tried to set up the script as follows, running it as zaurus
#!/bin/sh
su ntpdate 'server'
kill ntpdate
It wont update the time, why?
how would I make it so ntpdate doesnt care who runs it, nah better do this the right way, thru script. So what am I doing wrong? Point me to some good tutorials.
-
You need to do
su -c ntpdate 'server'
so that su nows what the command is, better if you use sudo (if its installed by default)
Stu
-
ok sudo'd ntpdate, still "can't adjust date operation not permitted."....hmmm