红联Linux门户
Linux帮助

Linux内核关闭IPv6协议的方式

发布时间:2015-02-20 10:41:17来源:linux网站作者:frankzfz

在Linux禁用IPv6可以使用下面的几种方式:


第一种方式:

在/etc/modprobe.d/dist.conf文件中添加install ipv6 /bin/true,在reboot后使用使用lsmod | grep ipv6查看,IPv6模块没有被加载,在/proc/sys/net目录下也已经没有了ipv6的目录文件。

[root@root net]# ls

core  ipv4  netfilter  unix


第二种方式:

在/boot/grub/grub.conf文件中,在启动的Linux内核版本中传递下面的参数ipv6.disable=1,该效果和方式一基本类似,都需要重新启动,但是在启动完成后,使用lsmod还是可以参看到ipv6模块信息,但引用ipv6模块数为0. 在/proc/sys/net目录下也没有了ipv6的目录文件。

[root@root~]# lsmod | grep ipv6

ipv6                  331149  0

上面这种方式其实是根据IPv6模块的三个参数进行的,通过modinfo可以看到,IPv6模块支持三个参数,

modinfo ipv6

filename:      /lib/modules/2.6.32/kernel/net/ipv6/ipv6.ko

alias:          net-pf-10

license:        GPL

description:    IPv6 protocol stack for Linux

author:        Cast of dozens

srcversion:    AA5735202A5094F448BF9AE

depends:      

vermagic:      2.6.32 SMP mod_unload modversions

parm:          disable:Disable IPv6 module such that it is non-functional (int)

parm:          disable_ipv6:Disable IPv6 on all interfaces (int)

parm:          autoconf:Enable IPv6 address autoconfiguration on all interfaces (int)

在Linux内核的文档中我们可以看到对这个三个参数的解释:

disable

Specifies whether to load the IPv6 module, but disable all

its functionality.  This might be used when another module

has a dependency on the IPv6 module being loaded, but no

IPv6 addresses or operations are desired.

The possible values and their effects are:

0 IPv6 is enabled.

This is the default value.

1 IPv6 is disabled.

No IPv6 addresses will be added to interfaces, and

it will not be possible to open an IPv6 socket.

A reboot is required to enable IPv6.

autoconf

Specifies whether to enable IPv6 address autoconfiguration

on all interfaces.  This might be used when one does not wish

for addresses to be automatically generated from prefixes

received in Router Advertisements.

The possible values and their effects are:

0 IPv6 address autoconfiguration is disabled on all interfaces.

Only the IPv6 loopback address (::1) and link-local addresses

will be added to interfaces.

1 IPv6 address autoconfiguration is enabled on all interfaces.

This is the default value.

disable_ipv6

Specifies whether to disable IPv6 on all interfaces.

This might be used when no IPv6 addresses are desired.

The possible values and their effects are:

0 IPv6 is enabled on all interfaces.

This is the default value.

1 IPv6 is disabled on all interfaces.

No IPv6 addresses will be added to interfaces.

在grub.conf中还可以使用ipv6.disable_ipv6=1禁止IPv6协议,和ipv6.disable不同的是对IPv6模块的引用不为零。

lsmod | grep ipv6

ipv6                  331934  30

使用echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6 命令可以把IPv6功能重新打开,

使用echo 0 > /sys/module/ipv6/parameters/disable_ipv6命令无法重新打开,这也是这两个控制IPv6协议开关的不同之处。即使在grub.conf文件中不添加ipv6的任何信息,向/sys/module/ipv6/parameters/disable_ipv6文件中写入也不能控制IPv6协议,建议使用proc目录下的变量控制。


第三种方式:

在/proc/sys/net/ipv6/conf/目录下有下面的目录:

[root@root conf]# ls

all  default  eth0  gre0  lo

可以针对不同的接口禁止,如果是针对所有的接口,可以使用下面的命令,该命令会直接把接口上的IPv6地址给删掉,包括本地链路地址fe80::,

IPv6, net.ipv6.conf.all.disable_ipv6 = 1

net.ipv6.conf.default.disable_ipv6 = 1

下面是Linux内核对该参数的解释:

disable_ipv6 - BOOLEAN Disable IPv6 operation.  If accept_dad is set to 2, this value

will be dynamically set to TRUE if DAD fails for the link-local

address.

Default: FALSE (enable IPv6 operation)

When this value is changed from 1 to 0 (IPv6 is being enabled),

it will dynamically create a link-local address on the given

interface and start Duplicate Address Detection, if necessary.

When this value is changed from 0 to 1 (IPv6 is being disabled),

it will dynamically delete all address on the given interface
Linux内核关闭IPv6协议的方式

以上图片上传到红联Linux系统教程频道中。


附录:模块参数的定义

module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);

MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces")

只在addrconf_init_net函数中使用了IPv6模块参数,所以IPv6模块的disable_ipv6参数只有在初始化时进行了赋值,系统启动后的修改无法改变原先的配置。

static int addrconf_init_net(struct net *net)

{

int err;

struct ipv6_devconf *all, *dflt;

err = -ENOMEM;

all = &ipv6_devconf;

dflt = &ipv6_devconf_dflt;

if (net != &init_net) {

all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);

if (all == NULL)

goto err_alloc_all;

dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);

if (dflt == NULL)

goto err_alloc_dflt;

} else {

/* these will be inherited by all namespaces */

dflt->autoconf = ipv6_defaults.autoconf;

dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;

}

net->ipv6.devconf_all = all;

net->ipv6.devconf_dflt = dflt;

#ifdef CONFIG_SYSCTL

err = __addrconf_sysctl_register(net, "all", NET_PROTO_CONF_ALL,NULL, all);

if (err < 0)

goto err_reg_all;

err = __addrconf_sysctl_register(net, "default", NET_PROTO_CONF_DEFAULT,NULL, dflt);

if (err < 0)

goto err_reg_dflt;

#endif

return 0;

#ifdef CONFIG_SYSCTL

err_reg_dflt:

__addrconf_sysctl_unregister(all);

err_reg_all:

kfree(dflt);

#endif

err_alloc_dflt:

kfree(all);

err_alloc_all:

return err;

}