为你的服务器添加免费的公网IPv6!

laomoy
2023-08-18 / 0 评论 / 518 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年08月21日,已超过517天没有更新,若内容或图片失效,请留言反馈。

今天教大家获取免费的公网IPv6,准确的说是一个公网的IPv6隧道 并且还是64位的掩码哟~

介绍完毕开始本次的教程,本次教程用到的官网地址是 https://www.tunnelbroker.net/
Tunnelbroker

登录后选择创建IPv6 Create Regular Tunnel
开始创建隧道

PS:填写VPS的公网IPv4地址进行验证 记得开启 ICMP 需要所有权验证!

然后选择你想要的IPv6隧道地区 推荐你这台服务器相近的地区~

复制配置文件
找到选择配置示例(Example Configurations),选择 Linux-net-tools,然后你会看到这5行配置,复制出来,然后直接粘贴在SSH终端即可!

ifconfig sit0 up
ifconfig sit0 inet6 tunnel ::216.218.221.6 #隧道通信的IPv4
ifconfig sit1 up
ifconfig sit1 inet6 add 2001:***:**:**::*/64 #分配给你的IPv6
route -A inet6 add ::/0 dev sit1

执行ip a或ifconfig查看是否添加成功


[root@test-tpe /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:16:3c:e3:14:e4 brd ff:ff:ff:ff:ff:ff
    inet 23.***.***.**/28 brd 23.***.***.** scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::216:3cff:fee3:14e4/64 scope link
       valid_lft forever preferred_lft forever
3: sit0@NONE: <NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN group default qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
    inet6 ::23.***.***.**/96 scope global
       valid_lft forever preferred_lft forever
    inet6 ::127.0.0.1/96 scope host
       valid_lft forever preferred_lft forever
4: sit1@NONE: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN group default qlen 1000
    link/sit 0.0.0.0 peer 216.218.221.6
    inet6 2001:***:**:**::*/64 scope global
       valid_lft forever preferred_lft forever
    inet6 fe80::17f8:b05c/64 scope link
       valid_lft forever preferred_lft forever
[root@test-tpe /]#

在网卡 inet6 可以看到有2001开头的IPv6地址,恭喜你已经添加成功了!
PS:网卡重启 服务器重启 这些临时配置都会失效的
如何实现永久生效呢?

Centos教程

让脚本开机执行即可 我这里利用Centos的/etc/rc.d/rc.local

先创建脚本文件touch /getipv6.sh

编辑/getipv6.sh脚本文件,添加配置命令 就是添加上面那5行命令

[root@test-tpe /]# ls
bin   dev    lib    lost+found  mnt  patch  root  sbin  sys  usr  www
boot  etc  home        lib64  media       opt  proc   run   srv   tmp  var
[root@test-tpe /]# touch /getipv6.sh
[root@test-tpe /]# vi /getipv6.sh
ifconfig sit0 up
ifconfig sit0 inet6 tunnel ::216.218.221.6
ifconfig sit1 up
ifconfig sit1 inet6 add 2001:***:**:**::*/64
route -A inet6 add ::/0 dev sit1
#保存后退出(按下Esc后 按住Shift+ZZ即可保存退出)

保存后执行 cat /getipv6.sh 查看是否添加进去了

[root@test-tpe /]# cat /getipv6.sh
ifconfig sit0 up
ifconfig sit0 inet6 tunnel ::216.218.221.6
ifconfig sit1 up
ifconfig sit1 inet6 add 2001:***:**:**::*/64
route -A inet6 add ::/0 dev sit1

[root@test-tpe /]#

为getipv6.sh文件赋予执行权限

[root@test-tpe /]# sudo chmod +x getipv6.sh

编辑/etc/rc.d/rc.local文件,添加getipv6.sh脚本配置到rc.local的最后一行


[root@test-tpe /]# vi /etc/rc.d/rc.local
[root@test-tpe /]# cat /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
sh /getipv6.sh
[root@test-tpe /]#

最后为rc.local文件赋予执行权限

sudo chmod +x /etc/rc.d/rc.local

启用rc-local服务以确保在启动时运行

sudo systemctl enable rc-local

启动rc-local服务

sudo systemctl start rc-local

Ubuntu教程

在Ubuntu系统中,开机自动执行脚本的方式与CentOS略有不同。Ubuntu通常使用systemd作为初始化系统,因此我们使用systemd来管理开机自启动的脚本。

创建Service文件: 使用文本编辑器创建一个.service文件,例如:

sudo vi /etc/systemd/system/getipv6.service

在文件中输入以下内容:

[Unit]
Description=Get IPv6 Script
After=network.target

[Service]
Type=oneshot
ExecStart=/bin/bash -c "ifconfig sit0 up && \
                        ifconfig sit0 inet6 tunnel ::216.218.221.6 && \
                        ifconfig sit1 up && \
                        ifconfig sit1 inet6 add 2001:***:**:**::*/64 && \
                        route -A inet6 add ::/0 dev sit1"
RemainAfterExit=true

[Install]
WantedBy=default.target

PS:请注意,IPv4和IPv6地址需要填写He.net分配给你的 不要直接照搬我的哈~

然后保存文件并重新加载 systemd 配置:

sudo systemctl daemon-reload

随后启用和启动你的 Service:

sudo systemctl enable getipv6.service
sudo systemctl start getipv6.service

PS:systemd在大多数现代Linux发行版中被广泛使用,Centos也可以用Ubuntu的方法。

配置完成后 重启服务器后会自动执行脚本 做到重启后IPV6隧道也不失效啦~

83

评论 (0)

取消