Disable and enable IPv6 on Linux
Disable IPv6 at runtime through /proc for one or all interfaces, and how IPv4-mapped listening behaves.
IPv6 can be turned off or back on for each network interface at runtime, through the disable_ipv6 entries under /proc/sys/net/ipv6/conf/. A change made this way does not survive a reboot (see Notes).
Disable or enable IPv6 on one interface
sudo sh -c 'echo 1 > /proc/sys/net/ipv6/conf/enp0s3/disable_ipv6' # disable
sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/enp0s3/disable_ipv6' # enable
The sh -c wrapper is required: a plain sudo echo 1 > file performs the redirection in the calling shell, which is not root.
Disable IPv6 on all interfaces
sudo sh -c 'echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6'
| Path | Scope |
|---|---|
conf/all/disable_ipv6 |
every interface |
conf/<interface>/disable_ipv6 |
one interface (for example enp0s3 or eth0) |
How sockets use IPv6 (ipv6(7))
- An IPv6 socket can accept IPv4 connections through IPv4-mapped IPv6 addresses (
::ffff:a.b.c.d). - Programs do not need to handle these addresses: the C library converts IPv4 addresses transparently, so a program written for the IPv6 API supports both protocols.
- The
IPV6_V6ONLYsocket option defaults to the value of/proc/sys/net/ipv6/bindv6only. With0, an IPv6 listener also accepts IPv4 connections; with1, it accepts IPv6 only.
Notes
- Values written under
/procare lost at reboot. To make the setting permanent, use a sysctl drop-in file (for example/etc/sysctl.d/99-ipv6.confcontainingnet.ipv6.conf.all.disable_ipv6 = 1) and load it withsudo sysctl --system. - Disabling IPv6 on a host whose services or applications use IPv6 addresses can break them.