Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use config.ssh.forward_env in Vagrant?

I want certain environment variables from my host system to be visible inside my Vagrant VM, using the config.ssh.forward_env setting. I'm using Vagrant 1.8.1. Here's my Vagrantfile:

Vagrant.configure(2) do |config|
  config.vm.box = 'ubuntu/trusty64'
  config.ssh.forward_env = ['FOO']
end

After creating it, I ran these commands:

vagrant up
export FOO=bar
vagrant ssh -c 'echo $FOO'

I expected the final line to output bar, but instead it outputs a blank line (followed by Connection to 127.0.0.1 closed.). What am I doing wrong?

like image 662
Taymon Avatar asked Mar 21 '16 02:03

Taymon


People also ask

How do I connect to SSH with vagrant?

Simply CMD-SHIFT-P then "Remote-SSH: Connect to Host..." and the ssh . config entry you just added is automatically listed - you simply select it and voila, vscode connects to your remote vagrant vm!

How do I generate a SSH key in vagrant?

The best way to add your pub_key is to use ssh-copy-id binary/command: $ ssh-copy-id -i ~/. ssh/id_rsa. pub -p 2222 vagrant@localhost /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/<username>/.

What port does vagrant use for SSH?

By default this is port 22. config. ssh.


1 Answers

TL;DR

fhenri@machine$ export LC_MYVAR=TEST
fhenri@machine$ vagrant ssh -c 'echo $LC_MYVAR'
TEST
Connection to 127.0.0.1 closed.
fhenri@machine$ 

As said in the doc, config.ssh.forward_env works as sendEnv so to pass variables using sendEnv you must configure your host to accept env variables, by default (and hoping my example above should work) the common setup allows LC_* through, you can review the authorized variables in /etc/ssh/sshd_config

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

you can add your own variables here as needed or use default prefix LC_

like image 170
Frederic Henri Avatar answered Nov 15 '22 11:11

Frederic Henri