Search code examples
ubuntusshamazon-ec2capistranocapistrano3

Capistrano from rc.local ssh authentication failed


I'm setting simple capistrano 3 deployment script to run from rc.local for each of new nodes that get spawned by aws autoscaler.

Everything runs just fine when I execute the script manually as root, but once I reboot and look at rc.local execution log I get following:

cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host 127.0.0.1: Authentication failed for user [email protected]
/var/lib/gems/1.9.1/gems/net-ssh-2.9.1/lib/net/ssh.rb:219:in `start'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/connection_pool.rb:50:in `call'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/connection_pool.rb:50:in `create_new_entry'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/connection_pool.rb:22:in `checkout'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:179:in `with_ssh'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:131:in `block in _execute'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `tap'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `_execute'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:66:in `execute'
/var/lib/gems/1.9.1/gems/capistrano-3.2.1/lib/capistrano/tasks/git.rake:17:in `block (3 levels) in <top (required)>'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `instance_exec'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `run'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/runners/parallel.rb:13:in `block (2 levels) in execute'
Net::SSH::AuthenticationFailed: Authentication failed for user [email protected]
/var/lib/gems/1.9.1/gems/net-ssh-2.9.1/lib/net/ssh.rb:219:in `start'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/connection_pool.rb:50:in `call'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/connection_pool.rb:50:in `create_new_entry'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/connection_pool.rb:22:in `checkout'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:179:in `with_ssh'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:131:in `block in _execute'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `tap'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `_execute'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:66:in `execute'
/var/lib/gems/1.9.1/gems/capistrano-3.2.1/lib/capistrano/tasks/git.rake:17:in `block (3 levels) in <top (required)>'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `instance_exec'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `run'
/var/lib/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/runners/parallel.rb:13:in `block (2 levels) in execute'
Tasks: TOP => git:check => git:wrapper
(See full trace by running task with --trace)
The deploy has failed with an error: #<SSHKit::Runner::ExecuteError: Exception while executing on host 127.0.0.1: Authentication failed for user [email protected]>

To avoid known_hosts problems root user has ~/.ssh/config

Host 127.0.0.1
    StrictHostKeyChecking no
    UserKnownHostsFile=/dev/null

As root user

ssh [email protected] 

goes just fine.

rc.local looks like this:

#!/bin/bash -ex
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exec > >(tee /var/log/rc.local.log|logger -t rc.local -s 2>/dev/console) 2>&1

/home/ubuntu/deployment/local-deploy.sh

exit 0

As said running /home/ubuntu/deployment/local-deploy.sh as root user manually works just fine but when it is run on boot (from rc.local) I get authentication error above.

Thanks for any help.


Solution

  • I've finally solved this with suing to ubuntu user and then executing command:

    su ubuntu -c '...'