利用tb-tun让OpenVZ使用he tunnel broker的IPv6

参考文章:
IPv6 tunnel on OpenVZ
OpenVZ 通过tb-tun 使用HE IPv6 tunnel

先决条件:
1、安装git和gcc
2、申请一个he-ipv6隧道
3、需要支持tun,可以用以下方式来确认

cat /dev/net/tun
cat: /dev/net/tun: File descriptor in bad state

如果显示No such device or address 或者是 Permission denied的话则为不支持,需要联系提供商开启,或者以下脚本“可能”有帮助

if ! [ -c /dev/net/tun ]; then
mkdir -p /dev/net
mknod -m 666 /dev/net/tun c 10 200
fi

保存为.sh后尝试执行一下再cat tun,如果正常则可以继续。

第一步:将tb-tun从git上拉下来然后编译

git clone https://github.com/acgrid/tb-tun
gcc tb_userspace.c -l pthread -o tb_userspace

第二步:配置&测试一下

setsid /root/tb-tun/tb_userspace he-ipv6 [Server IPv4 Address] [Client IPv4 Address] sit > /dev/null
ip link set he-ipv6 up
ip address add [Client IPv6 Address from HE-TunnelBroker like a:b:c:d::2/64] dev he-ipv6
ip link set dev he-ipv6 mtu 1480
ip -6 route add ::/0 dev he-ipv6
ip -6 route del default dev venet0
ip -f inet6 addr
ping6 ipv6.google.com
curl -6 ifconfig.co

只要ip配置上去并且通了就可以了。
这里有一个要注意的是:有些机器如果没有用vnet0:0方式把你的公网ip配置到你本地,而是只使用了venet0(也就是类似127.0.0.2的ip)的话,请把你的客户端ip修改为venet0的IP(也就是不写公网ip而是直接写类似127.0.0.2的ip)

第三步:上面的测试成功后配置一个自动启动

vim /etc/network/if-pre-up.d/ipv6tb

#参考文章当时使用的还是ifconfig方式,可以在系统启动的时候就被正常执行,而iproute2方式则需要放在这里,否则无法正常启动
#! /bin/sh
###BEGIN INIT INFO
# Provides:          ipv6
# Required-Start:    $local_fs $all
# Required-Stop:     $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the ipv6 tunnel
# Description:       ipv6 tunnel start-stop-daemon
### END INIT INFO
# /etc/init.d/ipv6tb
 touch /var/lock/ipv6tb
 case "$1" in
     start)
         echo "Starting ipv6tb with tb_userspace"
             setsid /root/tb-tun/tb_userspace he-ipv6 [Server IPv4 Address] [Client IPv4 Address] sit > /dev/null & sleep 3s #ugly, but doesn't seem to work at startup otherwise
             ip link set he-ipv6 up
             ip address add [Client IPv6 Address from HE-TunnelBroker like a:b:c:d::2/64] dev he-ipv6 #Add as many of these as you need from your routed /64 allocation
             ip link set dev he-ipv6 mtu 1480
             ip -6 route add ::/0 dev he-ipv6
             ip -6 route del default dev venet0
         ;;
     stop)
         echo "Stopping ipv6tb with tb_userspace"
             ip link set he-ipv6 down
             ip -6 route add ::/0 dev he-ipv6
             killall tb_userspace
         ;;
     *)
     echo "Usage: /etc/init.d/ipv6tb {start|stop}"
     exit 1
     ;;
 esac
 exit 0
chmod 755 /etc/network/if-pre-up.d/ipv6tb

/etc/network/if-pre-up.d/ipv6tb start

之后基本上就可以用了,可选配置一下ipv6的dns~