A script to disable IPv6 on your Linux machine

Simply create a file using the following command:

sudo nano ipv6_disable.sh

Copy and paste the following commands into the file:

(or simply download the file at bottom of this page)


# INSTALLING AND CONFIGURING IPTABLES PERSISTENT AND DISABLING IPV6
echo ""
echo "INSTALLING IPTABLES PERSISTENT....................."
sleep 1
sudo apt install iptables-persistent -y
echo "IPTABLES PERSISTENT INSTALLED!"
sleep 1

echo ""
echo "DISABLING IPV6 VIA IPTABLES....................."
sleep 1
sudo ip6tables -P INPUT DROP
sudo ip6tables -P FORWARD DROP
sudo ip6tables-save | sudo tee /etc/iptables/rules.v6
echo "IPV6 RULES SAVED!"
sleep 1

sudo cp /etc/sysctl.conf /etc/sysctl.conf.bak

echo "Backup of /etc/sysctl.conf created at /etc/sysctl.conf.bak"

echo ""
echo "MODIFYING /etc/sysctl.conf TO DISABLE IPV6....................."
sleep 1
sudo sed -i '/^net.ipv6.conf.all.disable_ipv6/d' /etc/sysctl.conf
sudo sed -i '/^net.ipv6.conf.default.disable_ipv6/d' /etc/sysctl.conf
sudo sed -i '/^net.ipv6.conf.lo.disable_ipv6/d' /etc/sysctl.conf
echo "net.ipv6.conf.all.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.conf
echo "net.ipv6.conf.default.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.conf
echo "net.ipv6.conf.lo.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.conf
echo "CHANGES TO /etc/sysctl.conf SAVED!"
sleep 1

sudo cp /etc/ufw/sysctl.conf /etc/ufw/sysctl.conf.bak

echo "Backup of /etc/ufw/sysctl.conf created at /etc/ufw/sysctl.conf.bak"

echo ""
echo "MODIFYING /etc/ufw/sysctl.conf....................."
sleep 1
sudo sed -i 's/^net\/ipv6\/conf\/all\/accept_redirects=0/#net\/ipv6\/conf\/all\/accept_redirects=0/' /etc/ufw/sysctl.conf
sudo sed -i 's/^net\/ipv6\/conf\/default\/accept_redirects=0/#net\/ipv6\/conf\/default\/accept_redirects=0/' /etc/ufw/sysctl.conf
sudo sed -i 's/^#net\/ipv6\/conf\/default\/autoconf=1/net\/ipv6\/conf\/default\/autoconf=1/' /etc/ufw/sysctl.conf
sudo sed -i 's/^#net\/ipv6\/conf\/all\/autoconf=1/net\/ipv6\/conf\/all\/autoconf=1/' /etc/ufw/sysctl.conf
echo "CHANGES TO /etc/ufw/sysctl.conf SAVED!"
echo ""
echo "Resetting to apply changes"
sysctl -p
sleep 1

# Path to the Avahi configuration file


AVAHI_CONF="/etc/avahi/avahi-daemon.conf"

# Check if the file exists
if [[ ! -f "$AVAHI_CONF" ]]; then
echo "Error: $AVAHI_CONF not found!"
exit 1
fi

# Modify the use-ipv6 setting
sed -i 's/^use-ipv6=yes/use-ipv6=no/' "$AVAHI_CONF"

# Restart the Avahi service to apply changes
systemctl restart avahi-daemon

# Confirm the change
grep "^use-ipv6" "$AVAHI_CONF"

echo "Avahi IPv6 disabled successfully."

# Disable IPv6 globally in NetworkManger

# Path to the NetworkManager configuration file
CONFIG_FILE="/etc/NetworkManager/NetworkManager.conf"

# Backup the configuration file before modifying it (in case of issues)
echo "Backing up $CONFIG_FILE to $CONFIG_FILE.bak"
sudo cp "$CONFIG_FILE" "$CONFIG_FILE.bak"

# Check if the file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo "Error: $CONFIG_FILE does not exist."
exit 1
fi

# Modify the NetworkManager.conf file
echo "Modifying $CONFIG_FILE to disable IPv6 and set ifupdown managed=true"

# Add/modify [ifupdown] section to ensure 'managed=true'
sudo sed -i '/^\[ifupdown\]/,/^\[/{s/^managed=.*/managed=true/}' "$CONFIG_FILE"
# If [ifupdown] doesn't exist, add it to the file
if ! grep -q "^\[ifupdown\]" "$CONFIG_FILE"; then
echo -e "\n[ifupdown]\nmanaged=true" | sudo tee -a "$CONFIG_FILE" > /dev/null
fi

# Add [ipv6] section to disable IPv6 (method=ignore)
if ! grep -q "^\[ipv6\]" "$CONFIG_FILE"; then
echo -e "\n[ipv6]\nmethod=ignore" | sudo tee -a "$CONFIG_FILE" > /dev/null
else
sudo sed -i '/^\[ipv6\]/,/^/{s/^method=.*/method=ignore/}' "$CONFIG_FILE"
fi

# Restart NetworkManager to apply the changes
echo "Restarting NetworkManager to apply the changes..."
sudo systemctl restart NetworkManager

# Inform the user of the successful operation
echo "NetworkManager has been restarted. IPv6 is now disabled, and ifupdown is set to managed=true."

echo ""
echo "IPv6 CONFIGURATION COMPLETE!"

then hit Ctl+O and Enter to save, Ctl+X to exit

Make the file executable by running this command:

sudo chmod +x ipv6_disable.sh

Now run the script like this:

sudo bash ipv6_disable.sh

You can also download the file here (avoids syntax errors):

ipv6_disable.sh