How to Fix “curl:(6) Could not resolve host” Error in Linux

Linux is an operating system whose core functionality revolves around using the command line terminal. All the tasks can be performed through the Linux terminal. Downloading from the web is one of these features. This is often accomplished using the curl command in the terminal. When utilizing this feature, an error can be encountered that has the statement “curl:(6) Could not resolve host”.

This article will shed some light on all possible reasons along with solutions for the “curl:(6) Could not resolve host” error by supporting the below content:

  1. What is the “curl:(6) Could not resolve host” Error in Linux?
  2. What are the Reasons/Causes for the “curl:(6) Could not resolve host” Error in Linux?
  3. How to Fix/Resolve “curl:(6) Could not resolve host” Error in Linux?
  4. Bonus Tip 1: How to Fix/Resolve the “Command ‘curl’ not found” Error?
  5. Bonus Tip 2: How to Make REST API Requests Via the Curl Command?

What is the “curl:(6) Could not resolve host” Error in Linux?

The error “curl:(6) Could not resolve host” occurs when the curl command cannot connect to the specified hostname or IP address. The visual representation of this error is given below:

Let’s focus on the reasons behind this error.

What are the Reasons/Causes for the “curl:(6) Could not resolve host” Error in Linux?

The “curl:(6) Could not resolve host” error can happen for several reasons, such as the disabled IPv6, the hostname or IP address is misspelled or invalid, the DNS server is not working properly, or cannot resolve the hostname to an IP address. 

This error may occur when the network connection is down or blocked by a firewall or proxy. Therefore, the curl command is not configured correctly or does not support the protocol or port of the host.

How to Fix/Resolve “curl:(6) Could not resolve host” Error in Linux?

To fix the “curl:(6) Could not resolve host” error, users can try multiple ways, check the spelling and validity of the hostname or IP address, use a different DNS server, or flush the DNS cache on the machine, check the network connection and firewall settings and make sure they allow curl to access the host.

There may be numerous reasons which will invoke this error. This section will not only elaborate on what these reasons are but also explain the necessary steps you can take to fix this problem:

Reason 1: Not Support IPv6 Properly

IPv6 is a network protocol that allows system devices to perform communication over the internet. Some Linux systems may not support IPv6 properly, and this can cause problems when using tools like curl to access web resources. 

Solution: Disable IPv6 on the System

One solution to fix/resolve the “curl:(6) Could not resolve host” error is to disable IPv6 on the Linux system, and use IPv4 instead. The steps to achieve this are as below:

Step 1: Check if IPv6 is Enabled

Firstly, users must check whether IPv6 is enabled or not on the current system. For this, use the “cat” command by specifying the path where the network configuration exists:

cat /proc/sys/net/ipv6/conf/all/disable_ipv6

After execution of the command, if the output is 0, then IPv6 is enabled. If the output is 1, then IPv6 is disabled. Therefore, don’t be required to move further steps and follow other solutions.

Step 2: Disable IPv6 Permanently

To disable IPv6 permanently, users need to edit the file “/etc/sysctl.conf” as the root user. For this, utilize a text editor, e.g., vim, Nano, or gedit. For instance, the nano editor is used to execute the command:

sudo nano /etc/sysctl.conf

It navigates to the file named “sysctl.conf”. Now, users can add the below script lines at the end of the above-mentioned file:


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

Now, save and close the file, and then run the command to apply the changes.

Verification

Finally, users can verify that the IPv6 has been disabled on the Linux operating system by executing the “sysctl” command with the “p” option that reads the value of the system configuration file:

sudo sysctl -p

Reason 2: Syntax Problem

The most obvious reason for the problem is that there is some issue with the curl command’s syntax. Usually, the syntax to curl is very simple to follow. Still, even the slightest mistake can invoke the error as is shown in the snippet below:

It is very clearly seen that the syntax for the command above is incorrect. It is a very simple mistake that there is an unnecessary space in the link provided after curl.

Solution: Use the Correct Syntax

The only way to fix this issue is to follow the correct syntax of any curl command. The standard syntax for any curl command is as follows:

curl [options] [URL]

Now, let’s implement this syntax for the link shown above:


curl https://www.google.com/

Now that the syntax problem has been avoided, we can see that the curl command works without any errors.

Reason 3: No DNS Nameserver

Nameservers act as a link between the domain name and the remote server of any web page. Instead of using the IP addresses, nameservers are utilized. But if the Nameserver is missing from your /etc/resolv.conf file then this error will be prompted.

Solution: Add Google DNS Nameserver

The simple solution to the “curl:(6) Could not resolve host” error is to add the nameserver of the URL to the file and save it. To add the nameserver, first, open the “resolv.conf” file using the nano editor via the below command:

sudo nano /etc/resolv.conf

It opens the file and users need to add the nameserver to the file via the following format:

nameserver ip.address

Check out the sample below to verify:

Once the nameserver is added, you can run the curl command with the link and the error will be resolved.

Verification

The dig command transmits the DNS query to Google’s server (8.8.8.8). To check the DNS is working, execute the below command by specifying the server’s name as “google.com”:

dig google.com

Let us explore another important error that people may also ask regarding the curl command in Linux.

Bonus Tip 1: How to Fix/Resolve the “Command ‘curl’ not found” Error?

To fix the “Command ‘curl’ not found” error, install the curl package on the Linux operating system. To do so, first, update the system repository and then install curl packages based on their Linux distributions. The reason for the possible solution is given below:

Reason: Not Installed Curl Package 

One of the most prominent reasons for causing the “Command ‘curl’ not found” error is that the curl package is not installed in the operating system. The screenshot of the above-mentioned error is as below:

To fix the “Command ‘curl’ not found” error, follow the below instructions step by step:

Step 1: Update System Repository 

Before installing any package, the user must update the system repository. For this, execute the following command with the “sudo” privileges:

sudo apt update    # Ubuntu/Debian/Linux Mint
sudo yum update    # RHEL/CentOS/Fedora/Oracle Linux/Alma Linux

Step 2: Install Curl Packages

To install curl packages in the Linux operating system, users can execute the command with the sudo keyword based on Linux distribution:

sudo apt install curl  # Ubuntu/Debian
sudo yum install curl    # RHEL / CentOS / Fedora
sudo zypper install curl  # OpenSUSE
sudo pacman -Sy curl   # Arch Linux

To explore more solutions, users can follow our guide on “How to fix: curl command not found” Error.

Bonus Tip 2: How to Make REST API Requests via the Curl Command?

To make a REST API request using curl, users are required to specify the method (GET, POST, PUT, DELETE, etc.), and the URL of the endpoint. Also, optionally any headers or data that users want to send or receive to the specific URL. Finally, it returns the response from the server end, which may be in JSON, XML, or another format.

To explore this topic further, follow our REST API Requests.

Conclusion

The “curl:(6) Could not resolve host” error occurs when the curl command is used with incorrect syntax, without the support of IPv6 properly, or the missing DNS nameserver. To fix it, users need to disable IPv6, verify and check the syntax of the command, use a different DNS server, or add a nameserver for the URL into the /etc/resolv.conf file. This article has provided a detailed guide on why the error “curl:(6) Could not resolve host” occurs and the possible solutions to fix it.