4

Im trying to use the Laravel Flysystem with the sftp adaptor from PHP League (league/flysystem-sftp). Using Laravel 5.4 and version 3.7 of the Flysystem.

When I attempt to put a file on the server, i get the message:

Cannot connect to someadress.com:22. Error 13. Permission denied

Here is the code:

$box = new Filesystem(new SftpAdapter(Config::get('flysystem.connections.sftp')));
$box->put('test.txt', 'bar');

and the connection details from the config:

'sftp' => [
     'driver'     => 'sftp',
     'host'       => 'someadress.com',
     'port'       => 22,
     'username'   => 'someuser',
     'password'   => 'ArndomPa55',
     'privateKey' => '/home/user/.ssh/id_rsa',
     'root'       => '/var/www/html/site/box/',
     'timeout'    => 20,
 ],

When I make an SSH connection from the server where this is running, it connects fine, without a password prompt, so it is using the Private Key. So not sure why this isn't working.

I've checked the secure log on the receiving server and nothing is in there.

Seán McCabe
  • 893
  • 4
  • 23
  • 47
  • Now that is a good MCVE. Did you resolve the issue? This problem occurs when the local user that the application runs under doesn't have permission to read */home/user/.ssh/id_rsa*. It works from the command line because your user *does* have permission to read that key. – Cy Rossignol Oct 23 '17 at 20:37
  • Also, you're specifying a `'password'` option along with `'privateKey'`, which won't work if the password *is not the private key passphrase*. When the two are used together, the value of `'password'` must be the passphrase for the private key, not the password of the remote user. – Cy Rossignol Oct 23 '17 at 20:54
  • @CyRossignol I got this working in the end, I did remove password, but it turned out to be an issue with apache being the user attempting to connect, as such I needed to give it access and have the keys in a place it had permission to access. – Seán McCabe Oct 23 '17 at 20:59
  • Great! Sorry I didn't see this question sooner :) Are you going to post this as an answer to your question? – Cy Rossignol Oct 23 '17 at 21:13

3 Answers3

4

While extremely old, and I am committing necromancy here, I came across this issue and none of these fixes helped. What happened (and may happen to others) is that the key type was openssh not rsa and the call to ssh2::_privatekey_login returned false, causing the whole event to fail. If your key is openssh (identified by -----BEGIN OPENSSH PRIVATE KEY----- as the header), run the following to convert it to RSA, fixing the problem.

ssh-keygen -p -m PEM -f [filename here]

Jeremy
  • 459
  • 1
  • 4
  • 5
  • Thank you for posting this! I tried to figure this out for hours and this finally worked. – jjeaton Oct 26 '20 at 03:51
  • yeap RSA problem, checked source code and didnt want to believe first, then tested everything and only this one worked – Paul Wall Feb 26 '22 at 17:12
2

I have a similar problem. The exception was :

local.ERROR: LogicException: Could not login with username: username, host: xx.xx.xx.xx. 

Following code in vendor\league\flysystem-sftp\src\SftpAdapter i found that hostFingerprint always returned null. After that I just removed privateKey => 'path/to/key' from the configuration for example

'sftp' => [
     'driver'     => 'sftp',
     'host'       => 'someadress.com',
     'port'       => 22,
     'username'   => 'someuser',
     'password'   => 'ArndomPa55',
     //'privateKey' => '/home/user/.ssh/id_rsa',
     'root'       => '/var/www/html/site/box/',
     'timeout'    => 20,
 ]

, and i connected to the server. I think it has to do something with the servers sftp configuration. For now that suites me for testing my scripts but i will look for the reason.

I hope that helps finding a permanent solution.

  • I had this at another stage on another server and got it solved, but was a different problem to the one above. the one above was that Apache was making the call, and as such didn't have a key to connect to the server. Solved that using other answers on this site. – Seán McCabe Oct 04 '17 at 21:12
0

I had the same problem. I have changed the user actually do php execution to the user writed sftp configuration. It was working on php-fpm. I have changed user & group on php-pfm confguration. I have not changed http.conf It works!