Ошибка could not resolve hostname

SSH stands for secure shell, and it is a security protocol that provides end-to-end encryption of data. SSH could not resolve hostname is an error message that occurs when the host system fails to resolve the IP address of the remote host provided with the ssh command. Ssh protocol is widely used to provide secure remote shell access.

The ssh command is used to connect to a remote host. The ssh command requires a hostname to connect to, which is then inquired from a DNS(Domain Name System) server that sends the IP address of the remote host the user is trying to login to. If, for some reason, if the hostname is not resolved, then the ssh could not resolve the hostname error message is displayed.

Understanding SSH

Secure Shell is an intermediate protocol that is implemented between the application layer and the transport layer of the Internet protocol stack. It works on top of the TCP protocol, ensuring reliable delivery and data integrity. To connect to a remote shell, use the ssh command and provide the username and the hostname of the remote host you are trying to connect. Public key cryptography is used to authenticate the user. The private key is stored in the user’s host, which provides authentication. The ssh could not resolve hostname is one of the common errors faced while using ssh.

ssh command running on Arch machine
ssh command running on Arch machine

SSH hostname resolution and DNS

The hostname provided in the ssh command has to be resolved to an IP address, i.e., the address of the remote host in computer-readable form. The hostname can be provided in the IP address form example, 192.168.43.89, or a name can be used instead of example.com, for example. A DNS server is used to resolve the hostname to an IP address. The string of characters provided as the hostname in the command is sent to the DNS server, which then, in turn, returns the IP address of the host.

If the host is unable to contact the DNS server or the DNS server doesn’t return a valid IP address in return then, ssh could not resolve the hostname error that occurs. The ssh could not resolve hostname can also happen due to the cached DNS data stored in the system. Frequently used Domain name addresses are stored in cache storage in the host system so that the system doesn’t need to enquire the DNS server every time it makes a request. If the cache data is outdated or the host’s IP has been charged, the cached data becomes useless and can cause problems like ssh could not resolve the hostname.

If you are receiving the ssh could not resolve the hostname error, then a few steps can be employed to resolve and avoid this error message. Consider the given steps to fix the ssh could not resolve the hostname error.

Check the hostname

Most of the basic and simple measures to avoid the ssh could not resolve hostname is to check whether the hostname you are using with the ssh command is correct or not. Using the wrong hostname or domain address might result in a Wrong DNS response or no valid response at all. Check your hostname address for typos and syntax errors of the command. If everything is correct but still getting the hostname does not resolve the error, then employ the following measures.

Fixing Xtools XToolkit Installation Error

Clearing DNS Cache

Clearing the DNS cache might also help resolve the ssh could not fix the hostname error. After the DNS cached data is deleted, a new fresh DNS inquiry is sent to the DNS server for each request, which would be beneficial if the IP address of the host is changed. Follow the given steps to clear the DNS cache in Windows.

  • Press Windows + R.
  • Enter cmd in the serach box and hit enter.
  • In the shell Enter the command ipconfig /flushdns.
  • The DNS cache is not cleared.
  • Try running the ssh command again.

For Ubuntu users, Enter the following command in the terminal to clear the DNS cache manually.

sudo clean restart

Editing the /etc/hosts file

If you are trying to connect to a remote host using ssh for the first time and getting a hostname error not resolved error in return, try editing the ssh hosts file and entering the username and hostname in the file. Employing the given steps to edit the hosts’ file and fix the ssh could not resolve the hostname error.

  • Open the terminal emulator.
  • Enter sudo nano /etc/hosts.
  • Enter your root password.
  • Now in the file, enter the IP address and hostname of your remote host.
  • Press ctrl + O to save the file.
  • Press ctrl + X to exit nano.
  • Run the ssh command again.
RemoteHostName

SSh could not resolve hostname github.com

ssh could not resolve hostname github.com

The ssh could not resolve hostname error can also show up while using the git version management program. The git uses the ssh while cloning a repository or pushing a remote repository. The issue can be fixed with the following steps.

  • Try to ping the github.com using the ping github.com command. If the server responds then try to run the git command again after rebooting the system, it might be a normal network issue.
  • If the issue persists, follow the steps mentioned above, such as clearing the DNS cache.
  • Run the following command to restart the network manager in linux, which might help resolve the issue.
