Skip to content

Instantly share code, notes, and snippets.

@shadyvb
Last active March 31, 2024 17:07
Show Gist options
  • Select an option

  • Save shadyvb/cd06ee086ab0f1f8e0898717a2f036f3 to your computer and use it in GitHub Desktop.

Select an option

Save shadyvb/cd06ee086ab0f1f8e0898717a2f036f3 to your computer and use it in GitHub Desktop.

Revisions

  1. shadyvb revised this gist Jun 3, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -23,3 +23,6 @@ curl https://gist.github.com/shadyvb/cd06ee086ab0f1f8e0898717a2f036f3
    - Do a `vagrant up` on any of your vagrant machines
    - Assuming your vagrant machine host name is: `client.local`
    - Try to `ssh cient.local`

    # Next up:
    - I've been expirementing with a magical dynamic `ssh vagrant` command that detects SSH connection information from the vagrant machine in the current folder, for which I've been playing with the `ProxyCommand` directive which can be scripted. But performance isn't the same as using the direct `ssh HOSTNAME.LOCAL` trick enclosed. Still an interesting idea nonetheless.
  2. shadyvb revised this gist Jun 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ This will let you SSH into any vagrant machine quickly, using its host name, and
    mkdir ~/.ssh/config.d &>/dev/null
    # Make sure it is included in your ~/.ssh/config
    bash -c "if ! grep -q 'Include ~/.ssh/config.d/*' ~/.ssh/config; then cp ~/.ssh/config ~/.ssh/config.backup; { echo 'Include ~/.ssh/config.d/*'; cat ~/.ssh/config } | tee ~/.ssh/config >/dev/null; fi"
    bash -c "if ! grep -q 'Include ~/.ssh/config.d/*' ~/.ssh/config; then cp ~/.ssh/config ~/.ssh/config.backup; { echo 'Include ~/.ssh/config.d/*'; cat ~/.ssh/config; } | tee ~/.ssh/config >/dev/null; fi"
    # Copy the global VagrantFile to your ~/.vagrant.d
    curl https://gist.github.com/shadyvb/cd06ee086ab0f1f8e0898717a2f036f3/raw/834fb36fe1fd99afeacf189886e89ee3d0e62640/VagrantFile > ~/.vagrant.d/VagrantFile
  3. shadyvb revised this gist Jun 3, 2019. 1 changed file with 1 addition and 7 deletions.
    8 changes: 1 addition & 7 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -11,13 +11,7 @@ mkdir ~/.ssh/config.d &>/dev/null
    bash -c "if ! grep -q 'Include ~/.ssh/config.d/*' ~/.ssh/config; then cp ~/.ssh/config ~/.ssh/config.backup; { echo 'Include ~/.ssh/config.d/*'; cat ~/.ssh/config } | tee ~/.ssh/config >/dev/null; fi"
    # Copy the global VagrantFile to your ~/.vagrant.d
    cat << EOF > ~/.vagrant.d/VagrantFile
    Vagrant.configure("2") do |config|
    config.trigger.after :up,
    name: "Caching vagrant ssh-config for '#{CONF['hosts'].join('\' and \'')}'",
    ruby: proc{|env,machine| puts `mkdir ~/.ssh/config.d &>/dev/null; vagrant ssh-config | sed 's/Host default/Host #{CONF['hosts'].join(" ")}/' | tee ~/.ssh/config.d/vagrant-#{CONF['hosts'][0]} >/dev/null; bash -c "if ! grep -q 'Include ~/.ssh/config.d/*' ~/.ssh/config; then echo '> Please add the following line to your ~/.ssh/config file: Include ~/.ssh/config.d/*'; fi"`}
    end
    EOF
    curl https://gist.github.com/shadyvb/cd06ee086ab0f1f8e0898717a2f036f3/raw/834fb36fe1fd99afeacf189886e89ee3d0e62640/VagrantFile > ~/.vagrant.d/VagrantFile
    ```
    # Explanation:

  4. shadyvb revised this gist Jun 3, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -10,8 +10,8 @@ mkdir ~/.ssh/config.d &>/dev/null
    # Make sure it is included in your ~/.ssh/config
    bash -c "if ! grep -q 'Include ~/.ssh/config.d/*' ~/.ssh/config; then cp ~/.ssh/config ~/.ssh/config.backup; { echo 'Include ~/.ssh/config.d/*'; cat ~/.ssh/config } | tee ~/.ssh/config >/dev/null; fi"
    # Copy the global VagrantFile to your ~/.vagrant
    cat << EOF > ~/.vagrant/VagrantFile
    # Copy the global VagrantFile to your ~/.vagrant.d
    cat << EOF > ~/.vagrant.d/VagrantFile
    Vagrant.configure("2") do |config|
    config.trigger.after :up,
    name: "Caching vagrant ssh-config for '#{CONF['hosts'].join('\' and \'')}'",
  5. shadyvb revised this gist Jun 3, 2019. 2 changed files with 0 additions and 2 deletions.
    1 change: 0 additions & 1 deletion VagrantFile
    Original file line number Diff line number Diff line change
    @@ -2,5 +2,4 @@ Vagrant.configure("2") do |config|
    config.trigger.after :up,
    name: "Caching vagrant ssh-config for '#{CONF['hosts'].join('\' and \'')}'",
    ruby: proc{|env,machine| puts `mkdir ~/.ssh/config.d &>/dev/null; vagrant ssh-config | sed 's/Host default/Host #{CONF['hosts'].join(" ")}/' | tee ~/.ssh/config.d/vagrant-#{CONF['hosts'][0]} >/dev/null; bash -c "if ! grep -q 'Include ~/.ssh/config.d/*' ~/.ssh/config; then echo '> Please add the following line to your ~/.ssh/config file: Include ~/.ssh/config.d/*'; fi"`}

    end
    1 change: 0 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,6 @@ Vagrant.configure("2") do |config|
    config.trigger.after :up,
    name: "Caching vagrant ssh-config for '#{CONF['hosts'].join('\' and \'')}'",
    ruby: proc{|env,machine| puts `mkdir ~/.ssh/config.d &>/dev/null; vagrant ssh-config | sed 's/Host default/Host #{CONF['hosts'].join(" ")}/' | tee ~/.ssh/config.d/vagrant-#{CONF['hosts'][0]} >/dev/null; bash -c "if ! grep -q 'Include ~/.ssh/config.d/*' ~/.ssh/config; then echo '> Please add the following line to your ~/.ssh/config file: Include ~/.ssh/config.d/*'; fi"`}
    end
    EOF
    ```
  6. shadyvb revised this gist Jun 3, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # TL;DR;

    This will let you SSH into any vagrant machine quickly, using your host name, and without further manual steps.
    This will let you SSH into any vagrant machine quickly, using its host name, and without further manual steps.

    ### Quick setup:
    ```
  7. shadyvb revised this gist Jun 3, 2019. 2 changed files with 34 additions and 15 deletions.
    17 changes: 2 additions & 15 deletions VagrantFile
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,6 @@
    # Apparently.. you can have a global VagrantFile!
    # .. that gets executed for all vagrant machines, and can be found/created
    # .. at `~/.vagrant.d/VagrantFile`.
    # .. Create/update the file to include the uncommented code below.
    #
    # Explanation: As a workaround to slow `vagrant ssh` calls,
    # .. this piece of code will auto-create a new `ssh_config` entry under `~/.ssh/config.d`
    # .. each time you do a `vagrant up`, so you can just `ssh HOSTNAME.LOCAL` any time and get
    # .. a milliseconds execution time, instead of the many seconds you get with `vagrant ssh`
    #
    # Pre-requiste: You need to have this line in your `~/.ssh/config` to allow loading of
    # .. custom ssh_config files in `~/.ssh/config.d`:
    # `Include ~/.ssh/config.d`

    Vagrant.configure("2") do |config|
    config.trigger.after :up,
    name: "Caching vagrant ssh-config for '#{CONF['hosts'].join('\' and \'')}'",
    ruby: proc{|env,machine| puts `mkdir ~/.ssh/config.d &>/dev/null; vagrant ssh-config | sed 's/Host default/Host #{CONF['hosts'].join(" ")}/' | tee ~/.ssh/config.d/vagrant-#{CONF['hosts'][0]} >/dev/null; bash -c "if ! grep -q 'Include ~/.ssh/config.d/*' ~/.ssh/config; then echo '> Please add the following line to your ~/.ssh/config file: Include ~/.ssh/config.d/*'; fi"`}
    end

    end
    32 changes: 32 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    # TL;DR;

    This will let you SSH into any vagrant machine quickly, using your host name, and without further manual steps.

    ### Quick setup:
    ```
    # Create the ~/.ssh/config.d folder
    mkdir ~/.ssh/config.d &>/dev/null
    # Make sure it is included in your ~/.ssh/config
    bash -c "if ! grep -q 'Include ~/.ssh/config.d/*' ~/.ssh/config; then cp ~/.ssh/config ~/.ssh/config.backup; { echo 'Include ~/.ssh/config.d/*'; cat ~/.ssh/config } | tee ~/.ssh/config >/dev/null; fi"
    # Copy the global VagrantFile to your ~/.vagrant
    cat << EOF > ~/.vagrant/VagrantFile
    Vagrant.configure("2") do |config|
    config.trigger.after :up,
    name: "Caching vagrant ssh-config for '#{CONF['hosts'].join('\' and \'')}'",
    ruby: proc{|env,machine| puts `mkdir ~/.ssh/config.d &>/dev/null; vagrant ssh-config | sed 's/Host default/Host #{CONF['hosts'].join(" ")}/' | tee ~/.ssh/config.d/vagrant-#{CONF['hosts'][0]} >/dev/null; bash -c "if ! grep -q 'Include ~/.ssh/config.d/*' ~/.ssh/config; then echo '> Please add the following line to your ~/.ssh/config file: Include ~/.ssh/config.d/*'; fi"`}
    end
    EOF
    ```
    # Explanation:

    - Apparently.. you can have a global VagrantFile! And that gets executed for all vagrant machines, and can be found/created at `~/.vagrant.d/VagrantFile`.
    - As a workaround to slow `vagrant ssh` calls, this piece of code will auto-create a new `ssh_config` entry under `~/.ssh/config.d` each time you do a `vagrant up`, so you can just `ssh HOSTNAME.LOCAL` any time and get a milliseconds execution time, instead of the many seconds you get with `vagrant ssh`
    - In order for your SSH config to pick up the new config files, you need to include the new folder in your `~/.ssh/config` file by adding the following to the beginning of the file: `Include ~/.ssh/config.d`

    # Try it!
    - Do a `vagrant up` on any of your vagrant machines
    - Assuming your vagrant machine host name is: `client.local`
    - Try to `ssh cient.local`
  8. shadyvb created this gist Jun 3, 2019.
    19 changes: 19 additions & 0 deletions VagrantFile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    # Apparently.. you can have a global VagrantFile!
    # .. that gets executed for all vagrant machines, and can be found/created
    # .. at `~/.vagrant.d/VagrantFile`.
    # .. Create/update the file to include the uncommented code below.
    #
    # Explanation: As a workaround to slow `vagrant ssh` calls,
    # .. this piece of code will auto-create a new `ssh_config` entry under `~/.ssh/config.d`
    # .. each time you do a `vagrant up`, so you can just `ssh HOSTNAME.LOCAL` any time and get
    # .. a milliseconds execution time, instead of the many seconds you get with `vagrant ssh`
    #
    # Pre-requiste: You need to have this line in your `~/.ssh/config` to allow loading of
    # .. custom ssh_config files in `~/.ssh/config.d`:
    # `Include ~/.ssh/config.d`

    Vagrant.configure("2") do |config|
    config.trigger.after :up,
    name: "Caching vagrant ssh-config for '#{CONF['hosts'].join('\' and \'')}'",
    ruby: proc{|env,machine| puts `mkdir ~/.ssh/config.d &>/dev/null; vagrant ssh-config | sed 's/Host default/Host #{CONF['hosts'].join(" ")}/' | tee ~/.ssh/config.d/vagrant-#{CONF['hosts'][0]} >/dev/null; bash -c "if ! grep -q 'Include ~/.ssh/config.d/*' ~/.ssh/config; then echo '> Please add the following line to your ~/.ssh/config file: Include ~/.ssh/config.d/*'; fi"`}
    end