---
title: 'Changing Priority from IPv6 to IPv4 in Ubuntu and CentOS: A Complete Guide | Zomro'
description: '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.'
image: 'https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/a48%40server-panel.net/2024/06/25/01_q50.jpg'
---

![opened image]()

  * [Zomro](https://zomro.com/)
  * [FAQ](https://zomro.com/blog/faq/)
  * Changing Priority from IPv6 to IPv4 in Ubuntu and CentOS: A Complete Guide 



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

25-06-2024

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

![Changing Priority from IPv6 to IPv4 in Ubuntu and CentOS: A Complete Guide - 1](https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/a48%40server-panel.net/2024/06/25/01_q50.jpg)

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

![Changing Priority from IPv6 to IPv4 in Ubuntu and CentOS: A Complete Guide - 2](https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/a48%40server-panel.net/2024/06/25/02_q50.jpg)

**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

![Changing Priority from IPv6 to IPv4 in Ubuntu and CentOS: A Complete Guide - 3](https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/a48%40server-panel.net/2024/06/25/03_q50.jpg)

**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

![Changing Priority from IPv6 to IPv4 in Ubuntu and CentOS: A Complete Guide - 4](https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/a48%40server-panel.net/2024/06/25/04_q50.jpg)

**Step 2: Applying Changes**

  1. Save the file and close the text editor.

  2. Apply the changes using the command:



    
    
    sudo sysctl -p

![Changing Priority from IPv6 to IPv4 in Ubuntu and CentOS: A Complete Guide - 5](https://dx86q6oq7ry0e.cloudfront.net/uploads/blog/a48%40server-panel.net/2024/06/25/05_q50.jpg)

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.

Similar articles:

[htop - Linux interactive system monitor](https://zomro.com/blog/faq/314-htop-interaktivnyj-sistemnyj-monitor-linux) [How to see whois of a domain](https://zomro.com/blog/faq/395-how-to-see-whois-of-a-domain) [Dockerfile: A Complete Guide for Beginners and Professionals](https://zomro.com/blog/faq/477-dockerfile-a-complete-guide-for-beginners-and-professionals) [How to enable sound on VPS with Windows Server 2012, 2016, 2019](https://zomro.com/blog/faq/269-kak-vkljuchit-zvuk-na-vps-s-windows-server-2012-2016-2019) [How to trace](https://zomro.com/blog/faq/331-kak-proizvesti-trassirovku)

[ GPU server from €174.80/mon ](/dedicated-servers/type=gpu)