sudo service network-manager restart

SSH could not resolve hostname Gitlab

Gitlab is another popular open-source platform that is widely used in DevOps operations. “ssh: Could not resolve hostname gitlab.example.com: Temporary failure in name resolution” is the error message that is displayed while logging into the GitLab server. As the message itself suggests, this is a temporary failure in the name resolution, and chances would be resolved automatically after some time. But if the problem persists, follow the steps suggested above to make it work. You could also try adding ssh:// before the git URL to resolve the hostname issue.

git clone ssh://[email protected]:username/This_is_a_demo.git

SSH could not resolve hostname Gitbucket.org

Bitbucket is another open platform used to host git repositories online. The same hostname resolution problem can be encountered with gitbucket.org as with github.com. To resolve this issue, use the suggested methods and configure your .git config file. Open the config file in the .git folder for your project’s main directory. Change the remote directory listing as mentioned below (remove the ssh:// in the config file) and rerun the command.

url = ssh://[email protected] 
//before

url = [email protected]
//after

FAQs on SSH Could Not Resolve Hostname

What is TCP?

It stands for Transfer Control Protocol. It is a transport layer protocol that ensures the reliable hosts-to-host delivery of the data packets.

What is the difference between git and GitHub?

Git is a version management program that keeps track of the changes in a project, While GitHub is an online platform where people can share their git repositories.

What is the Linux equivalent of ipconfig?

The Linux equivalent of the ipconfig command is ifconfig.

Conclusion

The ssh could not resolve hostname is a common error that any user can face. It is not that big of an issue. It simply happens when the ssh command fails to resolve the IP address of the provided hostname. In such situations clearing the DNS cache or editing the /etc/host file can help. This article provided you with the necessary measure and solutions to fix this error. In the end, we also mentioned what to do if you are receiving this error while using the git CLI.

Trending Now

  • Fixing Xtools XToolkit Installation Error

    Fixing Xtools XToolkit Installation Error

    March 3, 2023

  • Troubleshoot DeepMotion Preview Not Working: Tips and Tricks

    Troubleshoot DeepMotion Preview Not Working: Tips and Tricks

    by Dharam Nath JhaMarch 3, 2023

  • Getting Virtwifi has No Internet Access? Here’s What to Do!

    Getting Virtwifi has No Internet Access? Here’s What to Do!

    by Dharam Nath JhaFebruary 16, 2023

  • The Ultimate Guide to Fixing Wabbajack Failed Unknown Error

    The Ultimate Guide to Fixing Wabbajack Failed Unknown Error

    by Dharam Nath JhaFebruary 16, 2023

The process of setting up a GitHub account works just fine but it doesn’t work when I try pushing my repository to GitHub. The error message it shows is as follows:

ssh: Could not resolve hostname github.com: Name or service not known
fatal: The remote end hung up unexpectedly

I have tried changing RSA keys and all but still getting error.

What causes this error?

waldyrious's user avatar

waldyrious

3,6354 gold badges33 silver badges41 bronze badges

asked Feb 22, 2012 at 10:53

Neel Pathak's user avatar

8

Recently, I have seen this problem too. Below, you have my solution:

  1. ping github.com, if ping failed. it is DNS error.
  2. sudo vim /etc/resolv.conf, the add:
    nameserver 8.8.8.8
    nameserver 8.8.4.4

Or it can be a genuine network issue. Restart your network-manager using sudo service network-manager restart or fix it up

nbro's user avatar

nbro

15.2k32 gold badges110 silver badges196 bronze badges

answered Jul 13, 2016 at 6:15

Qiangks's user avatar

QiangksQiangks

1,2911 gold badge8 silver badges3 bronze badges

5

I have just received this error after switching from HTTPS to SSH (for my origin remote). To fix, I simply ran the following command (for each repo):

ssh -T git@github.com

Upon receiving a successful response, I could fetch/push to the repo with ssh.

I took that command from Git’s Testing your SSH connection guide, which is part of the greater Connecting to GitHub with with SSH guide.

answered May 18, 2018 at 23:41

Todd's user avatar

ToddTodd

2,7622 gold badges29 silver badges39 bronze badges

4

Github is currently (on 22nd of feb 2012, again on 5th of sept 2016) having some problems with pushing/pulling over ssh.
I’m experiencing the same problems.

Pulling over https is working thought.

You can try pushing over https by following this guide: https://help.github.com/articles/using-ssh-over-the-https-port/

This would only help if https is not affected.
you can try and look at https://www.githubstatus.com/ to see if there are any reported issues at github’s end.

If github is currently being DDOS’ed they may choose to enable DDOS protection from a 3rd party. This would mean they change their dns records and/or servers if these are part of the DDOS.

Flushing your dns, changing your dns server or restarting your network (which will possibly flush your dns cache) as stated in the other answer might help in fixing the issue for you if github is mitigating a DDOS.

Or if you have problems with your ssh setup you can find the ssh faq here: http://help.github.com/ssh-issues/

Other ways to flush your dns,
in windows in your terminal

ipconfig /flushdns

on macos

dscacheutil -flushcache

on linux

service nscd restart

or simply rebooting ;)

