Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RoyGoode's avatar

How to push Codes from VScode to Server using SSH-PrivateKey

Hi there I have a new server that i want to move my website to it. before i use a Git Remote :

git remote add production ssh://[email protected]:420/var/repo/site.git

And to access this server I used a Password to push my codes, Site.git is a git Hook and this file push the changes to main folder /var/www/mySite.

Now I set the SSH-Keys, but i can't to do that again, i must to set a Hostname on Git Config file in .ssh Folder like this :

Host myhost
  HostName 185.***.***.123
  Port 420
  User admin
  IdentityFile ~/.ssh/ar-privatekey.pem
  IdentitiesOnly yes
  AddKeysToAgent yes

// with this Remote
git remote add production roy@myhost:/var/repo/site.git
// roy is that user that i set in git Config global.

with this, I can Conect to this server using git Command == 'ssh myhost' with no problem, But when i want to Push or Clone all of my codes, its givrs me this error :

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

Please make sure you have the correct access rights
and the repository exists.

??????? ThankYou

0 likes
3 replies
rhand's avatar

I recommend using PHP Deployer https://github.com/deployphp/deployer/ , but in your case it just seems your public key is not being accepted so add your public ssh key to the server's authorized ssh keys and that should solve your issue.

1 like
RoyGoode's avatar

@rhand I can see the Public key in (authorized_keys) file in .ssh directory on ubunto. Where is the problem! :(

rhand's avatar

It is either the public key issue or the access rights are not in order. So you need to figure this out.

Well, how are the rights set on /var/repo/ and /var/repo/site.git? Are the writable and in the same group the user is in? Example

forge@app-staging:~/staging.app.com$ ll
total 20
drwxrwxr-x  5 forge forge 4096 Sep 14 08:01 ./
drwxr-xr-x 11 forge forge 4096 Sep  1 02:34 ../
drwxrwxr-x  2 forge forge 4096 Sep 14 08:01 .dep/
lrwxrwxrwx  1 forge forge   12 Sep 14 08:00 current -> releases/605/
drwxrwxr-x 12 forge forge 4096 Sep 14 08:01 releases/
drwxrwxr-x  4 forge forge 4096 Jun 18 02:56 shared/

and

forge@app-staging:~/staging.app.com/current$ ll
total 5420
drwxrwxr-x 16 forge forge    4096 Sep 14 08:01 ./
drwxrwxr-x 12 forge forge    4096 Sep 14 08:01 ../
-rw-rw-r--  1 forge forge      96 Sep 14 08:00 .babelrc
lrwxrwxrwx  1 forge forge      17 Sep 14 08:00 .env -> ../../shared/.env
-rw-rw-r--  1 forge forge     292 Sep 14 08:00 .env.dusk.local.example
-rw-rw-r--  1 forge forge    2234 Sep 14 08:00 .env.example
-rw-rw-r--  1 forge forge     207 Sep 14 08:00 .eslintignore
-rw-rw-r--  1 forge forge     556 Sep 14 08:00 .eslintr.json
-rw-rw-r--  1 forge forge     625 Sep 14 08:00 .eslintrc.json
drwxrwxr-x  8 forge forge    4096 Sep 14 08:00 .git/

where all is in the group my git push/pull and ssh user is in as well as set with owner forge.

Did you git push --verbose origin master to get more details on the issue? Did you push to the correct branch?

Is the private key added to your ssh config file connected to the public key in authorized_keys? Are the authorized_keys rights set properly. General setup is normally done this way:

forge@app-staging:~$ stat -c "%a %n" .ssh/
755 .ssh/
forge@app-staging:~/.ssh$ stat -c "%a %n" *
755 authorized_keys
700 id_rsa
755 id_rsa.pub
755 known_hosts

or set things stricter if possible this way but if ssh works separately from git do not do this. For general Ubuntu server ssh setups recommend this often:

chmod 700 .ssh
sudo chmod 640 .ssh/authorized_keys
sudo chown $USER .ssh
sudo chown $USER .ssh/authorized_keys

But always be careful and have another way to get if if possible when you change ssh rights and permissions.

My forge user ssh example is a Laravel Forge Ubuntu example and so 755 is used for .ssh and not 700.

Please or to participate in this conversation.