Alternative ssh identity files with drush aliases

Posted on September 19, 2010 at 12:00 pm

I’ve now started to use drush almost exlusively to manage our Drupal instances. If you’re not familiar with drush it provides an excellent command line interface to Drupal.

One of the most powerful features in drush is site aliases. This allows you to setup shortcuts to your dev, staging and live servers. Once you’ve setup the aliases (see below), you can simply reference any of you servers with the @alias. For example, I’ve setup an alias to dev.begrand.net, so I can simply issue any drush command with @dev.begrand.net, for example:

drush @dev.begrand.net st

from my staging server and see what the status of my dev server is – which is nice. All drush commands accept the alias, so it saves a lot of time because I don’t have to ssh into each server as needed.

Creating an alias

In order to setup an alias, you need to be in the drush installation directory (mine is installed in ~/drush and symbolically linked to /usr/local/sbin, but yours may vary). Once there, create a new file called sitename.alias.drushrc.php (where sitename is the alias you want to use, in my case it’s dev.begrand.net so the full filename is dev.begrand.net.alias.drushrc.php).

The contents of the file should look like this:

<?php
  $aliases['dev.begrand.net'] = array (
    'root' => '/var/www',
    'uri' => 'dev.begrand.net',
    'remote-host' => 'dev.begrand.net',
    'remote-user' => 'devuser'
  );
?>

So far so good, but I’m using Amazon EC2, which relies of certificate-based ssh authentication. We use a non-standard name for our certificate files as well, so I needed a way of specifying the identity file for drush to use. In my case, the pem file is called dev.pem and is installed in the .ssh directory in my home directory (so ~/.ssh/dev.pem).

After a bit of searching around, I found the answer; it’s very very simple.

All I had to do was add and ‘ssh-options’ parameter to the aliases array, so my final sitename.alias.drushrc.php looked like this:

<?php
  $aliases['dev.begrand.net'] = array (
    'root' => '/var/www',
    'uri' => 'dev.begrand.net',
    'remote-host' => 'dev.begrand.net',
    'remote-user' => 'devuser',
    'ssh-options' => '-i ~/.ssh/dev.pem'
  );
?>

Job done! The more I use drush, the more I love it!

Tags: , ,

One Response to “Alternative ssh identity files with drush aliases”

 
  1. [...] Alternative ssh identity files with drush aliases « SlashZero Limited [...]

 

Leave a Reply