This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

Internet Protocol version 6 (IPv6) is the most recent version of the Internet Protocol (IP), the communications protocol that provides an identification and location system for computers on networks and routes traffic across the Internet. IPv6 was developed by the Internet Engineering Task Force (IETF) to deal with the long-anticipated problem of IPv4 address exhaustion. IPv6 is intended to replace IPv4. In most cases IPv6 is enabled by default on Ubuntu Operating Systems

In this tutorial we would be focusing on how to disable IPv6 on Ubuntu Systems

Check If IPv6 Is Already Enabled

To check for IPv6 existence open up a terminal and type in the following command.

root@codesposts:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 56:00:02:23:c5:49 brd ff:ff:ff:ff:ff:ff
    inet 149.28.57.113/23 brd 149.28.57.255 scope global dynamic ens3
       valid_lft 76682sec preferred_lft 76682sec
    inet6 fe80::5400:2ff:fe23:c549/64 scope link
       valid_lft forever preferred_lft forever
3: vboxnet0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq state DOWN group default qlen 1000
    link/ether 0a:00:27:00:00:00 brd ff:ff:ff:ff:ff:ff
    inet 192.168.99.1/24 brd 192.168.99.255 scope global vboxnet0
       valid_lft forever preferred_lft forever
    inet6 fe80::800:27ff:fe00:0/64 scope link
       valid_lft forever preferred_lft forever

As you can see from the second last line inet6 fe80::800:27ff:fe00:0/64 scope link , IPv6 is enabled.

Disabling IPv6

Disable Until Next Reboot

The simplest way to instantly disable the IP version 6 network protocol system on on Ubuntu 18.04 is to execute the following commands:

root@codesposts:~# sysctl -w net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.all.disable_ipv6 = 1
root@codesposts:~# sysctl -w net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6 = 1
root@codesposts:~# sysctl -w net.ipv6.conf.lo.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6 = 1

Check again using ip a command for IPv6 connectivity

root@codesposts:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 56:00:02:23:c5:49 brd ff:ff:ff:ff:ff:ff
    inet 149.28.57.113/23 brd 149.28.57.255 scope global dynamic ens3
       valid_lft 75294sec preferred_lft 75294sec
3: vboxnet0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq state DOWN group default qlen 1000
    link/ether 0a:00:27:00:00:00 brd ff:ff:ff:ff:ff:ff
    inet 192.168.99.1/24 brd 192.168.99.255 scope global vboxnet0
       valid_lft forever preferred_lft forever

As you can see, the line starting with inet6 has vanished, thus confirming that IPv6 has been successfully disabled. However, these commands disable the IPv6 connectivity only for current session. It will be re-enabled after boot.

Disable Permanently Using sysctl.conf

To make this consistent and permanently disable IPv6, one of the widely used methods are modifying /etc/sysctl.conf Open the file using nano or any other editor you wish to use and add the last three lines as shown below.

nano /etc/sysctl.conf
/etc/sysctl.conf
...
###################################################################
# Protected links
#
# Protects against creating or following links under certain conditions
# Debian kernels have both set to 1 (restricted)
# See https://www.kernel.org/doc/Documentation/sysctl/fs.txt
#fs.protected_hardlinks=0
#fs.protected_symlinks=0

# Accept IPv6 advertisements when forwarding is enabled
net.ipv6.conf.all.accept_ra = 2

net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr

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

Save and close the file and then type the following command for the settings to take effect.

root@codesposts:~# sysctl -p
net.ipv6.conf.all.accept_ra = 2
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1

Optionally Reboot and check for IPv6 connectivity. If the connectivity still exists, create a file /etc/rc.local using commands shown below:

touch /etc/rc.local

Open the file using an editor such as nano and vim. Fill the file as shown.

/etc/rc.local
#!/bin/bash
# /etc/rc.local

/etc/sysctl.d
/etc/init.d/procps restart

exit 0

Use the following command to make the file executable:

chmod 755 /etc/rc.local

Disable Permanently Using GRUB

An alternative method is to disable IPv6 connectivity is to configure GRUB to pass kernel parameters at boot time. You’ll have to edit /etc/default/grub. Open the file using an editor and edit the last two lines so that it looks similar to the file shown below

/etc/default/grub
GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=2
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`

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

To apply changes made to the GRUB boot loader configuration, use:

update-grub

Re-Enabling IPv6

To re-enable IPv6, you have to undo all the changes made to files.

Re-Enable Until Next Reboot

To Re-enable until current reboot enter the following command:

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

Permanently Re-Enable Using sysctl.conf

If you had modified the /etc/sysctl.conf, open it again and delete the lines added previously.

/etc/sysctl.conf
...
###################################################################
# Protected links
#
# Protects against creating or following links under certain conditions
# Debian kernels have both set to 1 (restricted)
# See https://www.kernel.org/doc/Documentation/sysctl/fs.txt
#fs.protected_hardlinks=0
#fs.protected_symlinks=0

# Accept IPv6 advertisements when forwarding is enabled
net.ipv6.conf.all.accept_ra = 2

net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr

Reload the sysctl.conf file using the following command to make the changes effective:

sysctl -p

If you had created the /etc/rc.local file, remove it using

rm /etc/rc.local

Permanently Re-Enable Using GRUB

Lastly, if you have used the GRUB method for IPv6 disabling, open the file /etc/default/grub and restore it back to following configuration

/etc/default/grub
GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=2
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX="consoleblank=0"

To apply changes made to the GRUB boot loader configuration, use:

update-grub