answered Feb 22, 2012 at 10:54

Jens Timmerman's user avatar

Jens TimmermanJens Timmerman

9,2361 gold badge41 silver badges47 bronze badges

I was having this exact issue and all that I had to do was to restart my desktop

answered Feb 23, 2022 at 14:50

Carlos's user avatar

CarlosCarlos

3,2001 gold badge16 silver badges14 bronze badges

2

In my case I had misspelled something when manually adding the remote origin with git remote add origin ....

It’s worth checking your remote is correct with git remote -v

answered Dec 18, 2018 at 0:15

Skylar Brown's user avatar

Skylar BrownSkylar Brown

3,1942 gold badges14 silver badges7 bronze badges

Each of this could help

  1. Confirm if you are still connected using ssh.
    Type this:
    ssh -T git@github.com

If you get the message that you were successfully authenticated and that github doesn’t suppoet shell access then you can trying pushing your code again.

2)Create a new ssh key, add it to github and test it using
ssh -T git@github.com

answered Nov 7, 2020 at 17:25

Onyejiaku Theodore Kelechukwu's user avatar

4

If you use Windows 10 and you are facing this error, try using ssh -T git@github.com to authenticate again to Github. It worked like charm for me.

answered Sep 29, 2021 at 14:43

Khaled Adrani's user avatar

Sometimes restarting your machine is also a good idea. When I faced the same issue after restarting the system, it told me that your private key is locked and github cant access that.. so enter your password to do that.. I entered the password and then the issue was resolved.

answered May 8, 2019 at 9:07

Namrata's user avatar

You can check which the key is being used by trying to connect to git@github.com:

$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/you/.ssh/id_rsa type -1
> debug1: identity file /Users/you/.ssh/id_rsa-cert type -1
> debug1: identity file /Users/you/.ssh/id_dsa type -1
> debug1: identity file /Users/you/.ssh/id_dsa-cert type -1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Trying private key: /Users/you/.ssh/id_rsa
> debug1: Trying private key: /Users/you/.ssh/id_dsa
> debug1: No more authentication methods to try.
> Permission denied (publickey).

In that example, we did not have any keys for SSH to use. The «-1» at the end of the «identity file» lines means SSH couldn’t find a file to use. Later on, the «Trying private key» lines also indicate that no file was found. If a file existed, those lines would be «1» and «Offering public key», respectively:

$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/you/.ssh/id_rsa type 1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Offering RSA public key: /Users/you/.ssh/id_rsa

Verify the public key is attached to your account

You must provide your public key to GitHub to establish a secure connection.

  1. Open Terminal.

  2. Start SSH agent in the background.

    $ eval «$(ssh-agent -s)»

    Agent pid 59566

  3. Find and take a note of your public key fingerprint.

    $ ssh-add -l -E sha256

    2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA)

  4. In the upper-right corner of any github page, click your profile
    photo, then click Settings.

  5. In the user settings sidebar, click SSH and GPG keys.

  6. Compare the list of SSH keys with the output from the ssh-add
    command.

  7. If you don’t see your public key in GitHub, you’ll need to add your SSH key to GitHub to associate it with your computer.

