Active Topics

 


Reply
Thread Tools
Posts: 3 | Thanked: 0 times | Joined on Aug 2007
#1
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's Avatar
Posts: 91 | Thanked: 25 times | Joined on Mar 2007 @ Marlow, Bucks
#2
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!
__________________
mfresh
visit the Minty Fresh N800 blog

Last edited by mfresh; 2007-08-15 at 09:07.
 
Posts: 2,152 | Thanked: 1,490 times | Joined on Jan 2006 @ Czech Republic
#3
Originally Posted by mfresh View Post
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'.
__________________
Newbies click here before posting. Thanks.

If you really need to PM me with troubleshooting question please consider posting it to the forum instead. It is OK to PM me a link to such post then. Thank you.
 
Posts: 3 | Thanked: 0 times | Joined on Aug 2007
#4
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.?
 
Posts: 2,152 | Thanked: 1,490 times | Joined on Jan 2006 @ Czech Republic
#5
Originally Posted by darkclark View Post
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)
__________________
Newbies click here before posting. Thanks.

If you really need to PM me with troubleshooting question please consider posting it to the forum instead. It is OK to PM me a link to such post then. Thank you.

Last edited by fanoush; 2007-08-17 at 07:04. Reason: paranoid exit call not exec
 

The Following User Says Thank You to fanoush For This Useful Post:
Posts: 3 | Thanked: 0 times | Joined on Aug 2007
#6
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
 
Reply


 
Forum Jump


All times are GMT. The time now is 00:41.