293

My problem is that I can't push or fetch from GitLab. However, I can clone (via HTTP or via SSH). I get this error when I try to push :

Permission denied (publickey) fatal : Could not read from remote repository

From all the threads I've looked, here is what I have done :

  • Set up an SSH key on my computer and added the public key to GitLab
  • Done the config --global for username and email
  • Cloned via SSH and via HTTP to check if it would resolve the issue
  • Done the ssh -T [email protected] command

If you have any insight about how to resolve my issue, it would be greatly appreciated.

12
  • 17
    run ssh -vvvv [email protected] to see if it picks up the SSH key Commented Nov 4, 2016 at 16:30
  • 2
    Did you add the SSH key via gitlab.com/profile/keys ? Commented Nov 4, 2016 at 16:32
  • @ Nils Werner : I get a lot of lines when I do this command, but one of them is "Authentification succeeded (publickey)" Commented Nov 4, 2016 at 16:33
  • 11
    make sure you are not running sudo git clone [email protected]:project/somethiing.git, otherwise ssh will look in /root/.ssh instead of the key you uploaded ~/.ssh/id_rsa
    – rivanov
    Commented Jun 21, 2017 at 20:18
  • 7
    I solved the same problem by adding a key to agent via "ssh-add ~/.ssh/the_key" followed by authorising they key by phrase. This is the private part of what I added to GitLab.
    – Celdor
    Commented Sep 24, 2019 at 12:23

45 Answers 45

624

I found this after searching a lot. It will work perfectly fine for me.

  1. Go to "Git Bash" just like cmd. Right click and "Run as Administrator".
  2. Type ssh-keygen
  3. Press enter.
  4. It will ask you to save the key to the specific directory.
  5. Press enter. It will prompt you to type password or enter without password.
  6. The public key will be created to the specific directory.
  7. Now go to the directory and open .ssh folder.
  8. You'll see a file id_rsa.pub. Open it on notepad. Copy all text from it.
  9. Go to https://gitlab.com/-/profile/keys or
  10. Paste here in the "key" textfield.
  11. Now click on the "Title" below. It will automatically get filled.
  12. Then click "Add key".

Now give it a shot and it will work for sure.

10
  • 5
    In case you're lost in copying the pub file into clipboard. try the following: type %userprofile%\.ssh\id_rsa.pub | clip
    – user5100770
    Commented May 28, 2018 at 18:59
  • 8
    in windows 10 if you do need to find .ssh\id_rsa.pub you can type this cat ~/.ssh/id_rsa.pub on git bash it will show you the "Key" , so you could copy and paste it to text field in gitlab.com/profile/keys Commented Sep 10, 2018 at 3:41
  • 27
    Make sure to do this: ssh-add filename(with-path if not in the rsa dir) after following above steps
    – Blasanka
    Commented Dec 13, 2018 at 15:57
  • 17
    I did this, problem is not fixed, getting permissions denied Commented Dec 31, 2019 at 10:40
  • 5
    Referencing @Blasanka 's answer ssh-add filename where filename is the file WITHOUT .pub at the end. You'll be prompt the password you've generated during ssh key creation to add your identity Commented Jun 2, 2020 at 22:59
119

Step 1: Added a config file in ~/.ssh/config file which looks like

   User git
   Hostname gitlab.com
   IdentityFile ~/.ssh/id_rsa_gitlab
   TCPKeepAlive yes
   IdentitiesOnly yes

Step 2: Just clone the git repo WITHOUT sudo.
Documentation: https://gitlab.com/help/ssh/README#working-with-non-default-ssh-key-pair-paths

** sometimes you have the config in your ~/.ssh/config, but, IdentityFile path is not correct. you can check the file name like this ls ~/.ssh/. the file is normally is id_ed25519 for gitlab. thus the correct config is IdentityFile ~/.ssh/id_ed25519

7
  • 4
    This is required if the key-file with the default name is already used for different purposes. If you are creating a key-file with non-default name, like id_rsa_gitlab in Fedo's example, you will need to supply a config-file. Gitlab's nice article about the matter: gitlab.com/help/ssh/…
    – Dany
    Commented Mar 9, 2019 at 10:31
  • This worked for me. I had both Github & Gitlab keys, so that you for this solution.
    – M.Cush
    Commented Aug 8, 2019 at 23:25
  • I have to change Hostname to Hostto make this working
    – Sadee
    Commented Oct 8, 2019 at 15:47
  • This work for me on Windows 10 with Ubuntu 20.04 WSL in Hyper terminal
    – hhsm95
    Commented May 6, 2021 at 23:10
  • The important part here is that IdentityFile is the private part; you should match this file with the *.pub public key which you've uploaded to Gitlab. Mixing one private key with another public key will fail, intentionally. Gitlab authenticates you because you have a matching pair of files.
    – MSalters
    Commented Jun 20, 2022 at 15:21
