I am trying to execute couple of commands on remote server and as authentication method, I am using public keys.
Public keys are configured properly on server and I can connect to it without system prompting me for password like this:
ssh root@example.com
So this is what I do in my code:
Net::SSH.start(host, user, :keys=>["~/.ssh/id_rsa","~/.ssh/id_rsa.pub"]) do |ssh|
ssh.exec "service nginx reload"
end
On local machine, everything works properly, when on production server, I get an error.
Net::SSH::AuthenticationFailed: Authentication failed for user root@example.com
I did some Googling and I had already tried to fallback to net_ssh 2.7.0, yet no luck, same issue. I also tried to replace relative path to absolute, in my case /root/.ssh/... I tried adding an option
:auth_methods => ['publickey']
yet still, no luck.
I am also certain that the keys reside in correct paths.
EDIT 1
Worth noting that this works perfectly well when run in production rails console.
EDIT 2
Symptoms here are similar with ones in this question: Net::SSH works from production rails console, AuthenticationFailed from production webapp with exception that my app runs from root and there shouldn't be permission issues because of that.
EDIT 3
I had temporary fallen back to password auth that works fine but I'd really like to use key auth instead.
Any help on this issue is much appreciated.