user192344
user192344

Reputation: 1396

linux command and shell script for remote transfer file

I have setup the passpahse between local server and remote server

and I try this command directly in linux (where file is a single file and $remote is the remote server)

scp $file root@$remote:/tmp/

it work fine

and I try to write the command in shell script and call it on linux , it also work

but if I try to call the shell script from php, it just output the error (from my logging)

Could not create directory '/var/www/.ssh'.

Host key verification failed.

How come?

Upvotes: 2

Views: 6815

Answers (4)

G.DURGA RAO VENKY
G.DURGA RAO VENKY

Reputation: 19

create a dummy directory in /var/www, then run mkdir /var/www/.ssh and chown -R www-data:www-data /var/www/.ssh

This should fix your problem. shell_exec('/usr/bin/scp $file root@$remote:/tmp/')

Upvotes: 1

Patrick
Patrick

Reputation: 11

Try this first in your Script:

whoami

If the User is www-data, create a Directory .ssh in /var/www. Then give this Directory /var/www/.ssh the Owner and Group of the User, your PHP runs.

For me (Perl-Script) this works well, although there's nothing in the Directory .ssh after starting my Script (sFTP).

Upvotes: 1

Roger Lindsjö
Roger Lindsjö

Reputation: 11543

Your php does not run as you, or at least has a changed root, so it sees its home as /var/www/ which does not contain the .ssh folder (and needed files).

If you add a .ssh in /var/www, make sure the .ssh is owned by the user that php runs as and that the files have correct attributes (who can read/write to the files).

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174624

Your normal user account has the ssh keys stored (by default, they are stored in $HOME/.ssh) not the web server user which is why the command runs fine when you use it in the shell and not from the browser. The browser process tries to create the .ssh directory in its home directory and fails to do so, and thus cannot continue.

You should be using ssh2_scp_send to do this from PHP.

Upvotes: 0

Related Questions