1

I am using ssh2_connect() in PHP to move files from one server to another. However for the time being there are not two servers and I am just connecting the server to itself.

This is setup so that when there are two servers only the connection information needs changing rather than converting everything to ssh2 from the standard PHP functions.

This has worked for a while using password authentication but I am now switching over to Public Key authorisation.

I get the following error when trying to connect:

Username/PublicKey combination invalid

I've setup up the key a couple of times and the same thing happens. I am now wondering if it is not possible to authenticate this way on the same machine?

I know everything else is working with ssh because I have accessed the server with a public key from another server for another purpose.

2
  • Does the key work on the command line?
    – Zoredache
    Nov 5, 2014 at 17:34
  • maybe php uses wrong user for the key?
    – Martynas
    Nov 6, 2014 at 9:00

1 Answer 1

0

Edit: Cleared things up. I assume you use a Debian based distro like Ubuntu, so details might differ in your case.

It might be because you switched methods from password to key based. Things to do:

  1. Delete all content of your ~/.ssh/known_hosts file. This is because it stores a fingerprint for all successfull connections you had before. If you change your method of access, you should delete either the line with the fingerprint of the old connection or empty the whole file to be safe.

  2. Activate the home key folder in your /etc/ssh/sshd_config by uncommenting the line

    AuthorizedKeysFile      %h/.ssh/authorized_keys
    
  3. Generate a key pair without a passphrase via ssh-keygen -t rsa. Do not enter a passphrase.

  4. Make sure both key files (id_rsa and id_rsa.pub) are in ~/.ssh/

  5. Add the public key to your authorized_keys via cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys (Maybe adjust to your keyname).

  6. Make sure the permissions of ~/.ssh/ look like this:

    drwx------  2 user user 4096 Oct 20 17:41 ./
    drwxr-xr-x 20 user user 4096 Nov  6 10:29 ../
    -rw-------  1 user user 1228 Oct 20 17:41 authorized_keys
    -rw-------  1 user user 1679 Oct 20 17:41 id_rsa
    -rw-------  1 user user  394 Oct 20 17:41 id_rsa.pub
    -rw-------  1 user user  984 Oct 20 17:41 known_hosts
    
  7. Restart the ssh server. This can for example be done via sudo service ssh restart. You can check if it's running via sudo lsof -i -P | grep -i sshd

  8. Do a manual log in the first time before using php, because there might be a prompt asking if you are trusting the host. You will have to answer by typing yes.

You can force ssh to use a certain key file either with the -i option or by configuring a shortcut in the /etc/ssh_config. You can also force it to disable password-based authentication by using -o PasswordAuthentication=no

Example:

ssh -i ~/.ssh/id_rsa -o PasswordAuthentication=no [email protected]
14
  • ssh user@ip from the commandline. Maybe ssh user@localhost or ssh [email protected] works as well.
    – zerweck
    Nov 6, 2014 at 16:17
  • How can I force it to use a public key though, I haven't yet turned off password auth and need to keep it on, also do I have to specify which public key to use otherwise the root key will be used.
    – Kline
    Nov 6, 2014 at 16:41
  • so I am using ssh -i /home/user/.ssh/id_rsa.pub [email protected] to login using the right public key but when I enter the keys passphrase it says permission denied.
    – Kline
    Nov 6, 2014 at 17:12
  • If you want to use it over a php script in the end anyway, you should use a key without a passphrase. Check man ssh_config , there is a settung called PasswordAuth or similar. You should turn that off with the -o argument. I am on my phone right now, i will post an example when im home.
    – zerweck
    Nov 6, 2014 at 17:50
  • Updated my post.
    – zerweck
    Nov 6, 2014 at 18:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .