1 最后由 cz (2014-08-28 15:37:07) 编辑

主题: vtun

折腾了一宿,先把链接放这,回头有时间再详述。

http://www.vtun.info/vtund.conf
http://www.enet.com.cn/article/2004/072 … 7385.shtml    \
http://www.xfocus.net/articles/200204/384.html             --- 这俩都比较老,部分参数和选项名称已改变

--------------------
VTun ver 3.X 04/12/2011
其实配置文件里有丰富的注释,只不过刚接触的人不知道这些参数该往哪写,怎么个结构。
Server: /etc/vtund.conf

options {
  port 5000; #服务器监听端口,服务器必须有公网地址,客户端可以用内网地址能连上服务端即可。
  bindaddr { addr 209.141.61.51; };#服务端监听的地址,也可以用
#下面是指定命令的绝对路径,或者这里不写,在下面直接用program path [args[...]]也可以,一样的。
  ppp           /usr/sbin/pppd;
  ifconfig      /sbin/ifconfig;
  route         /sbin/route;
#  firewall     /sbin/ipchains;
  firewall      /sbin/iptables;
  ip            /sbin/ip;
}

default { #默认选项
  type tun;        #类型或者说是模拟方式,有tun、ether、 tty、pipe四种,tun用的最多吧,除非你的vps之类的禁用了tun。
  proto udp;      #tcp udp都支持。不过我配udp没成功。
#  compress zlib:1;
  keepalive yes;
#  speed 0;
}

txc {                        #设置管道txc,貌似每个客户端需要分别配置一个这个东西
  passwd pswstr;     #链接密码
  type tun;               #这里选项覆盖默认
  proto tcp;             #同上
#  compress lzo:6; #可以设置压缩通信数据,压缩方法有lzo和zlib,等级有1(快)-9(好)
#  encrypt yes;       #加密数据
  persist yes;          #断线自动重连,no的话,断线则退出。
  keepalive yes;      #心跳包保持链接

  up {#连接成功后执行设置命令
    ifconfig "%% 10.3.0.1 pointopoint 10.3.0.2 mtu 1356";
    program "/usr/sbin/arp -sD 10.3.0.2 venet0:0 pub";
    #route "add -net 10.3.0.0 netmask 255.255.255.0 gw 10.3.0.1";
  };
  down {#链接断开后执行命令
    program "/usr/sbin/arp -d 10.3.0.2 -i venet0";
  };
}

可以用"vtund -s"手动启动服务模式,或者编辑/etc/default/vtun,设置RUN_SERVER=yes,就可以开机自动启动或者service vtun start启动


客户端大同小异,注意密码、加密等设置要与服务端一致。
Client: /etc/vtund.conf
options {
  port 5000;       #客户端用不到这个
  timeout 60;     #连接超时时间

  ppp           /usr/sbin/pppd;
  ifconfig      /sbin/ifconfig;
  route         /sbin/route;
  #firewall     /sbin/ipchains;
  firewall      /sbin/iptables;
  ip            /sbin/ip;
}

txc {
  passwd pswstr;   
#  device tun1;
  type tun;
  proto tcp;
#  compress lzo:6;
#  encrypt yes;
  persist yes;
  keepalive yes;
  up {
    ifconfig "%% 10.3.0.2 pointopoint 10.3.0.1 mtu 1356";
    program "/usr/sbin/arp -sD 10.3.0.1 eth0 pub";
    route "add -net 10.3.0.0 netmask 255.255.255.0 gw 10.3.0.1";
    program "/bin/sh /root/vtun_up_route.sh";
#    firewall "-A FORWARD -s 10.3.0.2 -d 10.3.0.1 -j ACCEPT";
  };
  down {
    program "/usr/sbin/arp -d 10.3.0.1 -i eth0";
#    firewall "-D FORWARD -s 10.3.0.2 -d 10.3.0.1 -j ACCEPT";
  };
}

哎?没有对方的ip怎么连???别着急啊,这不还有个/etc/default/vtun文件呢嘛
CLIENT0_NAME=txc   #指定配置
CLIENT0_HOST=serv.host.com #服务端地址

或者也可以用命令手动启动
vtund txc serv.host.com

-----------------
最后,别忘了在服务端加上防火墙规则,放行客户端的ip连接5000端口。

成功连接后ifconfig就会出现如下:
tun0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
          inet addr:10.3.0.2  P-t-P:10.3.0.1  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1356  Metric:1
          RX packets:11175 errors:0 dropped:0 overruns:0 frame:0
          TX packets:32063 errors:0 dropped:785 overruns:0 carrier:0
          collisions:0 txqueuelen:500
          RX bytes:784589 (784.5 KB)  TX bytes:43123934 (43.1 MB)