Setting Up and Managing Multiple SSH Accounts for Drush on Mac/Linux

It may be the case, you have multiple ssh connections some for github accounts and some for one or more virtual boxes for Drupal sites. In this post, we cover how to generate, configure and manage multiple SSH accounts for drush. In addition, we go over how to overcome the error ‘ssh-copy-id: command not found ‘ that happens for Mac machines when trying to setup ssh keys via drush command ‘pushkey’

Generating Public/Private SSH Key Pair

For each different ssh account, you would generate ssh public/private key pair as following:

ssh-keygen -t rsa -C "user@hostname.com"

By default, this will store the generated key pairs(two files one with .pub extensions) in the directory you run the command from. We moved it into $HOME/.ssh directory and we will be referring to this directory in this post

Manage SSH Keys

You can manage multiple SSH keys by creating host aliases for each ssh key pair. The host aliases are defined in the $HOME/.ssh/config file and all they do is reference the keys per host alias as following

Host virtual-box-main
        HostName hostdomain.com
        User user-name
        IdentityFile ~/.ssh/id_rsa_virtual_box

Host another-host-alias
        HostName some-remote-box.com
        User ssh-user
        IdentityFile ~/.ssh/private_key_gen_above

Here the host alias is ‘virtual-box-main’. The ‘hostname’ is actual domain name to the server you like to ssh. The ‘user’ is the actual ssh user account that will be used to ssh into the server. At last, the ssh key pair is referenced via ‘IdentityFile’ that was generated in the above step.

So, you can repeat the above steps to generate ssh key pairs for each ssh remote account. Then configure host alias by adding another entry as specified above for each different ssh login you need.

Setup Drush

Once you have the ssh host alias setup, then to configure drush via the alias(one location $HOME/.drush/aliases.drushrc.php) as following:

$aliases['ds.prod'] = array(
	'remote-host' => 'virtual-box-main',
	'remote-user' => 'root',
	'root' => '/home/websites/site',
	'uri' => 'http://site.com',
    );

Here the host alias configure in ssh is specified ‘remote-host’, so when drush makes the ssh connection it will look it up the alias to identify ssh key to use for login

If you try to connect with the current setup:

drush @ds.prod st

You will get following error:
Permission denied (publickey,password).

This is because the remote machine needs to have the ssh public key in order to be able authenticate. Lets push the key:

drush pushkey @ds.prod

This function copies the ssh key into the server for ssh authentication.

For Mac, this will result into the following error because Mac doesn’t have the function ‘ssh-copy-id’
ssh-copy-id: command not found
So, solution is to copy ssh key manually

Copy Public SSh Key Manually

To copy the public ssh key, run the following:

cat id_rsa_virtual_box.pub | ssh user@virtualbox.com 'cat >> .ssh/authorized_keys' 

This pipes the ssh key and then logs into the virtual server and pasts the key into authorized_keys file. You may be missing the .ssh folder on the remote server. In that case, you make it create one as following:

cat id_rsa_virtual_box.pub | ssh user@virtualbox.com 'umask 077; mkdir -p .ssh ; cat >> .ssh/authorized_keys'

Now, running:

drush @some.alias st

It should be displaying the status of the site on remote server

Troubleshooting

1. Test SSH Authentication

To test any of your SSH Aliases setups:

ssh virtual-box-main

Here, we test the alias ‘virtual-box-main’. This will prompt for password if logging in for the first time. All other times it logs you in without password
Note: for ssh services that has turned off interactive mode such as Git, you use -T flag:

ssh -T git-ssh-alias

This will confirm if your ssh is setup correctly or not

2. Permission denied (publickey,password).

You will get this message if you haven’t set up the ssh as described in this post

3. ‘pushkey’ could not be found drush

This is because the push key function is addition and needs to be downloaded as following:

drush dl drush_extras

This will download and install drush_extras module that includes “pushkey” command

4. “sudo: no tty present and no askpass program specified”

This is issue comes up when you open ssh without tty session. It can be solved in 2 ways depending on how much access you have on remote instance or whatever the remote instance has the capability of NOPASSWORD

  1. Turn on NOPASSWORD on remote instance as described here:
    http://askubuntu.com/questions/192050/how-to-run-sudo-command-with-no-password/443071#443071
  2. Configure Drush to pass in credentials at time of ssh-ing. Its being done by editing .drush/drushrc.php and adding following line:
    $options['ssh-options'] = '-o PasswordAuthentication=no -i $HOME/.ssh/id_rsa/lamp_vbox';
    

    Here the lamp_vbox is the private key generated by ssh-keygen

5. WARNING: UNPROTECTED PRIVATE KEY FILE!

The permissions need to be reset:

sudo chmod 600 /path/.ssh/id_rsa/lamp_vbox
sudo chmod 600 /path/.ssh/id_rsa/lamp_vbox.pub
sudo chmod 644 /path/.ssh/known_hosts
sudo chmod 755 /path/.ssh
References

http://mail-archives.apache.org/mod_mbox/hadoop-mapreduce-user/201210.mbox
http://blogs.uoregon.edu/developments/2012/06/21/bash-function-ssh-copy-id-for-mac/
http://www.drush.org/sites/default/files/attachments/DGD7-Drush.pdf
http://stackoverflow.com/questions/3844393/what-to-do-about-pty-allocation-request-failed-on-channel-0

