• VPN@Fedora 9 with xl2tpd - [走很长路]

    2008-08-26 | Tag:Linux

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://cycasii.blogbus.com/logs/28182220.html

          今天成功地搞定了VPN,虽然说速度也就那样子,不过比起之前用代理的效果还是赞了很多。在这里和大家分享下!

          1、安装xl2tpd

           安装xl2tpd其实是一个很简单的事情,但是呢,要小心的是也许你并没有安装成功。我是吃过这个苦的,在这里我先说说我没有安装成功的case。

          A、到 www.xelerance.com/software/xl2tpd/去下载xl2tpd-1.1.12.tar.gz。

           #tar zxvf xl2tpd-1.1.12.tar.gz
           #cd  xl2tpd-1.1.12
           #make
           #make install
           #mkdir /etc/xl2tpd/

           但是我用rpm -q xl2tpd查看了一下,发现并没有安装成功。

          B、找一个速度快的代理,直接通过yum安装xl2tpd。如果速度不够快的话,会time out连接不上source。可以另外找台电脑,给自己开个代理。至少我是这么做的。

          yum install xl2tpd

          2、写配置文件

          #mkdir /etc/xl2tpd/
          #cd /etc/xl2tpd/
          #vi xl2tpd.conf

          xl2tpd.conf的内容如下:

          [global]                              ; Global parameters:
          port = 1701                           ; * Bind to port 1701
          [lac zju]                             ; Example VPN LAC definition
          lns = lns.zju.edu.cn                  ; * Who is our LNS?
          refuse pap = yes                      ; * Refuse PAP authentication
          require authentication = no           ; * Require peer to authenticate
          name = zealot@a                       ; * Report this as our hostname
          ppp debug = yes                       ; * Turn on PPP debugging
          pppoptfile = /etc/l2tpd/zju.options   ; * ppp options file for this lac
          其中name就是你的VPN帐号。

          #vi zju.options

          zju.options的内容如下:

          asyncmap 0
          noauth
          crtscts
          lock
          hide-password
          modem
          netmask 255.255.255.0
          proxyarp
          lcp-echo-interval 30
          lcp-echo-failure 4
          ipcp-accept-local
          ipcp-accept-remote
          noipx

          3、编辑密码文件

          #vi /etc/ppp/chap-secrets

          chap-secrets的内容如下:
          # Secrets for authentication using CHAP
          # client        server  secret                  IP addresses
          *       *               "password"                * 

          password是的的VPN密码,保留引号。

          4、添加hosts

          #mkdir /var/run/xl2tpd
          然后,在/etc/hosts加入一行
          10.5.1.9 lns.zju.edu.cn 

          5、运行与调试

          启动 l2tpd

          # /etc/init.d/xl2tpd start
          终端这里出现Starting xl2tpd:并且没有什么错误提示就ok了
          然后发送连接到 vpn 的命令
          # echo "c zju" > /var/run/xl2tpd/l2tp-control
          之后运行一下 ifconfig(如果你的path没有设置好的话,就用全路路径/sbin/ifconfig),如果看到 ppp0 设备,那么恭喜你成功了!
          ppp0      Link encap:Point-to-Point Protocol 
                    inet addr:172.16.37.39  P-t-P:172.16.255.1  Mask:255.255.255.255
                    UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1442  Metric:1
                    RX packets:267606 errors:0 dropped:0 overruns:0 frame:0
                    TX packets:189248 errors:0 dropped:0 overruns:0 carrier:0
                    collisions:0 txqueuelen:3
                    RX bytes:375656610 (358.2 MiB)  TX bytes:11688257 (11.1 MiB) 

          路由配置

          要让校内的网络资源仍然通过原先的路由,而校外资源则通过 vpn,需要配置 一下路由。连接到 vpn 以后,用 ip route(如果你的path没有设置好的话,就用全路路径/sbin/ip route)查看当前路由。

          # ip route
          222.205.64.1 dev ppp0 proto kernel scope link src 222.205.74.23
          222.205.9.0/24 dev eth0 proto kernel scope link src 222.205.9.2
          default via 222.205.9.1 dev eth0
          可以看出默认路由是通过网关,而不是 vpn ,我们希望 10.* 、 210.32.* 和 222.205.* 使用默认路由

          # ip route add 10.0.0.0/8 via 222.205.9.1 dev eth0
          # ip route add 222.205.0.0/16 via 222.205.9.1 dev eth0
          # ip route add 210.32.0.0/16 via 222.205.9.1 dev eth0

          注意 222.205.9.1 要替换成你刚才 ip route 所看到的 default 一句里面的那 个地址。

          然后删除原来的默认路由

          # ip route del default

          注意不要先删除默认路由再设置 10.* 的路由,因为 vpn 服务器在 10.* 网段,删除默认路由将直接导致 vpn 断开。

          设置其他地址使用 vpn ,地址可以通过 ifconfig 查看

          # ifconfig
          ppp0 Link encap:Point-to-Point Protocol
          inet addr:222.205.74.23 P-t-P:222.205.64.1 Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1442 Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:40 (40.0 b) TX bytes:46 (46.0 b)

          就是 P-t-P 那里的地址,这里是 222.205.64.1 ,然后设置新的默认路由

          # ip route add default via 222.205.64.1 dev ppp0

          这样 vpn 就配置完毕可以使用了。 

          6、VPN连接脚本

          #!/bin/bash
          ########################################
          # Shell script to (re)connect to VPN
          # pluskid <pluskid.zju@gmail.com>
          ########################################
          ####################
          # Variables
          ####################
          ## How many seconds to wait for the ppp0 to come up each try
          TIMEOUT=10
          ## How many times to try to bring up ppp0 until fail
          N_RETRY=3
          ## Interface default to use
          INTERFACE=eth0
          ## How many seconds to wait for l2tpd to restart
          L2TPD_TIMEOUT=10
          ## For xl2tpd: /etc/init.d/xl2tpd
          ## For l2tpd: /etc/init.d/l2tpd
          L2TPD_SCRIPT=/etc/init.d/xl2tpd
          ## For xl2tpd: /var/run/xl2tpd/l2tp-control
          ## For l2tpd: /var/run/l2tp-control
          L2TPD_PIPE=/var/run/xl2tpd/l2tp-control
          ####################
          # Commands
          ####################
          function usage
          {
          echo "$1 login to the school VPN."
          echo "$1 [-h] [-r]"
          echo " Default to connect to VPN."
          echo " -r reconnect."
          echo " -h help, i.e. show this information."
          }
          function connect
          {
          if ppp0_alive ; then
          echo "Already connected."
          else
          bring_up_ppp0 && setup_route
          fi
          }
          function reconnect
          {
          restart_l2tpd && bring_up_ppp0 && setup_route
          }
          ####################
          # Internal utility functions
          ####################
          function super_user
          {
          if [ "$UID" = "0" ]; then
          return 0 # Yes, super user
          else
          return 1
          fi
          }
          function ppp0_alive
          {
          if /sbin/ifconfig | grep -s 'ppp0' > /dev/null ; then
          return 0 # Yes, connected
          else
          return 1
          fi
          }
          function bring_up_ppp0
          {
          for i in $(seq $N_RETRY)
          do
          echo -n "Trying to bring up ppp0"
          echo "c zju" > $L2TPD_PIPE
          for j in $(seq $TIMEOUT)
          do
          if ppp0_alive; then
          echo " Done!"
          return 0 # Yes, brought up!
          fi
          echo -n "."
          sleep 1
          done
          echo
          done
          echo "ERROR: Failed to bring up ppp0!"
          return 1
          }
          function setup_route
               {
          echo "Setting up route..."
          STATIC=$(/sbin/ip route | grep '^default' | cut -d" " -f3)
          # If already set the route before, we cannot find
          # the original default route any more. But fortunately,
          # the route previously setted often won't change and
          # we can just safely ignore this task
          if [ ! -z $STATIC ]; then
          /sbin/ip route add 10.0.0.0/8 via $STATIC dev $INTERFACE
          /sbin/ip route add 222.205.0.0/16 via $STATIC dev $INTERFACE
          /sbin/ip route add 210.32.0.0/16 via $STATIC dev $INTERFACE
          /sbin/ip route add 239.43.1.1 via $STATIC dev $INTERFACE
          /sbin/ip route del default
          fi
          PPP=$(/sbin/ifconfig | sed -n '/P-t-P/s/^.*P-t-P:\([0-9.]*\).*$/\1/p')
          /sbin/ip route add default via $PPP dev ppp0
          echo "Done!"
          }
          function restart_l2tpd
          {
          echo "Restarting l2tpd..."
          $L2TPD_SCRIPT restart
          for i in $(seq $L2TPD_TIMEOUT)
          do
          if [ -e $L2TPD_PIPE ]; then
          echo "Done!"
          return 0 # Successfully restarted!
          fi
          sleep 1
          done
          echo "ERROR: Failed to restart l2tpd!"
          return 1 # Failed to restart l2tpd
          }
          ####################
          # Main
          ####################
          if ! super_user ; then
          echo "ERROR: You must be super user to run this utility!"
          exit 1
          fi
          if [ $# -lt 1 ]; then
          connect
          elif [ "$1" = "-h" ]; then
          usage
          else
          reconnect
          fi 

     

          将以上内容保存为 vpn.sh ,然后添加可执行权限 chmod +x vpn.sh ,之后直接执行就可以连接 vpn : ./vpn.sh ,加参数 -r 表示重新连接, -h 显示帮助信息。 

     


    收藏到:Del.icio.us




    引用地址:

    评论

  • A、到 www.xelerance.com/software/xl2tpd/去下载xl2tpd-1.1.12.tar.gz。

    #tar zxvf xl2tpd-1.1.12.tar.gz
    #cd xl2tpd-1.1.12
    #make
    #make install
    #mkdir /etc/xl2tpd/

    但是我用rpm -q xl2tpd查看了一下,发现并没有安装成功。

    源代码编译安装的,通过rpm命令怎么可以查看出来呢?
  • momo
  • another process has the yum lock pid XXXX
    羽衣白菜回复staywizu说:
    zanbuluota...
    2008-08-27 00:48:09
  • 呵呵