78

I think the simple solution is to add private key to authentication agent (if your key is not ~/.ssh/id_rsa),

ssh-add ~/.ssh/<your private key>

You basically let the ssh-agent take care of it.

Additionally, you can add it permanently.

3
  • 1
    it's the file with the extension .pub
    – user3741325
    Commented May 29, 2018 at 5:49
  • 9
    No. Private key is the file without .pub extension.
    – Hussain
    Commented May 30, 2018 at 2:53
  • 2
    Worked for me. I had the default config , so i typed : ~$ ssh-add ~/.ssh/id_ed25519 in console and It worked . @Hussain thanks Commented Aug 21, 2022 at 9:43
25

There is a very simple solution to this: Instead of working with ssh - move to https. to do this: in your project folder you have a .git folder in there - you have a config file - open it in a text editor and change the line

url [email protected]:yourname/yourproject.git

to

url = https://gitlab.com/yourname/yourproject.git

7
  • 6
    This doesn't solve the problem it sidesteps it entirely.
    – UpAndAdam
    Commented May 17, 2021 at 20:41
  • 1
    That's the answer I was searching to avoid ssh and use https way.
    – Felix H.
    Commented Jun 11, 2021 at 7:55
  • http doesn't allow me to make a git push -f, only git push Commented Oct 23, 2021 at 16:34
  • This worked, thanks. @UpAndAdam this sidesteps the issue, sure, but ssh is so problematic and frustrating on Windows that I would rather sidestep it. I'd rather get work done than wrestle with ssh.
    – Samaursa
    Commented Jan 10, 2022 at 20:40
  • 1
    Your personal anecdote is meaningless. That doesn't make it an answer to the question asked. It is a workaround and does not answer the question. Furthermore windows is NOT tagged in the question and Ubuntu is. So your anecdote has no relevance
    – UpAndAdam
    Commented Jan 18, 2022 at 22:45
24

In my case it did not work in the WSL (Windows Subsystem for Linux).

When I start the WSL, I must

  • start ssh-agent_ eval $(ssh-agent -s)
  • add the key to the ssh-agent: ssh-add ~/.ssh/id_rsa
  • if prompted, enter the password

Now the connection works.
We can test this with ssh -T [email protected]

notes:

0
20

if you are in Linux or macox , just try this in terminal:

ssh-add -l

if it return nothing, try this:

ssh-add

it must create identity in ~/.ssh/id_rsa

after retry :

ssh-add -l

it must return your identity, so after retry to clone, it's must work

NB: don't forget to add your ssh key in your profile gitlab

thanks

2
  • 1
    Constantly returns The agent has no identities. Commented Aug 31, 2021 at 20:50
  • 1
    you save my day, thank you! Commented Nov 22, 2021 at 14:21
18

make sure you are not running sudo git clone [email protected]:project/somethiing.git, otherwise ssh will look in /root/.ssh instead of the key you uploaded ~/.ssh/id_rsa

1
  • Thank u man. My company require the project em / folder. I had to gerenate ssh using sudo. That way, i can use sudo git clone. Your advice helped me a lot.
    – Jocsã
    Commented Aug 10, 2020 at 14:41
11

I know, I'm answering this very late and even StackOverflow confirmed if I really want to answer. I'm answering because no one actually described the actual problem so wanted to share the same.

The Basics

First, understand that what is the remote here. Remote is GitLab and your system is the local so when we talk about the remote origin, whatever URL is set in your git remote -v output is your remote URL.

The Protocols

Basically, Git clone/push/pull works on two different protocols majorly (there are others as well)-

  1. HTTP protocol
  2. SSH protocol

When you clone a repo (or change the remote URL) and use the HTTPs URL like https://gitlab.com/wizpanda/backend-app.git then it uses the first protocol i.e. HTTP protocol.

While if you clone the repo (or change the remote URL) and uses the URL like [email protected]:wizpanda/backend-app.git then it uses the SSH protocol.

HTTP Protocol

In this protocol, every remote operation i.e. clone, push & pull uses the simple authentication i.e. username & password of your remote (GitLab in this case) that means for every operation, you have to type-in your username & password which might be cumbersome.

So when you push/pull/clone, GitLab/GitHub authenticate you with your username & password and it allows you to do the operation.

If you want to try this, you can switch to HTTP URL by running the command git remote set-url origin <http-git-url>.

