This topic shows you how to enable or disable IPv6 on an Elastic Compute Service (ECS) instance that runs Alibaba Cloud Linux 2.
Prerequisites
The instance type must support IPv6. For more information, see Overview of instance families.
The instance must be in an IPv6-enabled region and deployed in a Virtual Private Cloud (VPC) with an IPv6 CIDR block. For more information, see Create a VPC with an IPv6 CIDR block.
Background information
In Alinux images earlier than aliyun-2.1903-x64-20G-alibase-20190829.vhd, IPv6 is disabled by default. Starting from version aliyun_2_1903_x64_20G_alibase_20200221.vhd, IPv6 is enabled by default.
Enable IPv6
Temporarily enable IPv6
This temporary configuration is lost when the instance stops or restarts. Use this method with caution.
In image versions aliyun_2_1903_64_20G_alibase_20190619.vhd and earlier, the .network files in the /etc/systemd/network/ directory are configured only with DHCP=ipv4. To temporarily enable IPv6, modify the file as follows:
Connect to the ECS instance.
For more information, see Connect to a Linux instance using Workbench.
Run the following command to go to the /etc/systemd/network/ directory:
cd /etc/systemd/network/Run the
lscommand to find the .network file in the directory.This topic uses the 50-dhcp.network file as an example.
Modify the 50-dhcp.network file.
vi /etc/systemd/network/50-dhcp.networkPress I to enter Insert mode.
Change the
DHCPsetting under[Network]toyes.NoteThe
Name=eth*parameter matches all network interfaces, which means that all network interfaces will have their IP addresses and routes configured by using DHCP. To configure a specific network interface, change theNameparameter to that interface, for example,Name=eth0. For more information about.networkfiles, see systemd.network.[Match] Name=eth* [Network] DHCP=yesAfter making the changes, press
Esc, enter:wq, and pressEnterto save and exit.Run the following commands to enable IPv6.
Enable IPv6 for all network interfaces.
sudo sysctl -w net.ipv6.conf.all.disable_ipv6=0 sudo sysctl -w net.ipv6.conf.default.disable_ipv6=0Enable IPv6 for a specific network interface (for example,
eth0).sudo sysctl -w net.ipv6.conf.eth0.disable_ipv6=0
Permanently enable IPv6
To permanently enable IPv6, follow these steps: This configuration persists after the instance restarts.
Connect to the ECS instance.
For more information, see Connect to a Linux instance using Workbench.
Modify the
/etc/sysctl.conffile.vi /etc/sysctl.confPress i to enter Insert mode. Modify the file content by using one of the following methods:
Delete the following configuration lines:
net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 net.ipv6.conf.lo.disable_ipv6 = 1Change the values in the configuration lines to the following:
net.ipv6.conf.all.disable_ipv6 = 0 net.ipv6.conf.default.disable_ipv6 = 0 net.ipv6.conf.lo.disable_ipv6 = 0To enable IPv6 for a specific network interface, use the following example configuration.
net.ipv6.conf.eth0.disable_ipv6 = 0
After making the changes, press
Esc, enter:wq, and pressEnterto save and exit.Check for differences between the
/etc/sysctl.conffile and the/etc/sysctl.conffile within initramfs.diff -u /etc/sysctl.conf <(lsinitrd -f /etc/sysctl.conf)NoteAlibaba Cloud Linux 2 uses an initial RAM filesystem (initramfs). If the
/etc/sysctl.conffile within the initramfs differs from the main/etc/sysctl.conffile, the system might load the initramfs configuration at startup, overriding your changes.If the two files are different, run the following command to regenerate the initramfs:
sudo dracut -v -fReboot the instance.
sudo rebootRun the
ifconfigcommand to verify that IPv6 is enabled.If the output includes the following lines, IPv6 is enabled:
inet6 <Unicast address that starts with fe80::> inet6 <IPv6 address of the instance>
Disable IPv6
Temporarily disable IPv6
Run the following commands to temporarily disable IPv6.
This temporary configuration is lost when the instance stops or restarts. Use this method with caution.
Disable IPv6 for all network interfaces.
sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1 sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1Disable IPv6 for a specific network interface (for example,
eth0).sudo sysctl -w net.ipv6.conf.eth0.disable_ipv6=1
Permanently disable IPv6
The following steps permanently disable IPv6. The configuration persists after the instance restarts. You can disable IPv6 by using either of the following methods.
Permanently disable IPv6 by using one of the following kernel parameters.
sudo grubby --args="ipv6.disable_ipv6=1" --update-kernel=/boot/vmlinuz-$(uname -r)sudo grubby --args="ipv6.disable=1" --update-kernel=/boot/vmlinuz-$(uname -r)
NoteThe
--args="ipv6.disable_ipv6=1"parameter disables IPv6 for network interfaces, while--args="ipv6.disable=1"disables the entire IPv6 kernel module. For more information, visit Linux kernel IPv6.Modify the /etc/sysctl.conf file to permanently disable IPv6.
Modify the
/etc/sysctl.conffile.vi /etc/sysctl.confPress I to enter Insert mode and change the configuration to the following:
net.ipv6.conf.all.disable_ipv6=1 net.ipv6.conf.default.disable_ipv6=1Disable IPv6 for a specific network interface. Example:
net.ipv6.conf.eth0.disable_ipv6=1Reboot the instance.
sudo rebootRun the
ifconfigcommand to verify that IPv6 is disabled.If the output does not include the following lines, IPv6 is disabled:
inet6 <Unicast address that starts with fe80::> inet6 <IPv6 address of the instance>
Configure IPv6 for multiple network interfaces
To configure multiple IPv6 addresses, follow these steps.
Connect to the ECS instance.
For more information, see Connect to a Linux instance using Workbench.
Run the following command to go to the
/etc/systemd/network/directory:cd /etc/systemd/network/Run the
lscommand to find the.networkfile in the directory.In this example, the 10-eth0.network file is used.
Run the
cpcommand to copy it to a new configuration file.For example:
cp 10-eth0.network 20-dhcp.networkRun the following command to modify the new configuration file.
sed -i 's/^Name.*$/Name=*/g' /etc/systemd/network/20-dhcp.networkRestart the systemd-networkd service to apply the changes.
sudo systemctl restart systemd-networkd