Should the sudo
command or elevated privileges be used with Git?
You should not be using the sudo
command or elevated privileges, such as administrator permissions, with Git. If you have a very good reason you must use sudo
, then ensure you are using it with every command (it’s probably just better to use su
to get a shell as root at that point). If you generate SSH keys without sudo
and then try to use a command like sudo git push
, you won’t be using the same keys that you generated.
Check that you are connecting to the correct server
Typing is hard, we all know it. Pay attention to what you type; you won’t be able to connect to «githib.com» or «guthub.com». In some cases, a corporate network may cause issues resolving the DNS record as well.
To make sure you are connecting to the right domain, you can enter the following command:
$ ssh -vT git@github.com
> OpenSSH_8.1p1, LibreSSL 2.7.3
> debug1: Reading configuration data /Users/YOU/.ssh/config
> debug1: Reading configuration data /etc/ssh/ssh_config
> debug1: /etc/ssh/ssh_config line 47: Applying options for *
> debug1: Connecting to github.com port 22.
The connection should be made on port 22, unless you’re overriding settings to use SSH over HTTPS.
Always use the «git» user
All connections, including those for remote URLs, must be made as the «git» user. If you try to connect with your GitHub username, it will fail:
$ ssh -T GITHUB-USERNAME@github.com
> Permission denied (publickey).
If your connection failed and you’re using a remote URL with your GitHub username, you can change the remote URL to use the «git» user.
You should verify your connection by typing:
$ ssh -T git@github.com
> Hi USERNAME! You've successfully authenticated...
Make sure you have a key that is being used
- Open TerminalTerminalGit Bash.
- Verify that you have a private key generated and loaded into SSH.
# start the ssh-agent in the background $ eval "$(ssh-agent -s)" > Agent pid 59566 $ ssh-add -l -E sha256 > 2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA)
If you have GitHub Desktop installed, you can use it to clone repositories and not deal with SSH keys.
-
If you are using Git Bash, turn on ssh-agent:
# start the ssh-agent in the background $ eval "$(ssh-agent -s)" > Agent pid 59566
If you are using another terminal prompt, such as Git for Windows, turn on ssh-agent:
# start the ssh-agent in the background $ eval $(ssh-agent -s) > Agent pid 59566
-
Verify that you have a private key generated and loaded into SSH.
$ ssh-add -l -E sha256 > 2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA)
- Open TerminalTerminalGit Bash.
- Verify that you have a private key generated and loaded into SSH.
$ ssh-add -l -E sha256 > 2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA)
The ssh-add
command should print out a long string of numbers and letters. If it does not print anything, you will need to generate a new SSH key and associate it with GitHub.
Tip: On most systems the default private keys (~/.ssh/id_rsa
and ~/.ssh/identity
) are automatically added to the SSH authentication agent. You shouldn’t need to run ssh-add path/to/key
unless you override the file name when you generate a key.
Getting more details
You can also check that 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.
-
Open Terminal.
-
Start SSH agent in the background.
$ eval "$(ssh-agent -s)" > Agent pid 59566
-
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)
-
In the upper-right corner of any page, click your profile photo, then click Settings.
-
In the «Access» section of the sidebar, click SSH and GPG keys.
-
Compare the list of SSH keys with the output from the
ssh-add
command.
-
Open the command line.
-
Start SSH agent in the background.
$ ssh-agent -s > Agent pid 59566
-
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)
-
In the upper-right corner of any page, click your profile photo, then click Settings.
-
In the «Access» section of the sidebar, click SSH and GPG keys.
-
Compare the list of SSH keys with the output from the
ssh-add
command.
-
Open Terminal.
-
Start SSH agent in the background.
$ eval "$(ssh-agent -s)" > Agent pid 59566
-
Find and take a note of your public key fingerprint. If you’re using OpenSSH 6.7 or older:
$ ssh-add -l > 2048 a0:dd:42:3c:5a:9d:e4:2a:21:52:4e:78:07:6e:c8:4d /Users/USERNAME/.ssh/id_rsa (RSA)
If you’re using OpenSSH 6.8 or newer:
$ ssh-add -l -E md5 > 2048 MD5:a0:dd:42:3c:5a:9d:e4:2a:21:52:4e:78:07:6e:c8:4d /Users/USERNAME/.ssh/id_rsa (RSA)
-
In the upper-right corner of any page, click your profile photo, then click Settings.
-
In the «Access» section of the sidebar, click SSH and GPG keys.
-
Compare the list of SSH keys with the output from the
ssh-add
command.
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.
Warning: If you see an SSH key you’re not familiar with on GitHub, delete it immediately and contact GitHub Support, for further help. An unidentified public key may indicate a possible security concern. For more information, see «Reviewing your SSH keys.»
If the user has not generated a ssh public/private key pair set before
This info is working on theChaw but can be applied to all other git repositories which support SSH pubkey authentications. (See [gitolite][1], gitlab or github for example.)
First start by setting up your own public/private key pair set. This
can use either DSA or RSA, so basically any key you setup will work.
On most systems you can use ssh-keygen.
- First you’ll want to cd into your .ssh directory. Open up the terminal and run:
cd ~/.ssh && ssh-keygen
- Next you need to copy this to your clipboard.
- On OS X run:
cat id_rsa.pub | pbcopy
- On Linux run:
cat id_rsa.pub | xclip
- On Windows (via Cygwin/Git Bash) run:
cat id_rsa.pub | clip
- On Windows (Powershell) run:
Get-Content id_rsa.pub | Set-Clipboard
(Thx to @orion elenzil)- Add your key to your account via the website.
- Finally setup your .gitconfig.
git config --global user.name "bob"
git config --global user.email bob@...
(don’t forget to restart your command line to make sure the config is reloaded)That’s it you should be good to clone and checkout.
Further information can be found at https://help.github.com/articles/generating-ssh-keys (thanks to @Lee Whitney)
[1]: https://github.com/sitaramc/gitolite
—
If the user has generated a ssh public/private key pair set before
- check which key have been authorized on your github or gitlab account settings
- determine which corresponding private key must be associated from your local computer
eval $(ssh-agent -s)
- define where the keys are located
ssh-add ~/.ssh/id_rsa
answered Apr 15, 2010 at 7:59
25
More extensive troubleshooting and even automated fixing can be done with:
ssh -vT git@github.com
Alternatively, according to below comments, we could issue:
ssh -vT git@gitlab.com
or substitute gitlab/github with whatever Git Instance your organisation is running.
Source: https://help.github.com/articles/error-permission-denied-publickey/
answered Dec 22, 2011 at 22:30
16
This error can happen when you are accessing the SSH URL (Read/Write) instead of Git Read-Only URL but you have no write access to that repo.
Sometimes you just want to clone your own repo, e.g. deploy to a server. In this case you actually only need READ-ONLY access. But since that’s your own repo, GitHub may display SSH URL if that’s your preference. In this situation, if your remote host’s public key is not in your GitHub SSH Keys, your access will be denied, which is expected to happen.
An equivalent case is when you try cloning someone else’s repo to which you have no write access with SSH URL.
In a word, if your intent is to clone-only a repo, use HTTPS URL (https://github.com/{user_name}/{project_name}.git
) instead of SSH URL (git@github.com:{user_name}/{project_name}.git
), which avoids (unnecessary) public key validation.
Update: GitHub is displaying HTTPS as the default protocol now and this move can probably reduce possible misuse of SSH URLs.
answered May 9, 2013 at 15:14
kavinyaokavinyao
2,9032 gold badges19 silver badges23 bronze badges
5
The github help link helped me sort out this problem. Looks like the ssh key was not added to the ssh-agent. This is what I ended up doing.
Command 1:
Ensure ssh-agent is enabled. The command starts the ssh-agent in the background:
eval "$(ssh-agent -s)"
Command 2:
Add your SSH key to the ssh-agent:
ssh-add ~/.ssh/id_rsa
answered Nov 29, 2015 at 15:09
jarorajarora
5,2342 gold badges34 silver badges46 bronze badges
6
Got the same error report.
Fixed with using the HTTPS instead of the SSH protocol. Since I don’t want to set «SSH keys» for a test PC.
Change URL to HTTPS when clone:
git clone https://github.com/USERNAME/REPOSITORY.git
My problem is a little bit different: I have the URL set to SSH when adding an existing local repo to remote, by using:
git remote add origin ssh://github.com/USERNAME/REPOSITORY.git
To fix it, reset the URL to HTTPS:
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
BTW, you may check your URL using the command:
git remote -v
origin https://github.com/USERNAME/REPOSITORY.git (fetch)
origin https://github.com/USERNAME/REPOSITORY.git (push)
Hope this will help some one like me.
answered Jan 3, 2018 at 23:58
Robina LiRobina Li
1,0887 silver badges4 bronze badges
2
Another possibility on Windows, which is not covered in any of these answers, and is not covered in the git or github docs on troubleshooting:
git may be using a different openssh executable than you think it is.
I was receiving the Permission denied (public key)
error when trying to clone or pull from github and ssh.dev.azure.com, and I’d followed all the instructions and verified that my SSH keys were setup correctly (from SSH’s standpoint) using ssh -vT git@github.com
and ssh -vT git@ssh.dev.azure.com
. And was still getting these errors:
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I eventually figured out that the problem is that Git for Windows, and Windows, both have their own versions of openssh. This is documented here: https://github.com/desktop/desktop/issues/5641
I was relying on the Windows ssh-agent service to store my ssh key passphrases, so git (with it’s separate version of openssh) couldn’t read my private keys. I consider it a bug that this error message is used — it’s misleading.
The fix was:
git config --global core.sshCommand "'C:WindowsSystem32OpenSSHssh.exe'"
Or in your ~/.gitconfig:
[core]
sshCommand = 'C:\Windows\System32\OpenSSH\ssh.exe'
Perhaps this will be fixed in git for Windows soon, but this is the 2nd time I’ve wasted time on this issue.
answered Apr 11, 2020 at 20:43
crimbocrimbo
9,9486 gold badges50 silver badges55 bronze badges
7
I was struggling with the same problem that’s what I did and I was able to clone the repo. I followed this procedure for Mac.
First Step: Checking if we already have the public SSH key.
- Open Terminal.
- Enter
ls -al ~/.ssh
to see if existing SSH keys are present:
Check the directory list to see if you already have a public SSH key. Default public is one of the following d_dsa.pub, id_ecdsa.pub, id_ed25519.pub, id_rsa.pub.
If you don’t find then go to step 2 otherwise follow step 3
Step 2: Generating public SSH key
- Open Terminal.
- Enter the following command with a valid email address that you use for github
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- You will see the following in your terminal
Generating public/private rsa key pair
. When it prompts to"Enter a file in which to save the key,"
press Enter. This accepts the default file location. When it prompts toEnter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
Just press enter again. - At the prompt,
"Type a secure passphrase. Enter passphrase (empty for no passphrase): [Type a passphrase]"
press enter if you don’t want toEnter same passphrase again: [Type passphrase again]
press enter again
This will generate id_rsa.pub
Step 3: Adding your SSH key to the ssh-agent
-
Interminal type
eval "$(ssh-agent -s)"
-
Add your SSH key to the ssh-agent. If you are using an existing SSH key rather than generating a new SSH key, you’ll need to replace id_rsa in the command with the name of your existing private key file. Enter this command
$ ssh-add -K ~/.ssh/id_rsa
-
Now copy the SSH key and also add it to you github account
-
In terminal enter this command with your ssh file name
pbcopy < ~/.ssh/id_rsa.pub
This will copy the file to your clipboard
Now open you github account Go to Settings > SSH and GPG keys > New SSH key Enter title and paste the key from clipboard and save it. Voila you’re done.
answered Mar 7, 2017 at 6:28
Zeeshan ShabbirZeeshan Shabbir
6,5244 gold badges35 silver badges73 bronze badges
6
This works for me:
ssh-add ~/.ssh/id_rsa
answered Jan 7, 2017 at 12:17
1
Visual guide (Windows)
1 of 2. Git batch side
1.1. Open git batch (Download her)
1.2. Paste the text below (Change to your GitHub account email)
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
1.3. Press Enter (Accepts the default file location)
1.4. Click Enter Twice (Or set SSH key passphrases — Gitbub passphrases docs)
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
1.5. The key generate:
Your identification has been saved in /c/Users/user/.ssh/id_rsa…
1.6. Copy the SSH key to your clipboard.
$ clip < ~/.ssh/id_rsa.pub
2 of 2. Github website user side
Under user setting
SSH and GPG keys
=> New SSH key:
Paste the code from step 1.6
Done
If someone doesn’t want to use SSH use HTTPS
:
Github docs: https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh
answered Jul 21, 2020 at 21:43
Ezra SitonEzra Siton
6,3292 gold badges21 silver badges34 bronze badges
0
If your problem appears out of the blue recently (the latter half of 2021), it may have been caused by incompatible hash algorithms.
As of this post (Oct 2021), the latest version of Git for windows is 2.33.1 (release note), who has embraced the latest OpenSSH 8.8p1 (release note), who in turn has deprecated SHA-1. Meanwhile, if your remote Git repository still sticks to SHA-1, you’ll fail the authentication.
To see whether you could have fallen into this case, check the version of your software by:
ssh -V
git --version
Then you should check the «Potentially-incompatible changes» section of OpenSSH 8.8/8.8p release note.
TL;DR
Solution 1: Enable SHA-1 again by adding this to your ~/.ssh/config
file:
Host <remote>
HostkeyAlgorithms +ssh-rsa
PubkeyAcceptedAlgorithms +ssh-rsa
Remember to replace <remote>
with the hostname of your remote repository.
Solution 2: Regenerate your key pair using ECDSA or Ed25519, instead of RSA. For example:
ssh-keygen -t ecdsa -C <comment>
Remember to replace <comment>
with your own mnemonic phrase. Then, upload the generated public key to your remote repository.
FYI, I encountered this prompt message when accessing Gitee.com, who uses golang.org/x/crypto/ssh
on their server and has posted a page on this issue here (in Mandarin).
git@gitee.com: Permission denied (publickey).
answered Oct 25, 2021 at 6:37
wtjwtj
4314 silver badges4 bronze badges
1
Note that (at least for some projects) you must have a github account with an ssh key.
Look at the keys listed in your authentication agent (ssh-add -l)
(if you don’t see any, add one of your existing keys with ssh-add /path/to/your/key (eg: ssh-add ~/.ssh/id_rsa))
(if you don’t have any keys, first create one. See: http://rcsg-gsir.imsb-dsgi.nrc-cnrc.gc.ca/documents/internet/node31.html or just google ssh-keygen)
To verify that you have a key associated with your github account:
Go to: https://github.com/settings/ssh
You should see at least one key with a hash key matching one of the hashes you saw when you typed ssh-add -l just a minute ago.
If you don’t, add one, then try again.
answered Jan 17, 2013 at 20:13
Mason BryantMason Bryant
1,35214 silver badges23 bronze badges
1
I met the same issue because of I was thought the difference between SSH and HTTPS is
https://github.com/USERNAME/REPOSITORY.git
ssh://github.com/USERNAME/REPOSITORY.git
So I changed from HTTPS to SSH just by changing https://
to ssh://
nothing on the end of the url was changed.
But the truth is:
https://github.com/USERNAME/REPOSITORY.git
git@github.com:USERNAME/REPOSITORY.git
Which means I changed ssh://github.com/USERNAME/REPOSITORY.git
to git@github.com:USERNAME/REPOSITORY.git
it works.
Stupid error but hope helps someone!
Amir
8,7217 gold badges44 silver badges48 bronze badges
answered Jul 28, 2016 at 3:53
William HuWilliam Hu
15k10 gold badges99 silver badges116 bronze badges
3
These are the steps I followed in windows 10
-
Open Git Bash.
-
Generate Public Key:
ssh-keygen -t rsa -b 4096 -C "youremailaddress@xyz.com"
-
Copy generated key to the clipboard (works like CTRL+C)
clip < ~/.ssh/id_rsa.pub
-
Browser, go to Github => Profile=> Settings => SSH and GPG keys => Add Key
-
Provide the key name and paste clipboard (CTRL+V).
-
Finally, test your connection (Git bash)
ssh -T git@github.com
Thanks!
answered Mar 21, 2020 at 15:12
1
Please try this if nothing is worked out
- Generate personal Access token (
Setting -> Developer settings -> Personal access tokens -> Generate new token
) git remote set-url origin https://<TOEKN>@github.com/USERNAME/REPOSITORY.git
Note: If a password popup comes, try to enter the token only (try twice)
answered Dec 20, 2021 at 5:06
GopalGopal
2,07323 silver badges17 bronze badges
1
I had a slight different situation, I was logged on to a remote server and was using git on the server, when I ran any git command I got the same message
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
The way I fixed it was by changing the file /etc/ssh_config on my Mac.
from
ForwardAgent no
to
ForwardAgent yes
answered Dec 17, 2013 at 0:43
RichipalRichipal
7211 gold badge9 silver badges11 bronze badges
2
I had to copy my ssh keys to the root folder.
Google Cloud Compute Engine running Ubuntu 18.04
sudo cp ~/.ssh/* /root/.ssh/
answered Dec 2, 2019 at 18:56
KandarpKandarp
7936 silver badges8 bronze badges
0
Solution using gh
i.e. Github’s official CLI
gh installation
brew install gh
gh login or authentication via cli
gh auth login
repo clone
gh repo clone <username or orgname>/<repo-name>
Example: gh repo clone keshavdulal/sample-repo
Rant: I too was bashing my head when git clone
suddenly decided not to work anymore and I don’t have the patience or brainpower to relearn ssh/public keys/cryptography from scratch just to clone a freaking repo I already have access to. Also surprised no one mentioned gh
in the answers yet
answered Oct 5, 2021 at 7:16
KeshavDulalKeshavDulal
2,56726 silver badges28 bronze badges
Guys this is how it worked for me:
- Open terminal and go to user [See attached image]
- Open .ssh folder and make sure it doesn’t have any file like id_rsa or id_rsa.pub otherwise sometimes it wont properly rewrite files
- git —version [Check for git installation and version]
- git config —global user.email «your email id»
- git config —global user.name «your name»
- git config —list [make sure you have set your name & email]
- cd ~/.ssh
- ssh-keygen, it prompts for saving file, allow it
- cat ~/.ssh/id_rsa.pub [Access your public key & copy the key to gerrit settings]
Note: You should not be using the sudo command with Git. If you have a very good reason you must use sudo, then ensure you are using it with every command (it’s probably just better to use su to get a shell as root at that point). If you generate SSH keys without sudo and then try to use a command like sudo git push, you won’t be using the same keys that you generated
Fabian Lauer
8,2504 gold badges27 silver badges35 bronze badges
answered May 18, 2016 at 3:40
1
Are you in a corporate environment? Is it possible that your system variables have recently changed? Per this SO answer, ssh keys live at %HOMEDRIVE%%HOMEPATH%.sshid_rsa.pub
. So if %HOMEDRIVE%
recently changed, git doesn’t know where to look for your key, and thus all of the authentication stuff.
Try running ssh -vT git@github.com
. Take note of where the identity file
is located. For me, that was pointing not to my normal UsersMyLogin
but rather to a network drive, because of a change to environment variables pushed at the network level.
The solution? Since my new %HOMEDRIVE%
has the same permissions as my local files, I just moved my .ssh folder there, and called it a day.
answered May 30, 2014 at 17:37
AndrewAndrew
8,9418 gold badges45 silver badges58 bronze badges
1
I hit this error because I needed to give my present working directory permissions 700:
chmod -R 700 /home/ec2-user/
answered Jun 29, 2018 at 17:33
duhaimeduhaime
24.6k15 gold badges163 silver badges209 bronze badges
1
On Windows, make sure all your apps agree on HOME. Msys will surprisingly NOT do it for you. I had to set an environment variable because ssh and git couldn’t seem to agree on where my .ssh directory was.
answered Nov 7, 2012 at 20:58
JasonJason
2,9051 gold badge22 silver badges25 bronze badges
One of the easiest way
go to terminal-
git push <Git Remote path> --all
answered Nov 6, 2013 at 6:04
VizllxVizllx
9,0451 gold badge40 silver badges78 bronze badges
The basic GIT instructions did not make a reference to the SSH key stuff. Following some of the links above, I found a git help page that explains, step-by-step, exactly how to do this for various operating systems (the link will detect your OS and redirect, accordingly):
http://help.github.com/set-up-git-redirect/
It walks through everything needed for GITHub and also gives detailed explanations such as «why add a passphrase when creating an RSA key.» I figured I’d post it, in case it helps someone else…
answered May 2, 2012 at 19:36
gMalegMale
16.5k17 gold badges91 silver badges115 bronze badges
The easiest solution to this, when you are trying to push to a repository with a different username is:
git remote set-url origin https://USERNAME@github.com/USERNAME/PROJECTNAME.git
answered Nov 20, 2016 at 21:28
Nizar B.Nizar B.
3,0689 gold badges38 silver badges56 bronze badges
If you have more than one key you may need to do
ssh-add private-keyfile
answered Jul 3, 2016 at 0:08
keioskeios
4524 silver badges9 bronze badges
1
Its pretty straight forward. Type the below command
ssh-keygen -t rsa -b 4096 -C "youremailid@yourdomain.com"
Generate the SSH key. Open the file and copy the contents. Go to GitHub setting page , and click on SSH key . Click on Add new SSH key, and paste the contents here. That’s it You shouldn’t see the issue again.
answered Aug 23, 2016 at 5:42
karthik339karthik339
1891 silver badge6 bronze badges
I deleted node_modules/ package-lock.json and yarn.lock files. Ran npm i
again. This resolved the issue for me.
answered Dec 16, 2021 at 11:59
The «Permission denied» error means that the server rejected the connection. In this tutorial, you will explore how to solve this problem.
If the user has not generated an SSH public/private key pair set, firstly, set up your own public/private key pair set. On most systems, you can use ssh-keygen.
Setting up a public/private key pair set
First, cd into your .ssh directory by running the following into the terminal:
Copying to clipboard
Secondly, copy the following to your clipboard:
On Linux execute:
On OS X execute:
On Windows (via Cygwin/Git Bash) execute:
cat id_rsa.pub | pbcopycat id_rsa.pub | clip
On Windows (via Cygwin/Git Bash) execute:
Adding key to account
The next step is adding your key to your account via the website.
- Set up git config:
git config --global user.name "w3docs" git config --global user.email [email protected]
- Restart the command line to be sure that the config is reloaded.
If the user has generated an ssh public/private key pair set, then you should follow the steps below.
Checking keys
Check which key have been authorized on your github account settings.
Discovering private key
Determine which private key must be associated from your local computer:
Specifying the location
Define the location of keys:
Then, you can add the SSH key to your account.
Using public key authentication instead of simple passwords is the question of security. The power of public key authentication lies in the provision of cryptographic strength that even long passwords can not offer. The security of public key authentication with SSH free users from remembering long passwords or write them down.
SSH keys are an access credential used in SSH protocol (Secure Shell). A secure shell network protocol helps to log in from one computer to another securely, as well as to manage networks, operating systems, and configurations. The SSH keys are created using the keygen program. The ssh-keygen is a tool for creating new authentication key pairs for SSH. The key pairs are used for automating logins, single sign-on, and for authenticating hosts.
The keys come in pairs, and each of them is composed of a public key and a private key.
- Public keys (or authorized keys), determine who can access each system.
- Private keys (or identity keys) identify users and give them access.
The SSH keys are nearly the same as passwords, but it is more secure to use SSH Keys to log in a server because it is almost impossible to decrypt.
Home » Git Permission Denied Public Key Quick Fix
Last updated on May 29, 2022 by
In this tutorial, we will see how to solve the Git permission denied public key error. If you try to clone the Github repository or run any Git commands on the Github repository, you might get this error. Let’s see why it’s causing this error and its solution?
Why Git Permission Denied Public Key Error Occurred?
The Git permission denied public key error is usually caused by one of the following three issues:
- You have used an incorrect email address in the GitHub SSH URL
- You have not configured your public SSH key in your GitHub account
- You must create GitHub SSH keys to be used by the secure shell
Solution 01 Generate Github SSH Key
SSH Key is a Secure Shell protocol suite found on Unix, Unix-like, and Microsoft Windows computer systems used to establish secure shell sessions between remote computers over insecure networks.
Let’s generate an SSH key in your PC.
- Open the Git Bash terminal or your preferred terminal.
- Check if
.ssh
folder is present or not in/home/<user>/
directory. If folder does not exists then create it by using the following command.
mkdir -p .ssh chmod 0700 .ssh
- Enter the following command with a valid email address that you use for GitHub
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- You will see the following in your terminal Generating public/private rsa key pair. When it prompts to
"Enter a file in which to save the key (/home/<your username>/.ssh/id_ras)"
press Enter. This accepts the file name. - At the prompt,
"Enter passphrase (empty for no passphrase): [Type a passphrase]"
press enter if you don’t want toEnter the same passphrase again: [Type passphrase again]
press enter again. - Two new files will be created in the
.ssh
folder namedid_rsa.pub
&id_rsa
- Now run the below command to read the file so that we can copy the content and paste it into the GitHub account and we are done.
cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQB/nAmOjTmezNUDKYvEeIRf2YnwM9 /uUG1d0BYsc8/tRtx+RGi7N2lUbp728MXGwdnL9od4cItzky /zVdLZE2cycOa18xBK9cOWmcKS0A8FYBxEQWJ/q9YVUgZbFKfYGaGQxsER+A0w /fX8ALuk78ktP31K69LcQgxIsl7rNzxsoOQKJ/CIxOGMMxczYTiEoLvQhapFQMs3FL 96didK/QbrfB1WT6s3838SEaXfgZvLef1YB2xmfhbT9OXFE3FXvh2UPBfN+ffE7iiay Qf/2XR+8j4N4bW30DiPtOQLGUrH1y5X/rpNZNlWW2+jGIxqZtgWg7lTy3mXy5x8 36Sj/6Lyouremail@example.com
- Copy the SSH key and go to your
GitHub account
>Settings
>SSH and GPG keys
and paste it. Add title as you want for ex. My PC SSH Key
That’s it. Now, just try to clone the GitHub repo or perform any actions you want to perform.
Solution 02 Use HTTPS Instead Of SSH Protocol
We suggest using the first solution, but if you would like to go with an alternate solution then you can follow this.
So use the HTTPS instead of the SSH protocol to quick fix the Git permission denied public key error.
- Change URL to HTTPS while cloning:
git clone https://github.com/USERNAME/REPOSITORY.git
- If you already have cloned repository on your machine then you can reset SSH Github URL to HTTPS Github URL by running the below command.
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
- You can verify the cloned URL by running the below command:
git remote -v origin https://github.com/USERNAME/REPOSITORY.git (fetch) origin https://github.com/USERNAME/REPOSITORY.git (push)
Additionally, read our guide:
- Git Fetch All Branches
- Laravel: Switch Case Statement In Controller Example
- Laravel: Change Column Type In Migration
- 2fa Laravel With SMS Tutorial With Example
- How To Use Where Date Between In Laravel
- How To Add Laravel Next Prev Pagination
- Laravel Remove Column From Table In Migration
- Laravel: Get Month Name From Date
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Update Pivot Table In Laravel
- How To Install Vue In Laravel 8 Step By Step
- How To Handle Failed Jobs In Laravel
- Best Ways To Define Global Variable In Laravel
- How To Get Latest Records In Laravel
- How To Break Nested Loops In PHP Or Laravel
- How To Pass Laravel URL Parameter
- Laravel Run Specific Migration
- Laravel Notification Tutorial With Example
- How To Schedule Tasks In Laravel With Example
- Laravel Collection Push() And Put() With Example
That’s it from our end. We hope this article helped you solve the Git permission denied public key issue.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!