How to ssh to vagrant without actually running “va

2019-01-07 01:54发布

I would like to reproduce the way how Vagrant logs to my VM within a shell script using ssh command, so I create an alias to my Vagrant instance.

What is the command syntax to use the regular ssh command to access it?

15条回答
淡お忘
2楼-- · 2019-01-07 02:23

If you just want to set it up so you can use normal the normal ssh commandline, as well as scp and such, you can run vagrant ssh-config and append the output to your default ssh configuration. If you replace the line "Host default" with a more descriptive hostname, you should be good to go.

vagrant ssh-config |sed -e "s/Host default/Host my_cool_dev_box/" >> ~/.ssh/config
ssh my_cool_dev_box
查看更多
老娘就宠你
3楼-- · 2019-01-07 02:23

You can take any of the ssh-config arguments, and pass them to ssh on the commandline as -o Key=value. So, for a simple one-host vagrant setup (you might have to do a little more work with grep or perl for a multihost setup), you can do something like the following (or replace perl with sed if you want):

ssh `vagrant ssh-config | tail -8 | perl -pe 's/^\s+/-o@/; s/\s/\=/;s/@/ /;s/\n/ /'` vagrant@localhost
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-07 02:24

There's a lot of answers already, but they all seem overly complicated or solve problems the asker didn't have.

simply:

# save the config to a file
vagrant ssh-config > vagrant-ssh

# run ssh with the file.
ssh -F vagrant-ssh default
查看更多
倾城 Initia
5楼-- · 2019-01-07 02:26

If you don't need to use stdin with ssh (for example you want to execute just a command and logout) you could use:

vagrant ssh-config --host default | ssh -F /dev/stdin default

This method was suggested in response to a similar question on google groups.

Unfortunately bash process substitution doesn't work either (see this question on unix.stackexchange for more details).

The best options you have, if you want an interactive shell, are to create a temp file and use that with ssh -F or use awk as suggested by the other answers.

查看更多
再贱就再见
6楼-- · 2019-01-07 02:27

I've had to re-implement "vagrant ssh" because it's -c option didn't pass on arguments properly. This is basically what it does (there might be more, but it works fine this way)

#!/bin/sh
PORT=$(vagrant ssh-config | grep Port | grep -o '[0-9]\+')
ssh -q \
    -o UserKnownHostsFile=/dev/null \
    -o StrictHostKeyChecking=no \
    -i ~/.vagrant.d/insecure_private_key \
    vagrant@localhost \
    -p $PORT \
    "$@"

As a one-liner (with thanks to kgadek):

ssh $(vagrant ssh-config | awk 'NR>1 {print " -o "$1"="$2}') localhost

To account for when you have more than one vagrant host, this will select the desired host, as well as cull blank lines from the config (using sed):

HOST=name-of-my-host
ssh $(vagrant ssh-config $HOST | sed '/^[[:space:]]*$/d' |  awk 'NR>1 {print " -o "$1"="$2}') localhost
查看更多
Deceive 欺骗
7楼-- · 2019-01-07 02:27

ssh vagrant@<host> password: vagrant

Examples:

  • ssh vagrant@vagrant.local

  • or after checking the IP (from the inside, using vagrant ssh) ssh vagrant@172.28.128.3

查看更多
登录 后发表回答