opened image

Changing Priority from IPv6 to IPv4 in Ubuntu and CentOS: A Complete Guide

In modern networks, IPv6 is increasingly used, but there are cases when it is necessary to give preference to IPv4. This article discusses ways to change the priority from IPv6 to IPv4 for various versions of Ubuntu (20.04 and 22.04) and CentOS 7. We will look at two methods: configuring the gai.conf file and using sysctl parameters.

 

Method 1: Changing Priority via gai.conf

 

The gai.conf (getaddrinfo configuration) file is used to set address preferences when resolving names. Configuring this file will allow the system to prefer IPv4 addresses over IPv6.

 

Ubuntu 20.04 and 22.04

Step 1: Opening and Editing gai.conf

 

1. Open the terminal.

2. Edit the /etc/gai.conf file with superuser rights:

 

sudo vim /etc/gai.conf

 

 

 

Find the line containing #precedence ::ffff:0:0/96 10. If it is commented out (starts with #), remove the # symbol and change the priority weight to 100 to activate it and increase the priority of IPv4:

 

precedence ::ffff:0:0/96 100

 

 

 

Step 2: Saving Changes and Restarting Network Services

  1. Save the file and close the text editor (in nano: press Ctrl+X, then Y and Enter).

  2. To apply the changes, restart the network services or reboot the system:

 

systemctl restart systemd-networkd

 

 

 

Automatic Line Change via Console Command

To automatically change the line, use the following command:

 

sudo sed -i 's/^#precedence ::ffff:0:0\/96  100/precedence ::ffff:0:0\/96 100/' /etc/gai.conf

 

 

This command will remove the # symbol and change the priority weight to 100 so that the system prefers IPv4 addresses.

 

CentOS 7

 

Step 1: Opening and Editing gai.conf

  1. Open the terminal.

  2. Edit the /etc/gai.conf file with superuser rights:

 

​​​​​​​sudo vim /etc/gai.conf

 

Find the line containing #precedence ::ffff:0:0/96 10. If it is commented out (starts with #), remove the # symbol and change the priority weight to 100 to activate it and increase the priority of IPv4:

 

precedence ::ffff:0:0/96 100

 

Step 2: Saving Changes and Restarting Network Services

  1. Save the file and close the text editor.

  2. Restart the network services:

 

systemctl restart systemd-networkd

 

Automatic Line Change via Console Command

To automatically change the line, use the following command:

 

sudo sed -i 's/^#precedence ::ffff:0:0\/96  100/precedence ::ffff:0:0\/96 100/' /etc/gai.conf

 

This command will remove the # symbol and change the priority weight to 100 so that the system prefers IPv4 addresses.

 

 

Method 2: Using sysctl to Configure IPv4 Priority

 

Using sysctl parameters allows you to manage kernel settings, which is useful for completely disabling IPv6 or changing its behavior at the kernel level.

 

Ubuntu 20.04 and 22.04

 

Step 1: Disabling IPv6 via sysctl

  1. Open the terminal.

  2. Edit the /etc/sysctl.conf file with superuser rights:

 

 

sudo vim /etc/sysctl.conf

 

Add the following lines to the end of the file to disable IPv6:

 

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1

 

 

Step 2: Applying Changes

  1. Save the file and close the text editor.

  2. Apply the changes using the command:

 

sudo sysctl -p

 

 

Restart the network services:

 

Restarting systemd-networkd:

 

sudo systemctl restart systemd-networkd

 

Restarting NetworkManager:

sudo systemctl restart NetworkManager

 

CentOS 7

Step 1: Disabling IPv6 via sysctl

  1. Open the terminal.

  2. Edit the /etc/sysctl.conf file with superuser rights:

 

 

sudo vim /etc/sysctl.conf

 

Add the following lines to the end of the file to disable IPv6:

 

net.ipv6.conf.all.disable_ipv6 = 1

net.ipv6.conf.default.disable_ipv6 = 1

 

Step 2: Applying Changes

  1. Save the file and close the text editor.

  2. Apply the changes using the command:

 

 

sudo sysctl -p

 

 

Restart the network services:

 

Restarting network:

 

sudo systemctl restart network

 

Restarting NetworkManager:

 

sudo systemctl restart NetworkManager

 

Comparison of Methods

 

Using gai.conf

Advantages:

  1. Does not require complete disabling of IPv6, which can be useful in networks where IPv6 is necessary.

  2. Easy to configure and minimal changes to the system.

Disadvantages:

  1. Changes only affect the order of name resolution, not the entire network protocol stack.

Using sysctl

Advantages:

  1. Complete disabling of IPv6 can simplify network troubleshooting and eliminate conflicts.

Disadvantages:

  1. Complete disabling of IPv6 can cause problems in networks that use only IPv6.

  2. Requires restarting network services or the system to apply changes.

 

Conclusion

 

Changing the priority from IPv6 to IPv4 in Ubuntu and CentOS can be achieved in several ways. Using the gai.conf file allows you to change address priorities when resolving names, while configuring through sysctl can completely disable IPv6, forcing the system to use only IPv4. Both methods have their advantages and disadvantages, and the choice depends on your specific requirements and network conditions. We hope this guide helps you configure your system.