To avoid that case, you can use the SSH protocol.

SSH Protocol

A simple SSH connection works on public-private key pairs. So in your case, GitLab can't authenticate you because you are using SSH URL to communicate. Now, GitLab must know you in some way. For that, you have to create a public-private key-pair and give the public key to GitLab.

Now when you push/pull/clone with GitLab, GIT (SSH internally) will by default offer your private key to GitLab and confirms your identity and then GitLab will allow you to perform the operation.

So I won't repeat the steps which are already given by Muhammad, I'll repeat them theoretically.

  1. Generate a key pair `ssh-keygen -t rsa -b 2048 -C "My Common SSH Key"
  2. The generated key pair will be by default in ~/.ssh named id_rsa.pub (public key) & id_rsa (private key).
  3. You will store the public key to your GitLab account (the same key can be used in multiple or any server/accounts).
  4. When you clone/push/pull, GIT offers your private key.
  5. GitLab matches the private key with your public key and allows you to perform.

Tips

You should always create a strong rsa key with at least 2048 bytes. So the command can be ssh-keygen -t rsa -b 2048.

https://gitlab.com/help/ssh/README#generating-a-new-ssh-key-pair

General thought

Both the approach have their pros & cons. After I typed the above text, I went to search more about this because I never read something about this.

I found this official doc https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols which tells more about this. My point here is that, by reading the error and giving a thought on the error, you can make your own theory or understanding and then can match with some Google results to fix the issue :)

9

Steps to be done, got same error but i fixed it. Gitlab wants ssh-rsa so below is the code to run ssh for rsa

  1. ssh-keygen -o -t rsa -b 4096 -C "[email protected]"

[email protected] is your gitlab account email

  1. It will prompt you to enter so just hit Enter after the below code is prompt,

    Enter file in which to save the key (/home/yourDesktopName/.ssh/id_rsa):

  2. It will prompt again you to enter so just hit Enter after the below code is prompt,

    Enter passphrase (empty for no passphrase):

  3. It will prompt again for the last you to enter so just hit Enter after the below code is prompt,

    Enter same passphrase again:

  4. You will show your ssh-rsa generate.

  5. Login to your Gitlab account and Go to the right navbar you will get setting and in the left sidebar you will get ssh key. Enter in it.

  6. Look above the prompt asking you to enter, you will get the path of ssh-rsa.

  7. Go to your SSH folder and get the id_rsa.pub

  8. Open it and get the key and Copy Paste to the Gitlab and you are nearly to done.

  9. Check by: ssh -T [email protected]

  10. You will get: Welcome to GitLab, @joy4!

  11. Done.

4
  • thanks. this works on windows 10. I tried ed25519 as gitlab instructed but not working.
    – Dika
    Commented Mar 29, 2019 at 2:14
  • This answer is pretty good - not sure why its not voted higher.
    – mgibson
    Commented Oct 15, 2019 at 17:52
  • This was the solution for me.
    – Byron2017
    Commented Dec 23, 2020 at 2:44
  • Thanks a lot! You saved me. Also tried on Win10
    – Angelika
    Commented Jan 9 at 11:04
7

In my case, it wasn't a gitlab problem, but a sshd configuration one. The ssh server didn't allow connection except for a list of users. The user git, the one connecting remotely to gitlab, wasn't in that list. So, check this before anything else.

You can check your ssh server configuration in /etc/ssh/sshd_config. If you have a line with the option AllowUsers, add git to it:

AllowUsers user1 user2 user3 git
0
5
  1. Go to project directory in terminal using cd path/to/project
  2. Run ssh-keygen
  3. Press enter for passphrase
  4. Run cat ~/.ssh/id_rsa.pub in terminal
  5. Copy the key that you get at the terminal
  6. Go to Gitlab/Settings/SSH-KEYS
  7. Paste the key and press Add Key button

This worked for me like a charm!

0
4

I have gitlab running with docker, this is what I did to fix my problem.

Found that inside docker /var/log/gitlab/sshd/current there were multiple occurences of a message:

Authentication refused: bad ownership or modes for file /var/opt/gitlab/.ssh/authorized_keys

After which I changed ownership of that file from 99:users to git:users with:

chown git:users authorized_keys

2
  • 2
    Thank you SO MUCH. I'm running Gitlab on Kubernetes and the /gitlab-data directory (which is also the mountpoint for authorized_keys) had permission problems. A simple chown was enough. Commented Jul 13, 2017 at 6:47
  • @DillenMeijboom what was the chown permission that you needed to set? I'm suspecting a similar issue on mine but I'm not sure what to set the gitlab-data mount directory to.
    – falc0nit3
    Commented Sep 8, 2020 at 7:30
4

When you have multiple git account and you want different ssh key

You have to follow same step for generating the ssh key, but be sure you

ssh-keygen -t ed25519 -C "[email protected]" 

Enter the path you want to save(Ex: my-pc/Desktop/.ssh/ed25519)

Add the public key to your gitlab (How to adding ssh key to gitlab)

You have to new ssh identity using the below comand

ssh-add ~/my-pc/Desktop/.ssh/ed25519
4

I found the solution in gitlab help.

To create a new SSH key pair: 
 1. Open a terminal on Linux or macOS, or Git Bash / WSL on Windows.
 2. Generate a new ED25519 SSH key pair: ssh-keygen -t ed25519 -C "[email protected]"
 2.1 Or, if you want to use RSA: ssh-keygen -o -t rsa -b 4096 -C "[email protected]"
 3. Next, you will be prompted to input a file path to save your SSH key pair to... use the suggested path by pressing Enter
 4. Once the path is decided, you will be prompted to input a password to secure your new SSH key pair. It's a best practice to use a password, but it's not required and you can skip creating it by pressing Enter twice.
 5. Copy your public SSH key to the clipboard by using one of the commands below depending on your Operating System:
        macOS:        pbcopy < ~/.ssh/id_ed25519.pub
        WSL / GNU/Linux (requires the xclip package):      xclip -sel clip < ~/.ssh/id_ed25519.pub
        Git Bash on Windows:      cat ~/.ssh/id_ed25519.pub | clip
 6. Navigating to SSH Keys and pasting your public key in the Key field
 7. Click the Add key button

I hope it can help some of you!

4

Earlier it was very difficult for me but when I tried it became so easy to add ssh key in Mac and Linux. There are a few steps and command to do this as follows:

  1. Open a terminal of your system and move inside your project directory by the command:
cd 'project directory name'
  1. Run command ssh-keygen in that terminal and enter it until the key's randomart image appears there.

  2. Then enter one more command in that terminal:

cat ~/.ssh/id_rsa.pub

It will generate your ssh key. Key will start with ssh-rsa and end with .local.

  1. Copy the key and go to your Gitlab profile section then ssh key section and paste it there. Click on the Add button this will work.
0
4

this way working for me.

  1. add ssh / rsa keys to gitlab
  2. ssh-add (type your terminal)
  3. for verified type - ssh -vT [email protected]
1
  • 1
    ssh-add was that I was missing! Thank you! Commented Jul 2, 2021 at 8:05
4

I spent many hours to fix this. Below solution worked for me.

Follow these commands on your terminal.

ssh-keygen -t rsa -C "<your email address>" -b 4096 -t ed25519

Rename generated keys as this: id_rsa and id_rsa.pub the Add generated public key to your gitlab server.

Then add ssh key to ssh agent as below.

  • Initiate ssh-agent

    eval "$(ssh-agent -s)"

  • If your key is generated with a different name, replace id_rsa in the command below

    ssh-add ~/.ssh/id_rsa

To test the connection use this command.

ssh -T git@<git lab server url>

To troubleshoot use below commands.

ssh -Tv git@<git lab server url>
ssh -vvv git@<git lab server url>

Now you can clone your project to your local environment.

4

For me this happened after a software update on Mac OS. I first had this problem:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

and fixed it running

sudo chmod 600 ~/.ssh/id_rsa
sudo chmod 600 ~/.ssh/id_rsa.pub

Next I had this problem

[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

And I could fix it simply with

ssh-add --apple-use-keychain ~/.ssh/id_rsa
3

I had the same issue, I resolved it by adding a new ssh key:

  1. ssh-keygen -t ed25519 -C "[email protected]"
  2. Copy your public SSH key to the clipboard (xclip -sel clip < ~/.ssh/id_ed25519.pub in my case on Linux )
  3. On gitlab, go to settings=>ssh keys and past the new key
1
  • Since rsa is an old algorithm, using a new one such as ed25519 as @ColdFire used is a better choice when creating ssh key with ssh-keygen . Using GIT_SSH_COMMAND="ssh -vvv" git clone ssh://git@<bitbucket URL>:<bitbucket port>/<project key>/<repository slug>.git show detailed information.
    – serkanh
    Commented Feb 14 at 7:53
3

For anyone using Windows 10 and nothing else working for him/her:

In my case, I had to clone the repo with https instead of ssh and a window popped-up asking for my credentials. After that everything works fine.

0
2

Two things mainly

  1. You must have id_rsa.pub and id_rsa (private) keys in your .ssh folder ( which should be in your home folder.Create it if it isn't there put your keys ). It wouldn't work If you have named your key files differently

  2. Change the permission of the id_rsa as chmod 400 ~/.ssh/id_rsa

2

Another issue that can cause this behaviour is when you have a setup with 2 possible %HOME%-locations.

I'm using a PC where some of my documents are stored locally, and some of them are stored on a network drive. Some applications think C:\Users\<MyUserName>\ is my %home%, others think that U:\ is the home.

Turns out ssh-keygen put my private key under C:\users\<MyUserName>\, and that ssh -T and ssh -v also look there.

So everything seems to work fine, except that git clone, git push and others look for a key in U:\. Which fails, so I get the aforementioned error.

It took me an hour to find out, but in the end the solution was simple: I copied everything from C:\Users\<MyUserName>\.ssh to U:\.ssh

2

Nothing worked for me on Windows 10 using Pageant as SSH agent, except adding a enviroment variable to windows (translated from german Windows 10, so the naming may differ):

  1. Search for "variables"
  2. Open System Enviroment Variables
  3. Click Enviroment Variables button at the bottom
  4. Add a new key named "GIT_SSH" and the value "C:\Program Files\PuTTY\plink.exe", to the top section "User Variables xxx"
  5. And you're done.

All thanks go to Benjamin Bortels, source: https://bortels.io/blog/git-in-vs-code-unter-windows-richtig-einstellen

1
  • Works after resatrt Windows 11 only.
    – isabsent
    Commented Dec 28, 2023 at 13:07
1

I solved like this..

Generated a key for Windows using this command:

ssh-keygen -t rsa -C "[email protected]" -b 4096

but the problem was that after running this command, it popped a line: "Enter file in which to save the key (/c/Users/xxx/.ssh/id_rsa): " Here, I was giving only file name because of which my key was getting saved in my pwd and not in the given location. When I did "git clone ", it was assuming the key to be at "/c/Users/xxx/.ssh/id_rsa" location but it was not found, hence it was throwing error.

At the time of key generation 2 files were generated say "file1" & "file1.pub". I renamed both these files as

file1 -> id_rsa 

and

file1.pub -> id_rsa.pub

and placed both in the location "/c/Users/xxx/.ssh/"

1

Go to the terminal and regenerate the ssh key again. Type ssh-keygen. It will ask you where you want to save it, type the path.

Then copy the public key to gitlabs platform. It usually starts with ssh-rsa.

1

The problem for me was, that I switched UsePAM from yes to no in the SSH configuration file under /etc/ssh/sshd_config. With UsePAM yes everything works perfectly.

1

How to add SSH key to gitlab account in ubuntu?

  1. Open terminal in your project directory.
  2. Type ‘ssh-keygen -o -t rsa -b 4096 -C "your gitlab email" ’ and hit enter
  3. Type ‘vim /home/mnbtech/.ssh/id_rsa.pub’ and hit enter (or manually open your 'id_rsa.pub'from where you saved it)
  4. SSH key will appear. Copy those and

  5. Go to your gitlab account.

  6. Click profile image And click setting
  7. In left side select SSH-Keys
  8. Then paste those key Click add key

SSH-Key will be added!

(N.B if you have Generate Previews SSH Key and Getting permission denied (public key). You Delete Your Previews ssh key and Generate new one and add git user.name and email on your terminal )

1
  • What is different in that answer than in the previous instructions?
    – RalfFriedl
    Commented Jul 23, 2019 at 5:20
1

There seem to be differences between the two ways to access a git repository i.e. using either SSH or HTTPS. For me, I encountered the error because I was trying to push my local repository using SSH.

The problem can simply be solved by clicking the clone button on the landing page of your project and the copying the HTTPS link and replacing it to the SSH link appearing with the format "git@gitlab...".

1
  • That doesn't answer the question.
    – RalfFriedl
    Commented Sep 16, 2019 at 17:59
1

I had the same problems, It has been fixed after I re-generate the ssh key inside .ssh folder without naming it (keep it as id_rsa.pub). Then add it again to gitlab ssh key. Everything working fine now.

1

I solved [email protected]: Permission denied (publickey) issue using following instructions

  1. RUN cat ~/.ssh/id_rsa.pub
  2. Copy id_rsa.pub (public key) to your getlab `Setting -> SSH Keys
  3. RUN cat ~/.ssh/id_rsa
  4. Copy id_rsa (private key) to `Code_repo->git_auth->id_rsa

NOTE: Take care of the machine user if you are using root user in your DockerFile or anywhere else then use sudo su before running the above commands to get root user public and private keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.