Disable IPv6 on Linux (GRUB and sysctl)
Disable IPv6 at boot with GRUB and at runtime with sysctl, and check the state with an audit script.
IPv6 can be disabled on a RHEL-family system at two levels: at boot, with a kernel parameter in GRUB, and at runtime, with sysctl. The audit script below checks both.
Check script
#!/bin/bash
output=""
efidir=$(find /boot/efi/EFI/* -type d -not -name 'BOOT')
gbdir=$(find /boot -maxdepth 1 -type d -name 'grub*')
[ -f "$efidir"/grubenv ] && grubfile="$efidir/grubenv" || grubfile="$gbdir/grubenv"
! grep "^\s*kernelopts=" "$grubfile" | grep -vq ipv6.disable=1 && output="ipv6 disabled in grub config"
if grep -Eqs "^\s*net\.ipv6\.conf\.all\.disable_ipv6\s*=\s*1\b" /etc/sysctl.conf /etc/sysctl.d/*.conf /usr/lib/sysctl.d/*.conf /run/sysctl.d/*.conf && grep -Eqs "^\s*net\.ipv6\.conf\.default\.disable_ipv6\s*=\s*1\b" /etc/sysctl.conf /etc/sysctl.d/*.conf /usr/lib/sysctl.d/*.conf /run/sysctl.d/*.conf && sysctl net.ipv6.conf.all.disable_ipv6 | grep -Eq "^\s*net\.ipv6\.conf\.all\.disable_ipv6\s*=\s*1\b" && sysctl net.ipv6.conf.default.disable_ipv6 | grep -Eq "^\s*net\.ipv6\.conf\.default\.disable_ipv6\s*=\s*1\b"; then
[ -n "$output" ] && output="$output, and in sysctl config" || output="ipv6 disabled in sysctl config"
fi
[ -n "$output" ] && echo -e "\n\n$output\n" || echo -e "\n\nIPv6 is enabled on the system\n"
Disable at boot (GRUB)
In /etc/default/grub, add ipv6.disable=1 to the value of GRUB_CMDLINE_LINUX:
GRUB_CMDLINE_LINUX="ipv6.disable=1"
Then regenerate the GRUB configuration:
grub2-mkconfig -o /boot/grub2/grub.cfg
Disable with sysctl
To make the setting persistent, add it to /etc/sysctl.conf or to a file in /etc/sysctl.d/:
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
To apply it immediately to the running kernel:
sysctl -w net.ipv6.conf.all.disable_ipv6=1
sysctl -w net.ipv6.conf.default.disable_ipv6=1
sysctl -w net.ipv6.route.flush=1
Notes
- On UEFI systems, the
grub.cfgfile that is read at boot can be under/boot/efi/EFI/<distribution>/. Check which file is actually used before regenerating the configuration.