sudo(command, options={}, &block)

The sudo action is used to execute commands via sudo. It can be used in either of two ways (although one is deprecated). Although you could use sudo directly in your run invocations, this helper makes it possible for Capistrano to prompt you for the sudo password when the remote server(s) prompt for it.


This use of sudo does not actually execute anything. Instead, it returns a string containing the sudo command to execute. This lets you embed it in commands invoked via run, which in turn lets you build arbitrarily complex commands that use sudo.

run "#{sudo} apachectl restart"
run "#{sudo :as => 'bob'} crontab -l"
run "cd /u/apps/social && #{sudo} script/restart"
run "if [ ! -d /u/apps ]; then #{sudo} mkdir -p /u/apps; fi"



The options hash can be used to customize the string that is returned. Currently, only a single option is supported:

Specify the user to sudo as. This defaults to nil, which is typically the same as specifying "root" as the user.


The sudo helper will reference the following Capistrano variables:

Specify the path to sudo on the servers. This defaults to just "sudo", but if sudo on your servers is either in a non-standard location or is named something other than "sudo", you can set this variable accordingly.
Specify the password prompt (not the password itself!) that sudo should use when asking for a password. You shouldn't ever need to change this (the default is "sudo password: "), since this is only needed so that Capistrano can recognize the sudo prompt and pass the prompt on to the user. Setting the value of this variable to the empty string will cause Capistrano to not request any specific password prompt.

sudo(command, options={}, &block)

This use of sudo is deprecated and is included only for backwards compatibility with some Capistrano recipes. It is essentially identical to run, except that it will prefix the command with sudo (see the first use of the sudo.

sudo "apachectl restart"
sudo "crontab -l", :as => "bob"

Note that this syntax does not allow complicated shell script commands, or chaining commands together in a single invocation. See the first syntax for that.


This version uses the same arguments as the run, with the addition of :as, as described in syntax #1 above.

See Also