How To Install Drush

Drush is command line utility for installing, maintaining and troubleshooting Drupal platform. This post logs the steps for installing drush on Mac(OS X 10.7.5)/Linux Ubuntu and Windows as well

Linux & Mac

Install Drush

To install the Dev or the most current version for Drush:
1. Clone Drush git repository

sudo git clone https://github.com/drush-ops/drush.git /root/tools/drush

2. Put Drush executable in the search path:

sudo ln -s /root/tools/drush/drush /usr/local/bin/drush

If you don’t know the search locations, then look it up variable $PATH that lists all the search locations for executables:

echo $PATH

Drush is using composer to deploy, so lets install composer as following:

sudo curl -sS https://getcomposer.org/installer | sudo php

this will download the composer. It displays the location that you will need next step

mv dir/downloaded/composer.phar /usr/bin/composer

Here, you move the composer into a path that looks for executables as specified by $PATH, so it can be found. At last, lets install drush

cd /path/to/drush
composer install

DONE! Test it by running ‘drush –v’ which should display current version

Upgrade Drush From Legacy Install

1. Clone Drush git repository

sudo git clone https://github.com/drush-ops/drush.git drush

2. Find where the executable is currently used:

which drush

This will display path of current drush executable. Go to that directory, rename or delete it and create new link to the new version of Drush cloned in Step 1 as following

sudo ln -s /Users/margots/DevTools/drush/drush drush

Here, the path is to the new version of drush cloned in step 1

Done!!


Old Way

Prerequisites

  • wget -or-
  • unzip -or-
  • git -or-

Step-1: Installing Prerequisites.

Verify Unzip installed by running ‘unzip’ from command line. If it isn’t installed then:

sudo apt-get install unzip

Verify wget is installed by running ‘wget’ from command line. If it isn’t installed then:

curl -O http://ftp.gnu.org/gnu/wget/wget-1.14.tar.gz
sudo tar -xzf wget-1.14.tar.gz
cd wget-1.14
sudo ./configure --with-ssl=openssl
sudo make
sudo make install
Stept-2: Installing Drush with PEAR

To verify PEAR(PHP Extension and Application Repo)┬áis installed type ‘pear version’ in command line. If it doesn’t exist then:
For Mac:

cd /usr/local
sudo wget http://pear.php.net/go-pear.phar
sudo php -d detect_unicode=0 go-pear.phar
sudo pear upgrade --force pear
sudo pear upgrade --force Console_Getopt Console_Table
sudo pear upgrade-all

For Ubuntu:

sudo apt-get install php-pear
sudo pear upgrade --force pear
Step-3: Installing Drush

To install drush:

sudo pear channel-discover pear.drush.org
sudo pear install drush/drush
which drush
drush

If you see the following message – ‘Drush needs to download a library from [..]Console_Table-1.x.x.tgz[..]’ with error, then:

sudo rm -Rf ~/.drush
Upgrade drush

To upgrade drush:

sudo pear upgrade drush

Windows

Prerequisites

  • cygen

Installing Drush

Run Windows installer for Drush listed at http://drush.ws/drush_windows_installer

Configure Drush for Cygen

To run drush from Cygen, we mount the path and then create shell alias. To mount path to drush in the Cygen, add the following in the /etc/fstab

C:\ProgramData\Drush\drush.php /cygdrive/c/ProgramData/Drush/drush.php binary,posix=0,user 0 0

Next, we create alias – drush by adding the following in .bashrc in your home dir

alias drush='/cygdrive/c/ProgramData/Drush/drush.php'

Afterwards, reload Cygen shell and run ‘drush –version’. It should display the drush version which is good way to verify if drush is working

Troubleshooting

1. Tip

If you run into any issues while trying to install some package, make sure you run ‘sudo apt-get update’

2. Unable to load autoload.php. Drush now requires Composer in order to install

This happen after upgrading drush. Solution is to install composer as following:

sudo curl -sS https://getcomposer.org/installer | sudo php

this will donwload the composer. It displays the location that you will need next step

mv dir/downloaded/composer.phar /usr/bin/composer

Here, you move the composer into a path that looks for executables as specified by $PATH, so it can be found. At last, lets install drush

cd /path/to/drush
composer install

The This should solve the problem

MS-DOS style path detected: C:\path\drush.php
Preferred POSIX equivalent is: /cygdrive/c/path/drush.php

You have to mount and then refer the drush in the Cygen. See step – Configure Drush for Cygen above

Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.

For details, please, see post – . To fix it, run:

composer update --lock

This will update the lock file to get rid of the error message,so you can proceed with the installation

[RuntimeException] vendor does not exist and could not be created

This message is given when the drush directory(or dir you try to run the composer installer) doesn’t have write permissions. To fix this:

sudo chmod -R 777 DIR
Unable to send e-mail drupal

To turn off drupal sending email add the following line to the php.ini

sendmail_path = /bin/true
[UnexpectedValueException] Could not parse version constraint ^2.6.3: Invalid version string

To solve this error, update the composer currently installed:

composer self-update
the requested PHP extension pcntl is missing from your system drush

The error came when installing drush via composer “composer install”. To solve, I deleted the composer.lock and rerun the installer “composer install –dev”

References

http://duntuk.com/how-install-drush-github-after-drupal-project-removal
https://drupal.org/node/1674222