140

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
  • 3,683
  • 4
  • 33
  • 41
Neel Pathak
  • 1,451
  • 2
  • 10
  • 5

19 Answers19

134

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
  • 15,395
  • 32
  • 113
  • 196
Qiangks
  • 1,341
  • 1
  • 8
  • 3
  • 1
    @JsonBruce and Fahmi: to know more about it why it worked read how DNS works, just to give you heads up, when you make any request it first checks the local cache for the "IP resolution" but if it's not there then it tries to resolve from nameserver configured in your /etc/resolv.conf (in case of UNIX/ Linux). Check https://en.wikipedia.org/wiki/Name_server – Vishrant Jul 02 '17 at 17:19
  • @FahmiRamadhan I believe that when github enables 3rd party DDOS protection their dns records and/or servers will change, above actions will flush your dns records, pointing you to the DDOS protected servers. (I've added this to my answer) – Jens Timmerman May 07 '18 at 15:25
  • 4
    adding nameservers to `/etc/resolv.conf` fixed the issue for me! Before it had only the router nameserver, which was apparantly not working correctly. – Adrian Jan 09 '21 at 13:23
  • 1
    Resetting device Network works for me – Yogesh Borkhade Nov 26 '21 at 05:29
  • faced it on ububtu 22.04 vm. reboot solved it. – Sushil Jun 03 '22 at 04:04
65

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.

Todd
  • 2,824
  • 2
  • 29
  • 39
  • 4
    Getting issues even after executing this command $ ssh -T git@github.com git@github.com: Permission denied (publickey). – Rahul Anand Sep 26 '20 at 05:36
  • 1
    I was able to succeed in testing my connection, but fetch still failed with OP's error. Turns out I had added ssh:// protocol to the SSH repo URL that GitHub gave me, and this caused it to fail. Removing the old origin and re-adding using the exact SSH URL from GitHub fixed it. – CCJ Jan 05 '22 at 00:09
  • 3
    `ssh: Could not resolve hostname github.com: nodename nor servname provided, or not known` – doublejosh Mar 18 '22 at 07:20
  • 2
    this fixed my issue stat! thank you! – alchemy Apr 13 '22 at 23:18
22

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 ;)

Jens Timmerman
  • 9,316
  • 1
  • 42
  • 48
6

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

Carlos
  • 3,480
  • 1
  • 16
  • 14
5

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

  • literally what is the "git@github.com", what should it be, give an example, there are email address, users names, repos that can be added in this infinite "git@github.com". This is typical github, you need to read their minds. – Brian Wiley Aug 13 '21 at 21:53
  • Hi @BrianWiley, git@github.com is the email Github has provided for users to check if your generated SSH connection between your local machine and Github is working. – Onyejiaku Theodore Kelechukwu Aug 14 '21 at 17:40
  • thanks i realized that since there was no <> you literally just type that – Brian Wiley Aug 14 '21 at 22:36
  • I am online as I am reading this, and I still have the error message while doing `ssh -T git@github.com`. This answer didn't helped me – Dimitri Kopriwa Nov 08 '21 at 09:08
4

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

Skylar Brown
  • 3,234
  • 2
  • 14
  • 7
2

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.

Khaled Adrani
  • 171
  • 3
  • 7
1

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.

Namrata
  • 11
  • 2
1

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

exan
  • 3,236
  • 5
  • 24
  • 38
1

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

TheBadgerEB
  • 473
  • 1
  • 4
  • 10
1

Can you try this approach,

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

Reference

Henshal B
  • 1,540
  • 12
  • 13
0

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
  • 4,711
  • 1
  • 25
  • 35
0

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.

Faizan Ul Haq
  • 454
  • 2
  • 7
  • 23
0

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.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

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)

Huy Hóm Hỉnh
  • 597
  • 7
  • 18
0

If anyone is using Windows Subsystem for Linux (WSL), then your distro might have lost connection.

Following these steps from another StackOverflow post helped my case: https://stackoverflow.com/a/63578387/6950666

Muhammet Balsoy
  • 103
  • 1
  • 8
0

For me this resolved the issue: sudo systemctl restart networking

Yauheni Leaniuk
  • 418
  • 1
  • 6
  • 15
-1

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
  • 29,388
  • 11
  • 94
  • 103
  • Please don't provide screenshots and replace it with text only. – izlin Sep 22 '22 at 09:37
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32739800) – Rager Sep 22 '22 at 22:22
-4

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

Kindly wait for couple of minutes and then try again.

Vipin Mittal
  • 370
  • 2
  • 6