Password-free deployments with Capistrano 2

by Damien on January 4, 2008

One of the benefits of Rails development is being able to do easy deployments using tools like Capistrano. It can, however, be a slight pain when with most setups you have to repeatedly enter different passwords, usually for different purposes:

  • Subversion checkout – yes, you your code should be password protected!
  • SSH login to the server
  • User password to restart the mongrel process

While you can avoid the last one by using a root account, “standard” security practices recommend using a non-root account for daily tasks (and disallowing the root user from logging in via SSH!), so we’re looking at three logins.

So lets get rid of those pesky prompts.

The first task is the code repository (e.g. subversion) checkout. Using an awesome project management tool like Unfuddle makes this a synch, just create a new user (e.g. “deploy”) and give it read-only access to the code and no other permissions. If your repository is managed by Apache or Subversion itself, check with the Subversion book for full instructions. Once you have the login password ready to go just store that in your code:

set :svn_user, "deploy"
set :svn_pass, "w1dg3t"
set :repository, "--username #{svn_user} --password #{svn_pass} https://myproject.unfuddle.com/svn/myproject/tags/deploy"

Simply replace “deploy” with your read-only username for the repository and “w1dg3t” with the password.”

The second task is the SSH login. Thankfully there’s a standard way of doing this with SSH – simply create your very own security key and paste the public file into a file called “.ssh/authorized_keys” in the home directory for the server’s deployment user (e.g. “~rails/.ssh/authorized_keys”). If you don’t follow that please use a more detailed guide (Kimmo Suominen wrote an excellent ssh guide for OSX, Linux et al, while the PuTTY manual explains key generation for Windows users), and note that for true password-free deployments use a blank passphrase.

The third and final step is to allow the deployment user on the server run the mongrel tasks without having to enter the password. This is done using the “sudo” system which allows a regular user run a command as the root user, i.e. nifty stuff but you need to be careful to not leave your system too open. There’s one small adjustment needed to make this work on the server without any passwords, you need to add a line to the /etc/sudoers file telling the server that it’s OK to allow your user to do this. So, here is the one line you need to add:

rails ALL = NOPASSWD: /usr/bin/mongrel_rails *

Just to explain that:

  • the “rails” word is the user to run the deployment as,
  • the “ALL” keyword allows you to run the command regardless of where you’re logged in from,
  • the “NOPASSWORD” keyword tells the system to not prompt for your password (as is the default),
  • then you tell it exactly where to find the mongrel_rails command (use the command “which mongrel_rails” if you don’t know off-hand),
  • lastly the star symbol allows for any arguments to be passed to the mongrel_rails command.

And that’s it. Just three quick steps and you’ll never have to enter those three passwords again.

Bookmark and Share

Other Posts That Might Interest You

  1. Ease Rails deployments with Phusion Passenger
  2. Subversion for Windows
  3. Windows Gotchas
  • Dave
    Took me ages to find a solution for this so posting it here, since this was a key blog to get me on the way:

    - deploy ability can be revoked from developer/machine as needed
    - svn access can be revoked as needed
    - developer knowledge of svn/deploy user passwords not required
    - sudo is not required
    - stored passwords not required
    - sftp to target required

    Thanks guys!

    http://www.experts-exchange.com/Programming/Lan...
  • @blake: Great advice. Thanks.
  • My team accomplishes the same thing without having to relax security with sudo or encode passwords directly into the Capistrano recipes. What we do instead is use a single user for doing all deployments. Instead of a password, we all have an SSH key installed in ~/.ssh/authorized_keys on each machine. To initialize Subversion access, I logged in once to the deploy user's account and did an svn ls https://server/path/to/svn and entered the credentials, which are cached into ~/.subversion/. Then we run our Rails processes as the deployment user, so there's no need to use sudo at all for working with Mongrel.

    This way lost laptops with source trees don't have keys to the kingdom and in the event of firing a developer, we just pull his SSH key from the shared account and he no longer has access. Problem solved.
blog comments powered by Disqus

Previous post:

Next post: