Laravel Flysystem sftp Permission Denied
Asked Answered
R

4

5

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.

Rimple answered 2/10, 2017 at 22:29 Comment(4)
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.Ladylike
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.Ladylike
@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.Steels
Great! Sorry I didn't see this question sooner :) Are you going to post this as an answer to your question?Ladylike
M
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]

Microscopy answered 3/7, 2019 at 15:51 Comment(2)
Thank you for posting this! I tried to figure this out for hours and this finally worked.Rann
yeap RSA problem, checked source code and didnt want to believe first, then tested everything and only this one workedDogtired
D
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.

Dinsdale answered 4/10, 2017 at 13:1 Comment(1)
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.Steels
P
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!

Pseudaxis answered 2/5, 2018 at 3:21 Comment(0)
N
0

For me the error above was due to wrong ownership of private key file

I did chmod the key to 600 and set ownership set to www-data:www-data

changed to the following and could authenticate fine actualuser:www-data

Nanettenani answered 24/11, 2023 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.