Sunday, March 26, 2006

Hamachi As Service In Fedora

Hamachi can be set up as a service in Linux easily. The following instructions work with Fedora and other Linux distributions that use chkconfig. A different method and set of instructions for other distributions can be found on the Hamachi forum topic. Normally, Hamachi puts configuration files in the user's home directory in .hamachi. A global configuration directory can be created
hamachi-init -c /etc/hamachi

To use Hamachi with the global configuration, the -c /etc/hamachi argument must me included in Hamachi commands
hamachi -c /etc/hamachi set-nick nickname
hamachi -c /etc/hamachi login
hamachi -c /etc/hamachi create network password
hamachi -c /etc/hamachi join network password
hamachi -c /etc/hamachi go-online network
hamachi -c /etc/hamachi list
hamachi -c /etc/hamachi go-offline network
A startup script named "hamachi" needs to be created in /etc/init.d/
#!/bin/bash
#
# hamachi         This shell script takes care of starting and stopping
#                 hamachi.
#
# chkconfig: 345 99 9
# description: hamachi is a zero-configuration VPN

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

[ -f /etc/hamachi/client.pri ]  exit 0
[ -f /etc/hamachi/client.pub ]  exit 0

[ -f /usr/bin/hamachi ]  exit 0

# See how we were called.
case "$1" in
start)
 echo "Starting hamachi..."
 /sbin/tuncfg
 /usr/bin/hamachi -c /etc/hamachi start
 ;;
stop)
 echo "Stopping hamachi..."
 killall tuncfg
 /usr/bin/hamachi -c /etc/hamachi stop
 ;;
restart)
 stop
 sleep 1
 start
 ;;
  *)
 echo "Usage: hamachi {startstoprestart}\n"
 exit 1
esac

exit 0
Next, give executable permissions to the start up script
chmod a+x /etc/init.d/hamachi
Add Hamachi to the list of services
chkconfig --add hamachi
Make sure the script was properly added
chkconfig --list  grep hamachi
The service can then be started, stopped, or restarted
service hamachi start
service hamachi stop
service hamachi restart

2 comments:

Brett said...

I just thought that I'd let you know, I found your page and help excellent! I enhanced the hamachi service script a little bit. The formatting will be all messed up because I can't use the 'pre' tag, but here is the version I currently use:

[code]
#!/bin/bash
#
# hamachi This shell script takes care of starting and stopping
# hamachi.
#
# chkconfig: 345 99 9
# description: hamachi is a zero-configuration VPN

# Run as user:
username=goober

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

HAMACHI=/usr/bin/hamachi
[ -f $HAMACHI ] || exit 0

# Make sure that ifconfig is in the path or
# will fail to start hamachi with error:
# tap: bad response 00007f00
# Failed to configure tap device to use 5.x.x.x/11111
x=`which ifconfig 2>&1 > /dev/null`
if [ $? -ne 0 ]; then
# Try to guess where it might be
if [ -e /sbin/ifconfig ]; then
export PATH=$PATH:/sbin
else
echo "tuncfg: 'ifconfig' not found in the path"
exit 1
fi
fi

hamachifilespresent=1
hamachiconfdir=/home/$username/.hamachi
#hamachiconfdir=/etc/hamachi
hclientpri=$hamachiconfdir/client.pri
hclientpub=$hamachiconfdir/client.pub
[ -f $hclientpri ] || hamachifilespresent=0
[ -f $hclientpub ] || hamachifilespresent=0
if [ $hamachifilespresent -ne 1 ]; then
echo "Hamachi client files not found: $hclientpri $hclientpub"
echo "You need to run $HAMACHI-init as user '$username' and move the files"
echo "to $hamachiconfdir in order to start the hamachi service."
fi

# NOTE: There really isn't a reason for locking
# the subsys since hamachi is designed to be run
# by multiple users

start() {
echo -n $"Starting hamachi..."
cmdout0=`/sbin/tuncfg 2>&1`
retval0=$?
x=`echo $cmdout0 | grep "already running"`
if [ $? -eq 0 ]; then
retval0=0
fi
cmdout1=`su -l $username -c "$HAMACHI -c $hamachiconfdir start" 2>&1`
retval1=$?
x=`echo $cmdout1 | grep "Hamachi is already started"`
if [ $? -eq 0 ]; then
retval1=0
fi
if [ 0 -eq $retval0 -a 0 -eq $retval1 ]; then
echo_success
else
echo_failure
fi
echo
if [ $retval0 -ne 0 ]; then
echo $cmdout0
fi
if [ $retval1 -ne 0 ]; then
echo $cmdout1
fi
}

stop() {
echo -n $"Stopping hamachi..."
cmdout0=`killall tuncfg 2>&1`
retval0=$?
x=`echo $cmdout0 | grep "no process killed"`
if [ $? -eq 0 ]; then
retval0=0
fi
cmdout1=`su -l $username -c "$HAMACHI -c $hamachiconfdir stop" 2>&1`
retval1=$?
x=`echo $cmdout1 | grep "Hamachi does not seem to be running"`
if [ $? -eq 0 ]; then
retval1=0
fi
if [ 0 -eq $retval0 -a 0 -eq $retval1 ]; then
echo_success
else
echo_failure
fi
echo
if [ $retval0 -ne 0 ]; then
echo $cmdout0
fi
if [ $retval1 -ne 0 ]; then
echo $cmdout1
fi
}

restart() {
stop
sleep 3
start
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
su -l $username -c "$HAMACHI -c $hamachiconfdir"
;;
list)
su -l $username -c "$HAMACHI -c $hamachiconfdir list"
;;
*)
echo "Usage: hamachi { start | stop | restart | status | list }\n"
exit 1
esac

exit 0
[/code]

I hope this helps you or someone else.

To change the user, find the line near the top that sets the username variable. Also, you might want to change the hamachiconfdir.

Enjoy.

Mike Chang said...

Hi there, just wanted to thank you for the instructions, made setting this up on Fedora 9 very simple!

Only thing is I've been having this problem when I try running "service hamachi start/stop/restart"

[root@localhost init.d]# service hamachi stop
/etc/init.d/hamachi: line 18: [: missing `]'
/etc/init.d/hamachi: line 19: [: missing `]'
/etc/init.d/hamachi: line 21: [: missing `]'