Follow this link for details

answered Nov 30, 2021 at 0:45

exan's user avatar

exanexan

3,0465 gold badges23 silver badges37 bronze badges

I was using «WSL: Ubuntu» via VS code, closing my connection and then restarting worked for me.enter image description here

answered Oct 3, 2022 at 9:43

TheBadgerEB's user avatar

TheBadgerEBTheBadgerEB

4631 gold badge4 silver badges10 bronze badges

Can you try this approach,

echo -e "[network]ngenerateResolvConf = falsen" | sudo tee /etc/wsl.conf
echo -e "nameserver 8.8.8.8n" | sudo tee /etc/resolv.conf

Reference

answered Apr 13 at 5:42

Henshal B's user avatar

Henshal BHenshal B

1,42010 silver badges11 bronze badges

On centos 7

  1. check ip address using command ifconfig. you will get information like this

    ens33: flags=****<UP,BROADCAST,RUNNING,MULTICAST>  mtu ****
    
    inet ***.***.***.***  netmask ***.***.***.0  broadcast ***.***.***.***
    
  2. add inet ip address and hostname in file /etc/hosts

    a. open file vi /etc/hosts

    b. add ip and host like ip_address hostname hostname

  3. reboot

It will attach ip and host.

leopal's user avatar

leopal

4,6311 gold badge25 silver badges35 bronze badges

answered Jan 23, 2020 at 7:14

Birbal Sain's user avatar

I was having the same issue, tried all the methods nothing worked.

then realized my VPN was on.

if you too have a VPN turned on try switching off your VPN and retry.

I was using Cloudflare VPN.

answered Apr 25, 2022 at 14:33

Faizan Ul Haq's user avatar

Faizan Ul HaqFaizan Ul Haq

4522 gold badges7 silver badges23 bronze badges

I had the same output and turned out I had contradicting insteadof declarations in the .gitconfig (because I was working simultaneously on node and golang projects). You can manually delete the sections from the .gitconfig. Though I’ve created a script that cleans the url sections to be able to add the insteadof declarations on a cleaner config:

for windows (should be saved as .bat):

@echo off

setlocal

for /f "usebackq tokens=* delims=" %%# in (`git config --get user.name`)  do (
    set git_user=%%#
)

echo %git_user%

for /f "usebackq tokens=1,2 delims=] " %%a in (`findstr "[url " "%userprofile%.gitconfig" ^|find /V "%git_user%:" 2^>nul`) do (
     git config --global --remove-section url.%%b
)
endlocal

rem -- git config --global url.ssh://git@github.com/.insteadOf https://github.com/

bash:

#!/bin/bash

git_user=$(git config --get user.name)


git config --global -l | grep -v $git_user | grep url.| awk -F '.insteadof' '{print $1}' | while read -r url ; do
    echo $url
    git config --global --remove-section $url
done

And it will be a good idea .gitconfig to be backed up.

answered Aug 17, 2022 at 9:38

npocmaka's user avatar

npocmakanpocmaka

55k18 gold badges146 silver badges186 bronze badges

Steps to troubleshoot here

This happen with me on SourceTree
In my case, something wrong with my pair public-private key. So I delete this pair(usually store on /Users/username/.ssh) and generate new one(also add SSH on github Settings-> SSH and GPG keys manually if needed)

answered Nov 11, 2022 at 8:14

Huy Hóm Hỉnh's user avatar

enter image description here

I’ve the same issue of recent to solve this problem all you just need to do is to connect to stable internet once you’re connected to stable internet try again

Suraj Rao's user avatar

Suraj Rao

29.3k11 gold badges94 silver badges103 bronze badges

answered Sep 17, 2022 at 8:25

Abdulganiu's user avatar

2

If ping github.com gives below error:
ping: cannot resolve github.com: Unknown host

Kindly wait for couple of minutes and then try again.

answered Jan 13, 2022 at 18:39

Vipin Mittal's user avatar

You may get an error message when connecting to a machine that says ssh: Could not resolve hostname server: Name or service unknown. This might happen if you cannot connect wirelessly or don’t have enough signal strength for your device, but it can also occur because of typos in the command line prompt.

