Skip to content

Instantly share code, notes, and snippets.

@digitalsparky
Last active February 20, 2022 14:17
Show Gist options
  • Save digitalsparky/b8ec04031006e7f3e1748e1014c3f09b to your computer and use it in GitHub Desktop.
Save digitalsparky/b8ec04031006e7f3e1748e1014c3f09b to your computer and use it in GitHub Desktop.
Disable IPv6 on Linux
#!/bin/bash
# set -x
##
# This is only needed in some occasions, please don't use this unless you know what you're doing.
# Installs/Uninstalls sysctl configuration disable IPv6 at the kernel level
#
# @AUTHOR: Matt Spurrier (@DigitalSparky) | https://digitalsparky.com | https://github.com/digitalsparky
# @LICENSE GPLv3
##
OUTFILE="/etc/sysctl.d/99-ipv6-disable.conf"
__disablev6_activate_sysctl_config() {
sysctl --system > /dev/null
if [ "$?" -gt 0 ]; then
__disablev6_term_revert
fi
}
__disablev6_write_sysctl_config() {
echo "Adding configuration to ${OUTFILE}..."
cat <<EOF > "${OUTFILE}"
net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
EOF
}
__disablev6_revert_config() {
rm -f "${OUTFILE}"
__disablev6_activate_sysctl_config
}
__disablev6_term_revert() {
echo "Configuration failed to apply, reverting."
__disablev6_revert_config
exit 255
}
__disablev6_root_check() {
if [[ "$UID" -ne 0 ]]; then
echo "This script must be run as root."
exit 126
fi
}
__disablev6_uninstall() {
__disablev6_root_check
__disablev6_revert_config
echo "Uninstallation Complete!"
}
__disablev6_install() {
__disablev6_root_check
__disablev6_write_sysctl_config
__disablev6_activate_sysctl_config
echo "Configuration Complete!"
echo "If you need to reverse this, simply run:"
echo "$0 --uninstall"
}
__disablev6_help() {
cat <<EOF
Linux IPv6 Disable/Enable
$0 [--install|--uninstall|--help]
--install: Installs the sysctl configuration to disable IPv6 and activates it
--uninstall: Uninstalls the sysctl configuration to disable IPv6 and activates the changes
--help: Prints this help screen
EOF
exit 0
}
trap '__disablev6_term_revert' TERM
case $1 in
"--install")
__disablev6_install
;;
"--uninstall")
__disablev6_uninstall
;;
"--help")
__disablev6_help
;;
*)
__disablev6_help
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment