centos7 开启ipv6
如果服务器使用的是阿里云公开的 CentOS 镜像,可按以下步骤操作:
1、阿里云公开的 CentOS 镜像将 IPv6 支持给去掉了,需要加载相关模块
1.1 修改 /etc/modprobe.d/disable_ipv6.conf
## 修改 options ipv6 disable 为 0
-
cp /etc/modprobe.d/disable_ipv6.conf /etc/modprobe.d/disable_ipv6.conf_backup ##先备份原始配置
-
vi /etc/modprobe.d/disable_ipv6.conf
-
- 修改前
-
alias net-pf-10 off
-
options ipv6 disable=1
-
- 修改后
-
alias net-pf-10 off
-
options ipv6 disable=0
-
1.2 修改 /etc/sysconfig/network
##修改 NETWORKING_IPV6 为 yes
-
cp /etc/sysconfig/network /etc/sysconfig/network_backup
-
vi /etc/sysconfig/network
-
- 修改前
-
PEERNTP=no
-
NETWORKING_IPV6=no
-
GATEWAY=139.255.255.0
-
- 修改后
-
PEERNTP=no
-
NETWORKING_IPV6=yes
-
GATEWAY=139.255.255.0
-
1.3 修改 /etc/sysconfig/network-scripts/ifcfg-eth0
## 添加 IPV6INIT 为 yes 和 IPV6_AUTOCONF 为 yes
-
cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0_backup
-
vi /etc/sysconfig/network-scripts/ifcfg-eth0
-
- 修改前
-
DEVICE=eth0
-
ONBOOT=yes
-
BOOTPROTO=static
-
IPADDR=10.10.10.1
-
NETMASK=255.255.254.0
-
- 修改后
-
DEVICE=eth0
-
ONBOOT=yes
-
BOOTPROTO=static
-
IPADDR=10.10.10.1
-
NETMASK=255.255.254.0
-
IPV6INIT=yes
-
IPV6_AUTOCONF=yes
-
1.4 修改 /etc/sysctl.conf
## 修改 net.ipv6.conf.all.disable_ipv6 为 0, net.ipv6.conf.default.disable_ipv6 为 0 和 net.ipv6.conf.lo.disable_ipv6 为 0
-
cp /etc/sysctl.conf /etc/sysctl.conf_backup
-
vi /etc/sysctl.conf
-
- 修改前
-
vm.swappiness = 0
-
net.ipv4.neigh.default.gc_stale_time=120
-
net.ipv4.conf.all.rp_filter=0
-
net.ipv4.conf.default.rp_filter=0
-
net.ipv4.conf.default.arp_announce = 2
-
net.ipv4.conf.all.arp_announce=2
-
net.ipv4.tcp_max_tw_buckets = 5000
-
net.ipv4.tcp_syncookies = 1
-
net.ipv4.tcp_max_syn_backlog = 1024
-
net.ipv4.tcp_synack_retries = 2
-
net.ipv6.conf.all.disable_ipv6 = 1
-
net.ipv6.conf.default.disable_ipv6 = 1
-
net.ipv6.conf.lo.disable_ipv6 = 1
-
net.ipv4.conf.lo.arp_announce=2
-
- 修改后
-
vm.swappiness = 0
-
net.ipv4.neigh.default.gc_stale_time=120
-
net.ipv4.conf.all.rp_filter=0
-
net.ipv4.conf.default.rp_filter=0
-
net.ipv4.conf.default.arp_announce = 2
-
net.ipv4.conf.all.arp_announce=2
-
net.ipv4.tcp_max_tw_buckets = 5000
-
net.ipv4.tcp_syncookies = 1
-
net.ipv4.tcp_max_syn_backlog = 1024
-
net.ipv4.tcp_synack_retries = 2
-
net.ipv6.conf.all.disable_ipv6 = 0
-
net.ipv6.conf.default.disable_ipv6 = 0
-
net.ipv6.conf.lo.disable_ipv6 = 0
-
net.ipv4.conf.lo.arp_announce=2
-
1.5 创建系统在启动时自动加载 IPv6 模块的脚本
## 创建脚本文件 ipv6.modules
vi /etc/sysconfig/modules/ipv6.modules
- 脚本内容
-
!/bin/sh
-
if [ ! -c /proc/net/if_inet6 ] ; then
-
exec /sbin/insmod /lib/modules/uname -r/kernel/net/ipv6/ipv6.ko
-
fi
-
1.6 重启系统,加载 IPv6 模块
- 查看 IPv6 模块
-
ifconfig | grep -i inet6 #### 查看ipv6的信息,有看到输出就可以
-
inet6 addr: fe80::x:x:x:x/64 Scope:Link
-
inet6 addr: fe80::x:x:x:x/64 Scope:Link
-
inet6 addr: x:x:x:x::2/64 Scope:Global
-
inet6 addr: fe80::x:x/128 Scope:Link
-
inet6 addr: ::1/128 Scope:Host
-
2、申请 IPv6 地址
2.1 在 tunnelbroker.net 上申请一个免费的 IPv6 地址
- 访问 tunnelbroker.net 注册一个自己的账号。
- 选择 Create Regular Tunnel 创建一个到自己公网 IP 的通道。(经本人测试,Tunnel Servers 可用 Tokyo, JP,Los Angeles, CA, US 可以配置,但实际网络环境不可用)
- 找到 Example Configurations,选择操作系统对应的脚本(CentOS 7.x 选择 Linux-net-tools),复制下来,在 linux 的控制台中执行脚本。
-
ifconfig sit0 up
-
ifconfig sit0 inet6 tunnel ::66.220.15.43
-
ifconfig sit1 up
-
ifconfig sit1 inet6 add 2001:4e0:d:e60::2/64
-
route -A inet6 add ::/0 dev sit1
-
- ping 一下服务器的 IPv6 地址,看看是否工作正常(CentOS 上 IPv6 版的 ping 名为 ping6),如果通了就说明可以了:
-
ping6 2001:4e0:d:e60::2
-
PING 2001:4e0:d:e60::2(2001:4e0:d:e60::2) 56 data bytes
-
64 bytes from 2001:4e0:d:e60::2: icmp_seq=1 ttl=64 time=340 ms
-
64 bytes from 2001:4e0:d:e60::2: icmp_seq=2 ttl=64 time=348 ms
-
64 bytes from 2001:4e0:d:e60::2: icmp_seq=3 ttl=64 time=349 ms
-
2.2 修改 /etc/init.d/network 网卡的启动脚本, 系统重启时自动配置 IPv6 tunnel
- 编辑脚本
-
cp /etc/init.d/network /etc/init.d/network_backup
-
vi /etc/init.d/network
-
- 在 touch /var/lock/subsys/network 之前添加以下脚本
-
# 添加 IPv6 脚本支持
-
ifconfig sit0 up
-
ifconfig sit0 inet6 tunnel ::66.220.15.43
-
ifconfig sit1 up
-
ifconfig sit1 inet6 add 2001:4e0:d:e60::2/64
-
route -A inet6 add ::/0 dev sit1
-
#修改结束
现在每次重启机器都可以自动分配好 IPv6 地址。
-
3、使用 AAAA 记录设置 IPv6 域名解析
- 添加一条 AAAA 记录值,填写 IPv6 地址即可
A. 记录类型:选 AAAA
B. 主机记录:填写子域名。若要将域名解析为 www.example.com,在主机记录填写 www;若要将域名解析为 example.com(不带www),在主机记录填写 @ 或者不填写。
C. 解析线路:若您未设置特定解析线路,则所有线路用户均访问该目标地址;若设置了特定解析线路(例如:联通),则特定线路用户访问特定目标地址,其他线路用户仍然访问该(默认)目标地址。
D. 记录值:为 IP 地址,且 AAAA 记录值只可以填写 IPv6 地址。
E. TTL:默认(10 分钟)即可。
4、配置 nginx 支持并监听 IPv6 的 80 端口
- 首先查看当前的 nginx 是否支持IPv6协议,使用命令:
nginx -V
- 若输出的 configure arguments 中不包含 —-with-ipv6,则需要重新编译安装 nginx,编译 nginx 时需要加上 —-with-ipv6。
- 修改 nginx.conf 中的 server 段,除了监听 IPv4 的 80 端口外还需要监听 IPv6 的 80 端口,若有需要还需要监听 HTTPS 协议的 IPv6 的 443 端口:
-
server {
-
listen 80; // 监听 IPv4 的 80 端口, HTTP 协议
-
listen [::]:80; // 监听 IPv6 的 80 端口, HTTP 协议
-
server_name example.com;
-
……
-
}
-
-
server {
-
listen 443; // 监听 IPv4 的 443 端口, HTTPS 协议
-
listen [::]:443; // 监听 IPv6 的 443 端口, HTTPS 协议
-
……
-
}
5、可使用以下网址测试服务器对 IPv6 的支持情况