Internet protocol version 6 (IPv6) is released long time ago, But still most of organisation are using ipv4 only.In Ubuntu 20.04, By default ipv6 is enabled in Ubuntu 20.04, which is show in various command output, like below. In this post we will wee how we can disable Ipv6.

root@jarvis:~# pifconfig wlp2s0
wlp2s0
	HWaddr 5c:e0:c5:1a:01:cb
	inet addr:192.168.0.3
	Bcast:192.168.0.255
	Mask:255.255.255.0
*	inet6 addr: 2a02:c7f:c61f:5300:d029:1c9e:c150:1689/64 Scope: global
*	inet6 addr: 2a02:c7f:c61f:5300:d560:7787:3407:ce11/64 Scope: global
*	inet6 addr: fdec:aea1:e763:0:d029:1c9e:c150:1689/64 Scope: global
*	inet6 addr: fdec:aea1:e763:0:2fce:6e12:2397:f1ac/64 Scope: global
*	inet6 addr: fe80::be73:c03f:796:2ff9/64 Scope: link
	UP BROADCAST RUNNING MULTICAST

SetUp

This machine is Ubuntu 20.04 updated as of now July 2020.

root@jarvis:~# lsb_release -d
Description:	Ubuntu 20.04 LTS
root@jarvis:~# uname -r
5.4.0-42-generic

How to Disable IPv6

On Ubuntu 20.04 ipv6 is enabled by-default but most of time organisation only using ipv4. We can disable ipv6 through mainly two ways

  • Disable kernel module
  • via sysctl settings

Disable ipv6 built-in-kernel module

TO disable ipv6 on Ubuntu 20.04 , we need to edit /etc/default/grub and append GRUB_CMDLINE_LINUX with ipv6.disable=1 like below. This method will need reboot

GRUB_CMDLINE_LINUX="crashkernel=auto ... ipv6.disable=1"

After changing any kernel parameter we rebuild grub.cfg through update-grub2

[root@jarvis ~]#update-grub2
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.4.0-42-generic
Found initrd image: /boot/initrd.img-5.4.0-42-generic
Found linux image: /boot/vmlinuz-5.4.0-40-generic
Found initrd image: /boot/initrd.img-5.4.0-40-generic
Adding boot menu entry for UEFI Firmware Settings
done

Further reboot Machine will disable ipv6 on all interface. As it need reboot, so i don’t suggest to use this method, instead we have another way which don’t need reboot.

Disable through sysctl settings

We can also disable through sysctl settings, which will apply without reboot. For this we need to edit /etc/sysctl.conf and append some lines to it.

# Disable ipv6 on all interfaces
net.ipv6.conf.all.disable_ipv6 = 1

# Disable ipv6 on specific interface only
net.ipv6.conf.ens2.disable_ipv6 = 1

To apply these settings, we need to run sysctl -p.

root@jarvis:~# awk 'END{print}' /etc/sysctl.conf 
net.ipv6.conf.all.disable_ipv6 = 1
root@jarvis:~# sysctl -p
net.ipv6.conf.all.disable_ipv6 = 1

We can also check it like below command

root@jarvis:~# pifconfig wlp2s0
wlp2s0
	HWaddr 5c:e0:c5:1a:01:cb
	inet addr:192.168.0.3
	Bcast:192.168.0.255
	Mask:255.255.255.0
	UP BROADCAST RUNNING MULTICAST

Above two methods are persistent across reboots and will not change after reboot. But we have another way which is not persistent, I just like explore this idea just to understand Linux better, how Ubuntu 20.04 distribute files in /proc file-system.

root@jarvis:~# pifconfig wlp2s0
wlp2s0
	HWaddr 5c:e0:c5:1a:01:cb
	inet addr:192.168.0.3
	Bcast:192.168.0.255
	Mask:255.255.255.0
	inet6 addr: 2a02:c7f:c61f:5300:d029:1c9e:c150:1689/64 Scope: global
	inet6 addr: 2a02:c7f:c61f:5300:d560:7787:3407:ce11/64 Scope: global
	inet6 addr: fdec:aea1:e763:0:d029:1c9e:c150:1689/64 Scope: global
	inet6 addr: fdec:aea1:e763:0:2fce:6e12:2397:f1ac/64 Scope: global
	inet6 addr: fe80::be73:c03f:796:2ff9/64 Scope: link
	UP BROADCAST RUNNING MULTICAST

root@jarvis:~# echo 1 > /proc/sys/net/ipv6/conf/wlp2s0/disable_ipv6
root@jarvis:~# pifconfig wlp2s0
wlp2s0
	HWaddr 5c:e0:c5:1a:01:cb
	inet addr:192.168.0.3
	Bcast:192.168.0.255
	Mask:255.255.255.0
	UP BROADCAST RUNNING MULTICAST