Generating Ssh Keys for 'Apache' User

Generating SSH keys for 'apache' user

As you are root, you can try it sudo -u apache ssh-keygen -t rsa

Generating ssh keys for 'apache' user on shared hosting

use GIT_SSH environment variable:

mkdir /path/to/home/dir/.ssh/
chmod 0700 /path/to/home/dir/.ssh/
chown apache:apache /path/to/home/dir/.ssh/

create wrapper for ssh (in home dir /path/to/home/dir/ssh_wrap)

#!/bin/sh
$target=$1;
$command=$2;
ssh -F /path/to/home/dir/.ssh/ssh_config -i /path/to/home/dir/id_rsa $target $command

run

chmod +x /path/to/home/dir/ssh_wrap

create file /path/to/home/dir/.ssh/ssh_config:

 UserKnownHostsFile=/path/to/home/dir/.ssh/known_hosts
StrictHostKeyChecking=no

in your script before git clone add

 export GIT_SSH=/path/to/home/dir/ssh_wrap

this may need changing, you need to get the idea. more info in man git man ssh

Apache user account passwordless access to the server - Ubuntu

The problem was the Apache user cannot access my keys. Therefore I had to generate SSH keys for the Apache user (it's www-data) although it was not so secure. First login as root.

mkdir /var/www/.ssh
chown -R www-data:www-data /var/www/.ssh

Now generate SSH keys as following. It will save your private key and public key in /var/www/.ssh folder:

sudo -u www-data ssh-keygen -t rsa

Now you should get something like this:

root@sampath-Vostro-1520:/var/www/.ssh# sudo -u www-data ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/var/www/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/www/.ssh/id_rsa.
Your public key has been saved in /var/www/.ssh/id_rsa.pub.
The key fingerprint is:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx www-data@machine-Vostro-1520
The key's randomart image is:
+--[ RSA 2048]----+
| ...o...o..|
| o.. o|
| + .. .+o|
| . .*o+o|
| ++ S ..B..|
| o . E + |
| . . o o |
| . . |
| |
+-----------------+

Now copy your public key to the remote server:

sudo -u www-data ssh-copy-id -i /var/www/.ssh/id_rsa.pub username@myserver.com

Now this should work. :-)

<?php
$c='rsync -azv /source/folder/path/ username@myserver.com:/destination/folder/path';
exec($c,$data);
print_r($data);
?>

How to let apache to do ssh

My recommendation: use phpseclib, a pure PHP SSH implementation. eg.

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>

Setting ssh keys to use with jgit with ssh from apache sshd

File sshDir = new File(FS.DETECTED.userHome(), "/.ssh");
SshdSessionFactory sshSessionFactory = new SshdSessionFactoryBuilder()
.setPreferredAuthentications("publickey")
.setHomeDirectory(FS.DETECTED.userHome())
.setSshDirectory(sshDir)
.build(null);

The above code worked for me, just need to have the id_rsa file in the .ssh folder.
and the key has to be without a passphrase.
Still looking for a way to pass a passphrase with a key.

You can use this link for better understanding:
https://medium.com/@pratikshende99/how-to-clone-git-repo-with-ssh-url-by-executing-command-through-java-using-processbuilder-e162010266f5

How to add public key identity from String?

I found a way to use a String instead of a file, see KeyPairResourceLoader#loadKeyPairs:

default Collection<KeyPair> loadKeyPairs(SessionContext session,
NamedResource resourceKey,
FilePasswordProvider passwordProvider,
String data)
throws IOException,
GeneralSecurityException

Throws:

IOException

GeneralSecurityException

My changed code:

KeyPairResourceLoader loader = SecurityUtils.getKeyPairResourceParser();
Collection<KeyPair> keyPairCollection = loader.loadKeyPairs(null, null, null, pem);

How to let apache to do ssh

My recommendation: use phpseclib, a pure PHP SSH implementation. eg.

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>


Related Topics



Leave a reply



Submit