In this article, we’ll discuss how to fix it so you can reaccess your machine.

What is SSH?

Secure Shell (SSH) is an intermediate protocol between the application and transport layers of the Internet protocol stack. To create a connection to a remote shell, use the ssh command providing the username and hostname of the remote server.

Authentication uses public key cryptography, storing a private key in the user’s host. The error ssh: Could not resolve hostname is one of the most common errors encountered when using SSH.

How Does SSH Resolution With DNS?

To initiate a secure connection via SSH, a hostname must be resolved to an IP address. This can be done by providing the remote system’s IP address (e.g., 74.6.11.164) or its domain name (e.g., techcolleague.com).

A DNS server is used to translate the hostname into an IP address, and if this process fails, an error that reads “ssh could not resolve the hostname” may occur.

Additionally, systems will often store frequently-used domain names in their cache storage, so they don’t have to query a DNS server every time they attempt a request – but if this data is outdated or incorrect, it can lead to similar errors with SSH resolution.

Solution 1: Ping the Host

If you’re having difficulty connecting to a host via SSH, you should try pinging it using the ping command. If the host does not respond, it may be due to an issue on the server side.

If the host responds, there might be a network issue. Check if you can SSH to other hosts.

Solution 2: Check the Hostname

When trying to SSH to a server, verify your full command. You may be typing a command that the ssh program does not understand.

Here is an example of a command you may have run:

As you can see, the error is about not resolving the hostname myserver.

When you use the ssh program, the command you will use should be the following:

ssh [email protected]
ssh root@74.6.11.164

In the previous example, the user is root and hostname is myserver which is not a valid hostname. The correct example is shown you provide user as root and hostname as 74.6.11.164.

Solution 3: Fix the Hosts File

Hostname problems can also arise due to damage to the hosts file (i.e., /etc/hosts). Sometimes, ssh can give the same warnings for such errors as other issues. To edit the hosts file, root access is required.

Type the following command to open the file for editing:

sudo nano /etc/hosts

If you’re familiar with the vi program, you can also edit the file using vi by typing:

sudo vi /etc/hosts

The sudo prompt will ask for your password.

Make sure you have read and write access to the hosts file and look at the top of the file. You will need the following two lines for it to work correctly:

127.0.0.1 localhost
127.0.1.1 myhostname

Replace “myhostname” with your machine’s actual hostname. If you are working with an IPv6 network, you may also need these lines:

::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

In most cases, correctly setting the first two lines is sufficient for networks that use only IPv4 technology. However, modern Internet connectivity is quickly moving towards the IPv6 standard, so you may need to set these additional lines in the future.

Your Linux distribution should have configured these settings for you, but errors can occur due to an errant package or user mistakes that corrupt the hosts file and direct connections to the wrong location.

Solution 4: Add an Entry in /etc/hosts

You may have a scenario where you want to specify myserver as your hostname because you don’t want to type the IP address 74.6.11.164. In this case, you need to add an entry in /etc/hosts.

To make this change, open the /etc/hosts file and add myserver line to the end of the file such as the following example:

Once this entry has been saved in /etc/hosts, you can ssh to the server by typing:

ssh [email protected]

You should not be able to connect to your specified server.

Solution 5: Check the Hosts Directive

If the previous solution didn’t fix your issue, it may be the ordering of the hosts directive in your /etc/nsswitch.conf file.

Take a look at your /etc/nsswitch.conf. It should have this line:

hosts: files dns

With the above line, your Linux system will look at files first, which is /etc/hosts to resolve DNS. If it finds nothing there, it will query DNS in /etc/resolv.conf.

If your /etc/resolv.conf shows a different entry than the above, you may consider updating it and then connect to your server again.

Frequently Asked Questions

What Should Be The Proper Format For The SSH Command?

What Is The Hosts Directive In /etc/nsswitch.conf?

The hosts directive in /etc/nsswitch.conf determines the order in which the system should look for host resolution.

What Is The Role Of /etc/resolv.conf In Resolving The Hostname?

/etc/resolv.conf contains information about the DNS servers used by the system for host resolution.

Conclusion

