我知道如何让用户使用密钥ssh到另一个框中:
ssh -l targetuser -i path/to/key targethost
但是像apache这样的非帐户用户呢?由于该用户没有可以编写.ssh目录的主目录,因此整个过程始终失败:
$sudo -u apache ssh -o StrictHostKeyChecking=no -l targetuser -i path/to/key targethost Could not create directory '/var/www/.ssh'. Warning: Permanently added '<hostname>' (RSA) to the list of known hosts. Permission denied (publickey).
我尝试使用-o UserKnownHostsFile = / dev / null并将$HOME设置为/ dev / null,并且这些都没有完成.我知道sudo可能会为我解决这个问题,但我正在努力避免需要手动配置服务器,因为此代码将部署在许多不同的环境中.
有任何想法吗?
以下是我尝试过的一些不起作用的例子:
$sudo -u apache export HOME=path/to/apache/writable/dir/ ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=path/to/apache/writable/dir/.ssh/known_hosts -l deploy -i path/to/key targethost $sudo -u apache ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=path/to/apache/writable/dir/.ssh/known_hosts -l deploy -i path/to/key targethost $sudo -u apache ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -l deploy -i path/to/key targethost
解决方法
只需创建/var/www/.ssh/并为其授予apache写权限或将apache的$HOME更改为他可以编写的目录.