48

After I switched from HTTPS to SSH for my repo then I received this error when pushing to origin master:

ssh: Could not resolve hostname git: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I also add my ssh in the gitlab. What should I do?

Majid Rajabi
  • 1,417
  • 6
  • 20
  • 35

15 Answers15

33

I was getting the same error.

This error typically occurs when there is an issue with the SSH configuration or the Git remote repository's hostname cannot be resolved.

Here, you can check your remote by running:

git remote -v

There are few reasons why this error occurs:

  • Incorrect Hostname
  • SSH Configuration
  • Network Connection
  • DNS Resolution
  • Firewall or Proxy

Typically, you can solve this by Change the [remote "origin"] URL value in .gitconfig or .config/git/config file

Previously:

https://git@github.com:userName/repo.git

OR

ssh://git@github.com:userName/repo.git

New:

git@github.com:userName/repo.git
DSDmark
  • 1,045
  • 5
  • 11
  • 25
Shivam Sharma
  • 1,277
  • 15
  • 31
19

HTTPS URLs provides an easy access to your repository, either private or public, even when you are working behind a firewall or proxy. And it is the recommended way.

On the other hand, SSH URLs use the secure ssh protocol in order to access a repository.

Coming back to your question, the error is likely because of improper configuration. The git remote add command is used to add a new remote to your repository, that you have already tried. However, switching from HTTPS to SSH url, means that your remote origin is already set to an http url and that you want to change.

Therefore, first check what url your current remote origin is referring to:

$ git remote -v

If it is referring to the HTTPS url, then you have to use

$ git remote set-url origin mySSH_url

command to change it to the SSH url.

Now, try git remote -v, it would display SSH urls configured for origin.

Do make sure that while working with SSH urls, you have generated and added the ssh key to the ssh-agent as well on GitLab/GitHub account.

Here is a very good article on how to change a remote's url.

Also, you can learn more about which remote url to use here.

Dhruv Joshi
  • 713
  • 7
  • 10
  • I try to delete the current HTTPS origin and then add a remote repository with SSH url. But I used your solution and it works too. thanks – Majid Rajabi Nov 03 '18 at 14:23
12

Well, in my case my local system was not connected to the VPN and hence was getting this error. So this could be one of the reasons apart from the above answers.

ABGR
  • 4,631
  • 4
  • 27
  • 49
  • Actually, I am also working with VPN, `git fetch` not working in terminal but I can connect to the private bitbucket server in the browser. – fuat Dec 07 '20 at 08:32
  • Funny/sad such an easy solution is not coming right away to our heads. This was also the case for me right now. – devaga Mar 16 '21 at 08:40
  • 1
    The same thing! Reconnect to VPN resolved the issue. Thanks – ModX May 17 '21 at 10:13
6

I faced the same issue when working on azure.

ssh: Could not resolve hostname ssh.abc.azure.com: Temporary failure in name 
resolution
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository 
exists.

Reason: This issue is due to the failure in WSL2.

Fix: Disable and enable the WSL.

  • it worked for me after installing sdk man – Anti Mafia Jun 30 '22 at 06:10
  • I had to turn the computer off and on again (using WSL), error definitely related to WSL – Matthias Sep 15 '22 at 15:06
  • 2
    Thanks. In my case, it's WSL issue. Usually getting this error every 2 months. I usually try with reconfiguring VPN, reboot the system. This time, I tried with restarting WSL service, it started working. – thulasi39 Sep 29 '22 at 10:49
2

While all the answers address the possibility of authentication failing with gitlab, the problem could also be DNS on your local system. For example:

$ ssh -Tvvv git@gitlab.com
debug2: resolving "gitlab.com" port 22
ssh: Could not resolve hostname gitlab.com: Name or service not known
$ ping gitlab.com
$ ping: gitlab.com: Name or service not known

This shows an issue with DNS settings on your computer. In RedHat, try this:

