Feb
21
2011
vpn over ssh

It’s super easy to setup a vpn connection over an ssh access.

You dont believe me ?
You can try it by your self in only a few seconds away.

You have to be root on your local machine and you have to ssh as root to your remote machine.
To do that, change your remote /etc/ssh/sshd_config with the below values:

AllowUsers root
PermitRootLogin yes
PermitTunnel = yes

restart your ssh daemon: /etc/init.d/sshd restart

After that, copy the below shell script : ssh.vpn.sh

This is ssh.vpn.sh code:



#!/bin/sh

# Evaggelos Balaskas < ebalaskas _AT_ ebalaskas _DOT_ gr >
# Last change: Mon Feb 21 23:48:53 GMT-2 2011
# If you can spare a dollar, plz donate !

clear
echo "Give me server's name: "
read SERVER
echo "Give me ssh port: "
read PORT
SIP=`host $SERVER|tail -1 | awk '{print $NF}'`
DGW=`ip route show | grep default | awk '{print $3}'`

ssh -fw any:any $SIP -p $PORT 'ifconfig tun0 down; 
ifconfig tun0 10.20.30.40 pointopoint 10.20.30.41; 
echo 1 > /proc/sys/net/ipv4/ip_forward;
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE;
grep nameserver /etc/resolv.conf ' > /etc/resolv.conf

# Configure  Client tun0 --> point-to-point with server tun0
ifconfig tun0 down
ifconfig tun0 10.20.30.41 pointopoint 10.20.30.40

# Configure Routing
route add -host $SIP dev eth0
route add default gw 10.20.30.40 dev tun0
route del default gw $DGW dev eth0

Make this file executable: chmod +x ssh.vpn.sh
and run it !!!

You should asked to write the server’s name (eg. ssh.example.com) and the ssh port (eg. 22).
Everything else is automate to make your life

Simple as that.

Open whatismyip to see your new ip address!

Add comment

Fill out the form below to add your own comments