[Solved] How do I dynamically disable and re-enable IPv6 via the command line?

Ubuntu Version: 22.04 and 24.04

Desktop Environment (if applicable): GNOME

Problem Description:

My internet connection uses IPv6. Unfortunately, this is a significant problem when it comes to updates, because when on IPv6:

So, what I’m looking to do is to disable IPv6 while doing apt update, apt upgrade, snap refresh, snap find, snap install, etc., but re-enable it afterwards.

What I’ve Tried:

I have the following commands.

To disable IPv6:

sudo sysctl --write net.ipv6.conf.all.disable_ipv6=1

This works perfectly and immediately.

To re-enable IPv6:

sudo sysctl --write net.ipv6.conf.all.disable_ipv6=0

This works, but only intermittently. Sometimes, it enables IPv6, and other times, it doesn’t. In the latter case, I have to reboot my machine to restore IPv6, which is hardly ideal.

What I think that I need

I think that I need a command that will tell the internet connection to refresh or restart or something like that after re-enabling IPv6.

I’ve been hunting around on the 'net, and I haven’t been able to find an answer to my needs.

Could you help, please?

Instead of disabling ipv6 at the kernel level, you can do it in NetworkManager.

There is a GUI for this setting: in the connection settings, IPv6 Settings tab > Method drop-down. Alternatively, you can change this setting by running the following command in Terminal:

nmcli connection modify 'Wired connection 1' ipv6.method disabled

replacing Wired connection 1 with the name of your actual internet connection.

To re-enable IPv6:

nmcli connection modify 'Wired connection 1' ipv6.method auto

These methods do not require rebooting, but may require disconnecting and reconnecting the connection in NetworkManager.

1 Like

I’ll use these two commands:

sysctl -w net.ipv6.conf.all.disable_ipv6=1
sysctl -w net.ipv6.conf.default.disable_ipv6=1

To re-enable IPv6, you can use the following commands:

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

Or on 'NMCLI’Display the list of network connections:

nmcli connection show

This command will list all network connections, including their UUID, type, and device name.
Set the ipv6.method parameter of the connection to disabled:

nmcli connection modify  ipv6.method "disabled"

Replace with the name of the connection you want to modify.
Restart the network connection to apply the changes:
nmcli connection down ; nmcli connection up
This will bring down the connection and then bring it back up, applying the new IPv6 settings.

To re-enable IPv6, you can follow a similar process by setting the ipv6.method parameter back to a non-disabled value, such as auto or linklocal:

nmcli connection modify  ipv6.method "auto"

To check, I leave the kernel alone:

 cat /sys/module/ipv6/parameters/disable
0
┌───────────────────>
│~ 
└─> sysctl net.ipv6.conf.all.disable_ipv6
net.ipv6.conf.all.disable_ipv6 = 1

0=enabled and 1=Disabled

1 Like

Thank you. I need the command line to do this, not the GUI, so that I can script it.

If I have a wireless connection, the actual internet connection isn’t the SSID of the wireless, is it? (I tried using that with your command. There was no error message, but it didn’t work.)

I don’t know how to find out the name of the actual internet connection, unfortunately. Do you know how I can do that?

Thats easy enough:

ip a
1 Like

That is the default, but it can be different.

In Terminal, you can get it by running

nmcli conn show

names are listed in the NAME column.

1 Like

@halogen2 and @1fallen — Thank you so much for your help and advice!

Using nmcli seems to have done the job for me.

2 Likes