maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Newbie (https://talk.maemo.org/forumdisplay.php?f=26)
-   -   aliases for gainroot (https://talk.maemo.org/showthread.php?t=8856)

darkclark 2007-08-15 05:20

aliases for gainroot
 
I'm a linux n00b but i'm loving it. Today I discovered aliases and now I'm positively drunk with power. Only problem is that I find my aliases stop working as soon as I use sudo gainroot so that I can launch openvpn.

All I am trying to do is create an alias that will launch openvpn. Can't do this from the /home/user/.profile because openvpn needs root access.

I have tried editing the /root/.profile and also the /etc/.profile but still no luck. Could anyone point me in the right direction for setting up aliases that will be available within the gainroot environment?

clark

mfresh 2007-08-15 09:00

Re: aliases for gainroot
 
Here's a suggestion:

Write a script to start OpenVPN with the config file you want, call it vpn, and place it in your path. Then, once you are root, you can simply type vpn to start up OpenVPN with your chosen config file.

As far as I can tell the effect is exactly the same as having an alias called vpn which starts OpenVPN, which is what I think you are trying to acheive in a root environment.

Anyway, if you want to try this and don't know how to then this entry in my blog has a script and all the info you need to set it up. Shouldn't take more than 3 or 4 minutes.

Hope this helps - it works for me!

fanoush 2007-08-15 11:37

Re: aliases for gainroot
 
Quote:

Originally Posted by mfresh (Post 68598)
Here's a suggestion:

Write a script to start OpenVPN with the config file you want, call it vpn, and place it in your path. Then, once you are root, you can simply type vpn to start up OpenVPN with your chosen config file.

Also if you go this way and create script (and have 'sudo gainroot' giving you root), you can start your vpn script with this header

Code:

#!/bin/sh
# use gainroot to become root and relaunch itself
if [ `id -u` != 0 ] ; then
#if not already root, call itself as root
TTY=`tty`
[ "$TTY" = "not a tty" ] && unset TTY
exec sudo gainroot <<EOF
if [ -z "$TTY" ] ; then exec $0 $* ; else exec <$TTY $0 $* ; fi
EOF
exit $?
fi
# real script follows

Then you can call it directly without being root. It first switches to root automatically (if not already being root) and then continues with your thing.
Append your code (i.e. vpn call) to the end. You can even create alias to this script i.e. put it to /home/user/bin call it vpn.sh and alias v=/home/user/bin/vpn.sh in your .profile and call it with 'v'.

darkclark 2007-08-16 06:10

Re: aliases for gainroot
 
:D :D :D Well that just rocks. Thanks for the script header and tips on scripting a solution. I wrote a couple of scripts to shortcut a couple of tasks. One was launching openvpn and the other mounting a network share. I have to connect to several windows servers on a daily basis via rdc over vpn so I decided to get fancy and added commandline params to my scripts, then created an alias for each server that invokes the script and passes the relevant params for the particular server. Same thing for the network share mounting too.

I was wondering if it was possible to externalize the header to gain root access and then include it somehow, or better create a standalone script that establishes root access and exec that at the start of any script needing root acccess...but seeing as this gains root first, then relaunches itself....that may not be possible because of the recursive nature.?

fanoush 2007-08-16 07:01

Re: aliases for gainroot
 
Quote:

Originally Posted by darkclark (Post 68872)
I was wondering if it was possible to externalize the header to gain root access and then include it somehow, or better create a standalone script that establishes root access and exec that at the start of any script needing root acccess...but seeing as this gains root first, then relaunches itself....that may not be possible because of the recursive nature.?

Well it is not that long so easiest is just to include it as header. If the script does not need keyboard input, there is shorter version
Code:

#!/bin/sh
# use gainroot to become root and relaunch itself
if [ `id -u` != 0 ] ; then
#if not already root, call itself as root
exec sudo gainroot <<EOF
exec $0 $*
EOF
exit $?
fi
# real script follows

And you can strip comments too to save few bytes or put it to one line to save screen space. And also remove one paranoic exit call
Code:

#!/bin/sh
if [ `id -u` != 0 ] ; then exec sudo gainroot <<E exec $0 $* E ; fi

or even

Code:

#!/bin/sh
[ `id -u` != 0 ] && exec sudo gainroot <<E exec $0 $* E

Still worth of having it in separate file?


Also please note that this is just a hack that (ab)uses gainroot script, correct way of calling something as root is to use sudo and add your scripts to /etc/sudoers (beware of messing up this file or change permissions, reboot loop needing reflash may be the result)

darkclark 2007-08-16 22:58

Re: aliases for gainroot
 
Haha that's cool, thanks for explaining I think I can live with one line header :)

Seems like this sudoers file is a very fundamental part of linux and well worthy of some serious attention. From brief research I can see that there is a lot to learn about this, and it's very powerful given the correct understanding of syntax and logical structure.

I am waiting for my MMC card to arrive so I can set up my device for booting from the memory and get easy backup/restore for the OS before I start experimenting with sudoers :)


All times are GMT. The time now is 13:09.

vBulletin® Version 3.8.8