systemctl status NetworkManager
dnsmasq[2328]: nameserver 192.168.100.1 refused to do a recursive query

Oh my, that's a problem.

Is your network connection up?

$ ip a s
3: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 28:cd:c4:80:b8:0b brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.7/24 brd 192.168.1.255 scope global dynamic noprefixroute wlp2s0
       valid_lft 86127sec preferred_lft 86127sec
    inet6 fe80::dd85:ef7b:1632:5975/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

$ ip route
default via 192.168.1.1 dev wlp2s0 proto dhcp metric 600 

$ ip link
3: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DORMANT group default qlen 1000
    link/ether 28:cd:c4:80:b8:0b brd ff:ff:ff:ff:ff:ff

For me, yes. Try restarting the connection:

$ sudo ip link set dev wlp2s0 down
$ sudo ip link set dev wlp2s0 up

Now try running:

sudo systemctl status NetworkManager

And if the previous error went away, you can now ping gitlab.com.

git push -u origin master
Daniel Viglione
  • 8,014
  • 9
  • 67
  • 101
2

Answer for when you have upgraded from WSL version 1 to version 2.

For some reason github.com can't be resolved once that update happens. The issue has been raised against this ticket, there are also several solutions provided. The once that worked for me are: https://github.com/microsoft/WSL/issues/4285#issuecomment-522201021 https://github.com/microsoft/WSL/issues/4285#issuecomment-532962762 -> modified version of the first solution

At step 3 you are asked to write to file /etc/resolv.conf which was tricky since it was a symbolic link. Following these steps resolved the writing problem

Konstantin Grigorov
  • 1,356
  • 12
  • 20
1

For me it was a simple solution to a "dumb" problem:

After typing $ git push, I was prompted Are you sure you want to continue connecting (yes/no)?, and instead of just hitting enter, actually writing "yes" and then hitting enter solved it!

arilebedey
  • 31
  • 7
1

I even got this error when cloning a repository from github, using git clone https://github.com/user/repo. Now we have to use git clone https://git@github.com/user/repo

Tim Chaubet
  • 498
  • 1
  • 7
  • 15
0

I also found this error because I used backslashes "\" in the URL instead of slashes "/".

0

I also had the same problem when I was using git push -u origin branchA, but when I used the one recommend on bash git push --set-upstream origin branchA it worked out fine.

0

If you are using WSL2 and WireGuard, turn off WireGuard while committing.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 09 '23 at 16:57
0

since the error says Could not resolve hostname git then its likely a dns problem and I usually have that problem with Ubuntu WSL

you can test it by pinging any website like google.com

ping google.com

to solve this problem add the lines bellow to your (/etc/resolv.conf) file

nameserver 8.8.4.4
nameserver 8.8.8.8

dont delete anything from the (/etc/resolv.conf) file, just add these lines at the end, and you may need to give it write permissions first.

OULAHTAK
  • 431
  • 3
  • 12
0

I resolved this issue on WSL2 doing that:

  1. on wsl: open the /etc/resolv.conf and copy the servername auto-generated

  2. on wsl: editing /etc/wsl.conf adding the follow code:

[network]
generateResolvConf = false
  1. close all wsl windows and on powershell run wsl --shutdown

  2. open wsl terminal and create the file /etc/resolv.conf (it will not exists because it is not auto-generated anymore)

  3. put into /etc/resolv.conf the follow content:

nameserver 1.1.1.1 # cloudflare dns
nameserver 8.8.8.8 # google dns
nameserver PASTE_HERE_NAMESERVER_FROM_OLD_GENERATED_FILE # old dns generated by wsl
  1. close all wsl windows and on powershell run wsl --shutdown

  2. open wsl terminal and it should work now

0

In my case,

ssh-add <ssh-key-path>

Solved the issue.

0

I had the same error. It was something wrong with DNS server. The following command helped:

sudo systemctl restart systemd-resolved