brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: one line in gai.conf makes Linux prefer IPv4 when IPv6 is broken

Linux
~/tips/tip-linux-gai-conf-prefer-ipv4

precedence ::ffff:0:0/96 100

Make programs try the IPv4 address first when a host has both.

When your network hands out IPv6 but the route out is flaky, programs try the IPv6 address first and hang before they fall back. /etc/gai.conf changes that order for everything that uses the glibc resolver.

On Debian and Ubuntu the file already contains the line you need, commented out, below a remark about sites that prefer IPv4. Remove the # so it reads:

precedence ::ffff:0:0/96  100

IPv4 addresses are represented in that table as ::ffff:0:0/96. Raising their precedence to 100 puts them above IPv6 when getaddrinfo() sorts the addresses of a host. There is no service to restart; each program reads the file when it does a lookup, though long running ones may cache it.

Check the result with:

getent ahosts example.com | head -n 3

The first lines should now be IPv4 addresses.

Read the man page before you add more lines. As soon as the file contains one precedence line, the whole built in default table is replaced by what you wrote. To keep the other defaults, copy all the precedence lines from the example in the file and change only the IPv4 one. I also see this as a workaround: the real fix is the IPv6 route. Programs that do their own resolving, like many Go binaries, ignore this file.

Further reading: gai.conf(5) manual page, getaddrinfo(3) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link