I want to write a recipe with Capistrano 3 executing a task on the remote server with sudo.
With Capistrano 2 this could be done for example:
default_run_options[:pty] = true
task :hello do
run "#{sudo} cp ~/something /something"
end
With Capistrano 3 I found:
set :pty, true
But I could not get to execute a task running with sudo.
How can I run a task with sudo?
The Capistrano 3 guide recommends the use of passwordless sudo. This allows your less-priveleged user execute the sudo command without having to enter a password via the PTY.
You can use the task that Kentaro wrote above, and add something like the following to your /etc/sudoers file:
http://www.capistranorb.com/documentation/getting-started/authentication-and-authorisation/#toc_8
To resolve this issue I needed to add
set :pty, true
to mydeploy.rb
file.I can now run the following:
This task basically runs a shell script called
restart.sh
that has a command withinsudo service nginx restart
.If you really need to use
sudo
you can always map the command likeSSHKit.config.command_map[:rm] = 'sudo rm'
which will makeexecute :rm
into the properrm
command invoked withsudo
. If your deploy user is in the sudoers things will work as expected.I usually write like this:
Edit
Capistrano 3 does not support sudo with password.
However, I created a small gem, which enables you to use sudo with password in Capistrano 3 task.
Add sshkit-sudo to your application's Gemfile:
And require 'sshkit/sudo' in you Capfile:
Now, you can execute a command with sudo as follows:
you want "as user do end", like