How to disable IPv6 in Linux system to solve the problem of connection timeout

Time:2022-6-11

IPv6 and IPv4 belong to the same network protocol. In the Linux system, the connection timeout may occur when IPv6 and IPv4 exist at the same time. At this time, it is only necessary to disable IPv6 to solve this problem. The following small series will introduce the method of disabling IPv6 in Linux.

IPv6 is considered as a substitute for IPv4, the traditional 32-bit address space on the Internet. It is used to solve the problem that the existing IPv4 address space is about to be exhausted. However, since a large number of hosts and devices have been connected to the Internet with IPv4, it is almost impossible to switch them all to IPv6 overnight. Many IPv4 to IPv6 conversion mechanisms (such as dual protocol stack, network tunnel, proxy) have been proposed to promote IPv6 adoption, and many applications are being rewritten to increase IPv6 support, as we advocate. One thing is certain: IPv4 and IPv6 are bound to coexist in the foreseeable future.

Ideally, the process of transition to IPv6 should not be seen by end users, but ipv4/ipv6 mixed environment sometimes makes you encounter various problems caused by the inadvertent collision between IPv4 and IPv6. For example, you may encounter the problem of application timeout, such as apt get or SSH attempts to connect through IPv6 fail, DNS server accidentally empties the AAAA record of IPv6, or your IPv6 enabled device is incompatible with the IPv4 network left by your Internet service provider, etc.

Of course, this does not mean that you should blindly disable IPv6 on your Linux machine. In view of the various benefits promised by IPv6, as a member of society, we should finally fully embrace it. However, as part of the troubleshooting process for end users, if IPv6 is indeed the culprit, you can try to turn it off.

Here are some tips that let you disable IPv6 partially (for example, for a specific network interface) or completely in Linux. These tips should apply to all major Linux distributions including Ubuntu, Debian, Linux mint, CentOS, Fedora, RHEL, and arch Linux.

Check whether IPv6 is enabled in Linux

IPv6 is automatically enabled by default for all modern Linux distributions. To see whether IPv6 is activated in your Linux, you can use ifconfig or IP commands. If you see the output of “inet6” after entering these commands, it means that your Linux system has IPv6 enabled.

  $ ifconfig

Linux系统如何禁用IPv6

  $ ip addr

Linux系统如何禁用IPv6

Temporarily disable IPv6

If you want to temporarily turn off IPv6 on your Linux system, you can use /proc file system. “Temporary” means that our changes to disable IPv6 will not be saved after the system restarts. IPv6 will be enabled again after your Linux machine restarts.

To disable IPv6 for a specific network interface, use the following command:

  $ sudo sh -c ‘echo 1 》 /proc/sys/net/ipv6/conf/《interface-name》/disable_ipv6’

For example, disable IPv6 for eth0 interface:

  $ sudo sh -c ‘echo 1 》 /proc/sys/net/ipv6/conf/eth0/disable_ipv6’

Linux系统如何禁用IPv6

Re enable IPv6 for eth0 interface:

  $ sudo sh -c ‘echo 0 》 /proc/sys/net/ipv6/conf/eth0/disable_ipv6’

If you want to disable IPv6 for all interfaces of the whole system, including the loopback interface, use the following command:

  $ sudo sh -c ‘echo 1 》 /proc/sys/net/ipv6/conf/all/disable_ipv6’

Permanently disable IPv6

The above methods cannot permanently disable IPv6. Once you restart the system, IPv6 will still be enabled. If you want to permanently close it, there are several ways you can try.

Method 1

The first method is through /etc/sysctl The conf file permanently modifies /proc.

In other words, open /etc/sysctl Conf then add the following:

#Disable IPv6 for all interfaces of the entire system

  net.ipv6.conf.all.disable_ipv6 = 1

#Disable IPv6 for a specified interface (for example: eth0, Lo)

  net.ipv6.conf.lo.disable_ipv6 = 1

  net.ipv6.conf.eth0.disable_ipv6 = 1

At /etc/sysctl Conf to make these changes effective, run the following command:

  $ sudo sysctl -p /etc/sysctl.conf

Or restart directly.

Method II

Another way to permanently disable IPv6 is to pass a necessary kernel parameter at boot time.

Open /etc/default/grub with a text editor and add “ipv6.disable=1” to the grubcmdlinelinux variable.

  $ sudo vi /etc/default/grub

  GRUB_CMDLINE_LINUX=“xxxxx ipv6.disable=1”

The above “xxxxx” represents any existing kernel parameters. Add “ipv6.disable=1” after it.

Linux系统如何禁用IPv6

Finally, don’t forget to save your changes to grub/grub2 in the following ways:

Debian, Ubuntu, or Linux Mint systems:

  $ sudo update-grub

Fedora, centos/rhel systems:

  $ sudo grub2-mkconfig -o /boot/grub2/grub.cfg

Now as long as you restart your Linux system, IPv6 will be completely disabled.

Additional optional steps after disabling IPv6

Here are some optional steps to consider after you disable IPv6. This is because when you disable IPv6 in the kernel, other programs may still try to use IPv6. In most cases, this behavior of applications is unlikely to affect anything, but for efficiency or security reasons, you can disable IPv6 for them.

  /etc/hosts

According to your settings, /etc/hosts will contain one or more IPv6 hosts and their addresses. Open /etc/hosts with a text editor and comment out the script line containing IPv6 hosts.

  $ sudo vi /etc/hosts

  # comment these IPv6 hosts# ::1 ip6-localhost ip6-loopback# fe00::0 ip6-localnet# ff00::0 ip6-mcastprefix# ff02::1 ip6-allnodes# ff02::2 ip6-allrouters

  Network Manager

If you are using NetworkManager to manage your network settings, you can disable IPv6 in NetworkManager. Open the wired connection in NetworkManager, click the “IPv6 settings” option, select “ignore” in the “method” column, save and exit.

Linux系统如何禁用IPv6

SSH service

By default, openssh service (sshd) will try to bundle IPv4 and IPv6 addresses.

To force sshd to bind only IPv4 addresses, open /etc/ssh/sshd with a text editor_ Config and add the following line. INET is only applicable to IPv4, while inet6 is applicable to IPv6.

  $ sudo vi /etc/ssh/sshd_config

  AddressFamily inet

Then restart the sshd service.

The above is how to disable IPv6 in Linux. You can choose to temporarily disable IPv6 or permanently disable IPv6. IPv6 will be enabled after the system is restarted. However, permanently disabling IPv6 will not cause this problem.

Recommended Today

JS generate guid method

JS generate guid method https://blog.csdn.net/Alive_tree/article/details/87942348 Globally unique identification(GUID) is an algorithm generatedBinaryCount Reg128 bitsNumber ofidentifier , GUID is mainly used in networks or systems with multiple nodes and computers. Ideally, any computational geometry computer cluster will not generate two identical guids, and the total number of guids is2^128In theory, it is difficult to make two […]