Nov 01 2016

How to Disable IPv6 in Ubuntu 16.04 Xenial Xerus

Internet Protocol version 6 (IPv6)

In the course of troubleshooting a pseudo-distributed mode installation of Apache Hadoop on my Ubuntu 16.04 vm, one of the tips I stumbled across suggested disabling IPv6.

For any networking neophytes, Internet Protocol version 6, aka IPv6, is the most recent version of the Internet Protocol (IP), the system governing the assignment of addresses to networks and computers on the Internet. IPv6 was created to replace IPv4 which, why still predominantly used by most systems, is quickly running out available addresses. Unlike IPv4, which is a 32 bit addressing scheme offering 2^{32} or roughly 4.3 billion addresses, IPv6 addresses are 128 bits, meaning that there are theoretically 2^{128} or roughly 3.4 \times 10^{38} addresses.

While IPv6 provides more than enough addresses to allow the Internet to continue to grow at its breakneck pace, the two protocols, IPv6 and IPv4, aren’t interoperable, and sometimes conflicts arise when both are active on the same computer. I’m honestly unsure yet whether or not IPv6 is causing my difficulties with Hadoop, but since I am not connected to an IPv6 network, there is not any reason for me to not disable it entirely.

UPDATE 2017-02-02: For a better solution than mine below, see Dave Bevan’s comment.

The steps to follow in order to disable IPv6 in Ubuntu 16.04 are:

  1. First check to see if IPv6 is already disabled. To do so, open up the Terminal, and at the command line enter: /proc/sys/net/ipv6/conf/all/disable_ipv6. If the return value is 1, then IPv6 is already disabled, and you are done. A return value of 0 indicates IPv6 is active, and you need to continue on to Step 2.

    Disable IPv6
  2. Open the file /etc/sysctl.conf in your text editor of choice. I chose to edit the file using Bluefish, but you can use vi, gedit, emacs, etc., but because the file is only writable by the root user (it should have 644 permissions), you will need to have superuser access in order to save your changes.

    Launching Bluefish as a superuser
  3. At the end of sysctl.conf, add the following three lines:
    net.ipv6.conf.all.disable_ipv6 = 1
    net.ipv6.conf.default.disable_ipv6 = 1
    net.ipv6.conf.lo.disable_ipv6 = 1

    sysctl.conf

  4. Now run sudo sysctl -p to update to reconfigure the kernel parameters with the new values set in Step 3. The output of sysctl will be the parameters that have changed, so unless you made other edits to sysctl.conf, the output will simply be the lines you added above.

    sysctl -p
  5. If you run /proc/sys/net/ipv6/conf/all/disable_ipv6 now, it should now return 1 indicating that IPv6 has been disabled.

    Disable IPv6

16 comments

Skip to comment form

    • nick on December 20, 2016 at 3:21 pm
    • Reply

    typo, it will say 1 to confirm that ipv6 is disabled in the final step (not 0)

      • Tony on December 20, 2016 at 5:37 pm
        Author
      • Reply

      Good catch, Nick. I’ve updated the post. Thanks for bringing it to my attention.

    • Anderson on December 30, 2016 at 5:53 pm
    • Reply

    This never “sticks” for me. All of the above works but at reboot it reverts back tto IPV6. The config is there but I have to type ‘sudo sysctl -p’ again in the terminal for it to pick up the config. Any suggestions?

      • Tony on January 1, 2017 at 12:36 pm
        Author
      • Reply

      Have you tried this?

      http://askubuntu.com/a/444464

      In short, you may need to edit /usr/lib/pm-utils/power.d/laptop-mode. In the laptop_mode_ac() function, comment out the line:

      Reboot and hopefully sysctl now picks up your changes.

  1. The more formal way of doing this, which fits in with the don’t-change-a-core-system-file-otherwise-it-could-be-replaced-when-you-upgrade-packages-and-your-changes-could-be-lost methodology, and such that it continues across a reboot, is as follows:


    # create the long-life config file
    echo "net.ipv6.conf.all.disable_ipv6 = 1
    net.ipv6.conf.default.disable_ipv6 = 1
    net.ipv6.conf.lo.disable_ipv6 = 1" | sudo tee /etc/sysctl.d/99-my-disable-ipv6.conf

    # ask the system to use it
    sudo service procps reload

    # check the result
    cat /proc/sys/net/ipv6/conf/all/disable_ipv6

      • Tony on February 2, 2017 at 9:46 am
        Author
      • Reply

      Thanks for sharing, Dave! I’ve linked to this comment from the body of the post as this is definitely a safer way to do it.

      • Donno on July 25, 2017 at 4:37 am
      • Reply

      sudo service procps reload

      sysctl: permission denied on key ‘kernel.kptr_restrict’
      sysctl: permission denied on key ‘kernel.sysrq’

      Any Help

    • Gary Mercer on February 21, 2017 at 6:15 pm
    • Reply

    Daves’ suggestion doesn’t work. Did he leave out something important like is this a file or does it belong in a file… Three tries at it and i finally figures out that all that stuff went on one line. Maybe using a line indicator other than the # would be better.

      • Gary Mercer on February 21, 2017 at 6:56 pm
      • Reply

      All i get is no such file or directory.

        • Dave Bevan on February 22, 2017 at 4:22 pm
        • Reply

        Hi Gary.

        You need to copy/paste the three blocks into a terminal window. You can either do it all at once, or one section at a time.

        When you paste the 1st section it will ask for your password.

        Rgds,
        –dave.

    • Andi on February 22, 2017 at 10:34 am
    • Reply

    Came accross this because internal machine tried to contact ubuntu repo servers via ipv6 when using apt.
    Used Dave Bevan’s suggestion and it worked out of the box.
    No reboot was needed. Simply put the lines above into one script file and run the file.

    After this ifconfig did not show any ipv6 address on the interface anymore. Apt worked now using solely ipv4.

    Cheers.

  2. Hii Buddy 🙂 You’re article is awesome. But this method is not working with Ubuntu 17.04. I found this article useful and works well with ubuntu 17.04 How to Disable IPv6 on Ubuntu 17.04

    • james on July 7, 2017 at 4:01 am
    • Reply

    This really helped me thanks for the “How To Guide”

    • bastos on August 19, 2017 at 10:34 am
    • Reply

    Hi, Thx for the post it’s very useful!
    There is just a typo on the line : “At the end of sysctl.conf, at the following three lines:” (I believe it should be “add” instead of “at”) 🙂

      • Tony on August 19, 2017 at 11:59 am
        Author
      • Reply

      Good catch! Thanks for letting me know. I’ve corrected the wording.

  3. this is not working on ubuntu 17.10

Leave a Reply