SOLVE GIT PERMISSION DENIED PUBLICKEY ON UBUNTU
Problem:
When trying to clone a repository or run Git commands via SSH on your server, you might encounter the following error:
root@your-server:/www/wwwroot/your-project# git clone [email protected]:username/repository.git
Cloning into 'repository'...
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Steps:
- Open your terminal on your VPS.
- Generate a new SSH key with the following command (press
Enterto accept all defaults when prompted):ssh-keygen -t ed25519 -C "[email protected]" - Start the SSH agent in the background:
eval "$(ssh-agent -s)" - Add your newly generated SSH private key to the SSH agent:
ssh-add ~/.ssh/id_ed25519 - View and copy the content of your public SSH key:
cat ~/.ssh/id_ed25519.pub - Register the SSH key in your GitHub account:
- Login to GitHub and go to Settings -> SSH and GPG keys.
- Click New SSH key.
- Enter a title (e.g., "My VPS Production Server") and paste the public key content you copied in step 5 into the "Key" field.
- Click Add SSH key.
- Test the SSH connection to GitHub to ensure it has been successfully authenticated:
If successful, you will see a message like this:ssh -T [email protected]Hi username! You've successfully authenticated, but GitHub does not provide shell access. - Go back to your project directory and run your git clone command again using SSH:
git clone [email protected]:username/repository.git
Note:
- If you encounter the warning
The authenticity of host 'github.com' can't be established, simply typeyesand pressEnterto save GitHub to your known hosts list. - Always make sure to copy the
.pubfile content (public key) for GitHub registration, NOT the private key file. - If you used a custom name or different file path for your keys during step 2, adjust the path in steps 4 and 5 accordingly.