3

I am using drupalvm to manage my vagrant machines and I want to be able to be able to log into the vagrant machine and run drush to sync with the dev/stage/prod environments. I cannot do this without a bunch of extra setup to pass ssh keys and change usernames.

I would prefer to just pass the ssh username along with the drush command. Does drush have an argument that allows me to do this? I was unable to find it in the docs.

1

3 Answers 3

2

I haven't been using a vagrant setup for a while, but back when I did, I used aliases similar to the one below:

$aliases['ogm'] = array(
  'remote-host' => '127.0.0.1',
  'remote-user' => 'vagrant',
  'ssh-options' => '-p 2222 -i /home/ga/.vagrant.d/private_key',
  'root' => '/srv/www/ogm.postfix.local/drupal',
  'uri' => 'http://ogm.postfix.local',
);

Something like this should still work for your DrupalVM sites.

n.b. remote-user is the ssh username.

2
  • 1
    yes, drupalvm actually sets this all up for me, however, I would like to be able to ssh over to the vagrant box to run a sql-sync or rsync against prod or test to bring my local box up-to-speed with my remotes. We maintain users and keys for our remote prod and stage environments, so I would like to be able to pass that in from the command line when I run the drush command. bash [email protected] /var/www/drupal $ drush --remote-user="fandeson" sql-sync @stage.example.com @self I know it's possible with an alias, I want to know if it can be a passed argument Commented Jun 7, 2016 at 5:38
  • 1
    Also, if there is a better way to sync the db between a vagrant machine and remotes please let me know. Commented Jun 7, 2016 at 5:45
1

To expand Greg answer. Here is my Drush alias for one of my project.

Put this script in your host ~/.drush folder and rename to something like PROJECT.aliases.drushrc.php.

<?php
/**
 * Requirements:
 *   Make sure to upload your ssh key to prod and staging.
 *
 * Uploading of ssh key:
 *   $ ssh-copy-id username@hostname
 */
// Site stage environment.
$aliases['stage'] = array(
  'parent' => '@parent',
  'site' => 'PROJECT',
  'env' => 'stage',
  'uri' => 'STAGING_URL',
  'root' => '/path/to/drupal',
  'remote-host' => 'STAGING_URL', // hostname
  'remote-user' => 'USERNAME', // ssh user
);
// Site prod environment
$aliases['prod'] = array(
  'parent' => '@parent',
  'site' => 'PROJECT',
  'env' => 'prod',
  'uri' => 'PROD_URL',
  'root' => '/path/to/drupal',
  'remote-host' => 'PROD_URL', // hostname
  'remote-user' => 'USERNAME', // ssh user
);
// For local
$aliases['local'] = array(
  'parent' => '@parent',
  'site' => 'PROJECT',
  'env' => 'local',
  'uri' => 'LOCAL_URL',
  'root' => '/path/to/drupal',
  'ssh-options' => '-p 2222 -i /home/HOST_USERNAME/.vagrant.d/private_key',
  'remote-host' => '127.0.0.1', // hostname or IP of Vagrant, default to loopback
  'remote-user' => 'vagrant', // vagrant username, default to vagrant
);

To use this against stage and prod servers you need to upload your ssh-key first by doing ssh-copy-id user@hostname.

The to test if the script is working on your Vagrant machine.

$ drush @PROJECT.local status where PROJECT is the actual value of site.

To test on staging.

$ drush @PROJECT.stage status

And to test on prod.

$ drush @PROJECT.prod status

NOTE: You may need to clear your Drush cache first before running the above commands.

With Drush aliases you don't need to ssh on the target machine and execute Drush.

Hope this helps.

1
  • This is alot of good information on drush aliases, except I don't need info on drush aliases. I need to pass this parameter via the command line. Commented Jun 7, 2016 at 5:46
1

I have found a good number of ways to do this now.

First, to expand on @greg_1_anderson's answer. What I have done is install my remote aliases in my vagrant machine and then, using ssh agent forwarding, run drush from vagrant to connect to remotes. This only works because agent forwarding allows me to use my host keys on the vagrant machine and there is one modification to the alias that needs to be made:

$aliases['ogm'] = array(
   'ssh-options' => '-l frob',
);

Using ssh-option -l frob makes drush connect via ssh with my username which will match the key that is forwarded with ssh agent forwarding (this is default in DrupalVM now).

The second option is to set the username in the ssh-config on the vagrant guest.

In ~/.ssh/config:

HOST *.example.com
  USER frob

This second option allows me to not modify the drush alias at all; all ssh connections to *.example.com will use my host username as set in the ssh-config on the vagrant box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.