This article explains the steps to resolve the error “ssh: Could not resolve hostname.” The error can be caused due to various reasons like incorrect hostname or command line prompt, missing entry in /etc/hosts, or incorrect ordering of the hosts directive in /etc/nsswitch.conf.

This article provides solutions to fix the issue. By following these steps, you can successfully connect to your machine.

SSH is the secure transmission network protocol that transfers the data files from one device to another (between local or remote servers) securely. This requires the correct hostname and IP address on both devices (client and server side) to create a secure SSH connection. The SSH hostname error occurs if the SSH credentials are not authenticated for the client and local server.

This post will provide all the reasons and possible solutions to fix the error. The content addressed in this post is:

  • Reason 1: Hostname is not Configured Correctly
  • Solution: Use the Correct SSH Hostname
  • Reason 2: Hosts File is not Configured
  • Solution: Configure /etc/hosts File

Let’s describe the causes and solutions for the SSH error.

Reason 1: Hostname is not Configured Correctly

SSH could not resolve hostname occurs when the hostname or ip address is incorrect like the hostname has extra space or some typing error while writing the hostname or ip address. For instance, when you write the incorrect hostname, the below error shows up:

This error can be removed using the two solutions described below.

Solution: Use the Correct SSH Hostname

The error for using the SSH protocol that the hostname can not be resolved can be removed using the correct hostname and ip address. The correct syntax of connecting an SSH connection is provided below:

$ ssh <host-name>@<ip-address>

 OR

$ ssh <ip-address> -l <host-name>

The IP-address can be found using “ip addr” command and hostname using the “hostname” command. For instance, to create an SSH connection with a server having username “itslinuxfoss” and ip address “192.168.18.18”, use the below command:

The SSH connection is successfully established.

Reason 2: Hosts File is not Configured

This error can occur if the hosts file is not configured or the hosts file is pointing toward the wrong SSH address. We can fix the SSH hostname could not be resolved error by manually adding the correct hostname and ip address in the “/etc/hosts” file.

Solution: Configure /etc/hosts File

Follow these steps to configure the host’s configuration file:

Open the system hosts configuration /etc/hosts in nano editor by using:

The above interface for the /etc/hosts file will open up, showing the hosts already exist in the system.

Add an entry for your desired IP address, and hostname in the /etc/hosts opened file, whose syntax is provided below:

  • <ip-addr>: Replace it with the remote server IP address.
  • <host-name>: Replace with system remote user name.

For instance, to create an SSH connection with the remote server having IP address “192.168.227.132” and username “itslinuxfoss”, add the below information in the /etc/hosts file:

$ 192.168.227.132 itslinuxfoss

After adding the remote user’s IP address and username, press the “Ctrl + O” to save the file changes. Keeping the same file name, press “Enter” and “Ctrl + X” to quit.

Create the SSH connection with the added remote server to verify the SSH connection is created using this command (Replace itslinuxfoss with your desired username):

Note: Enter “yes” to confirm creating the connection and enter the remote server password when asked.

The output shows the local server is successfully connected to the remote server (itslinuxfoss) via SSH.

Conclusion

The SSH could not resolve the hostname error that occurs if the hostname and ip address of local and client servers are incorrect. To fix the SSH hostname error, enter the correct server credentials or add the correct SSH entry in the “/etc/hosts” file as described in this guide.

As WooJoo stated, you need to tell it a valid server to connect to. If you wanted to use the form $ ssh root@server you can, but you would need to have server as an entry in /etc/hosts or your dns server (which is not the case or you would not have had an error), or an entry in a file called config located typically at /home/username/.ssh/config.

A sample /etc/hosts entry would look like:

# Sample /etc/hosts file
127.0.0.1    localhost
127.0.1.1    computerhostnamehere

10.0.2.15    server

and a sample /home/username/.ssh/config could be as simple as:

Host server
  HostName 10.0.2.15
  User root

This would get you the basic functionality you are looking for. There are many more options available for placing in the ~/.ssh/config file.

See man ssh for more options :)

Like this post? Please share to your friends:
  • Ошибка could not reset the direct3d device
  • Ошибка could not open install log file
  • Ошибка could not load xpcom
  • Ошибка could not load tls libraries
  • Ошибка could not load ssl library