Majid Rajabi
Majid Rajabi

Reputation: 1755

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

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?

Upvotes: 72

Views: 307034

Answers (21)

Shivam Sharma
Shivam Sharma

Reputation: 1597

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 or .git/config/ file

Previously:

https://[email protected]:userName/repo.git

OR

ssh://[email protected]:userName/repo.git

New:

[email protected]:userName/repo.git

Upvotes: 45

Yevhenii Ostash
Yevhenii Ostash

Reputation: 41

After a FEW YEARS getting this error - there are a few ways(Linux&Windows):

I. Try to use git bash(with admin rules)(if you Windows user, or just a terminal on Linux)

  • eval "$(ssh-agent -s)"

  • ssh-add path-to-your-ssh-key(you should create it)

  • Note: connect to VPN if needed.

II. If it didn't work - it could be a DNS problem

  1. enter the ipconfig /flushdns to any terminal/console
  2. reboot the PC
  3. try to pull/push; if it doesn't work - go through steps from part I.

If the ssh key is correct, you are connected to VPN(if needed), the ssh-key not expired, registered in github/gitlab etc., and you did all these steps - and it didn't work then it can be the DNS problem, a problem with your router, internet provider.

Upvotes: 0

Tms91
Tms91

Reputation: 4154

I have encoutered the same error message.

The issue was that I was trying to git clone a project by using the https link

https://github.com/my_user/my_project.git

I solved it by using the ssh link instead

[email protected]:my_user/my_project.git

Upvotes: 0

Tms91
Tms91

Reputation: 4154

I have encoutered the same error message.

In my case the problem was that my user was not in the authorized members of my git repo.

Upvotes: 0

Kacper
Kacper

Reputation: 104

The issue:

git remote add REMOTENAME ssh://[email protected]:username/reponame.git
git fetch REMOTENAME
< the error >

The fix (add remote without ssh://):

git remote remove REMOTENAME
git remote add REMOTENAME [email protected]:username/reponame.git

Upvotes: 0

Gustavo Duregger
Gustavo Duregger

Reputation: 71

I had the same problem, close my IDE completely and open it again to solve it

Upvotes: 0

Benyamin Jafari
Benyamin Jafari

Reputation: 33986

I had the same issue.

To Persian guys, who are using Shecan's DNS, I would have to say that, apparently, there's an issue with it. Just disable it!

Upvotes: 0

Mykola Gerasymenko
Mykola Gerasymenko

Reputation: 549

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

sudo systemctl restart systemd-resolved

Upvotes: 1

Purushottam Nawale
Purushottam Nawale

Reputation: 349

In my case,

ssh-add <ssh-key-path>

Solved the issue.

Upvotes: 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

Upvotes: 1

OULAHTAK
OULAHTAK

Reputation: 501

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.

Upvotes: 2

NetLight
NetLight

Reputation: 1

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

Upvotes: 0

Tim Chaubet
Tim Chaubet

Reputation: 726

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://[email protected]/user/repo

Upvotes: 1

Konstantin Grigorov
Konstantin Grigorov

Reputation: 1602

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

Upvotes: 2

Gideon Oba
Gideon Oba

Reputation: 1

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.

Upvotes: 0

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

Upvotes: 0

Santhiya Gunasekaran
Santhiya Gunasekaran

Reputation: 221

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.

Upvotes: 9

Daniel Viglione
Daniel Viglione

Reputation: 9407

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 [email protected]
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

Upvotes: 4

arilebedey
arilebedey

Reputation: 31

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!

Upvotes: 1

ABGR
ABGR

Reputation: 5205

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.

Upvotes: 18

thepoetdj
thepoetdj

Reputation: 773

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.

Upvotes: 23

Related Questions