Enable/Disable IPv6 on Ubuntu

How do you disable or enable IPv6 on your machine?

Check if IPv6 is enabled

Use the following command to list the interfaces and their IP. Check if there is an inet6 IP in the list

$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 48:b0:2d:3c:c0:99 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.11/24 brd 192.168.1.255 scope global dynamic noprefixroute eth0
       valid_lft 66709sec preferred_lft 66709sec
    inet6 <IPv6 IP> scope link noprefixroute 
       valid_lft forever preferred_lft forever

Notice the inet6 entry in the output.

If there IPv6 is not enabled:

$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 48:b0:2d:3c:c0:99 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.11/24 brd 192.168.1.255 scope global dynamic noprefixroute eth0
       valid_lft 66709sec preferred_lft 66709sec

Notice that there are no inet6 entries anymore.

Disable IPv6

Using sysctl

To disable IPv6 you need the following 3 commands:

sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=1

However, this only temporarily disables IPv6. The next time your system boots, IPv6 will be enabled again. To persist the changes you need to edit the /etc/sysctl.conf file and add the following lines to it:

net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1

Apply them using the command:

sudo sysctl -p

Disable IPv6 using GRUB

This is a more permanent way to disable IPv6 and the way I recommend. You’ll have to edit the /etc/default/grub file. Modify the following lines:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""

to

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash ipv6.disable=1"
GRUB_CMDLINE_LINUX="ipv6.disable=1"

Save the file and update grub by running:

sudo update-grub

and reboot the machine

Enable IPv6

If using sysctl

To enable IPv6 you need the following 3 commands:

sudo sysctl -w net.ipv6.conf.all.disable_ipv6=0
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=0
sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=0

If they are persisted in the /etc/sysctl.conf file, you can remove the lines added in the above section or add the following lines:

net.ipv6.conf.all.disable_ipv6=0
net.ipv6.conf.default.disable_ipv6=0
net.ipv6.conf.lo.disable_ipv6=0

Apply the changes using the command:

sudo sysctl -p

If using GRUB

Revert the changes in the above section, update GRUB as mentioned in the above section and reboot the machine.