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条回答
倾城 Initia
2楼-- · 2019-01-07 02:28

Vagrant stores the private key in ~/.vagrant.d/insecure_private_key and uses it to connect to every machine through ssh, considering that it is configured to connect on port 2200 (default) it would be something like:

ssh vagrant@localhost -p 2200 -i ~/.vagrant.d/insecure_private_key

Note: make sure that the private key is owned by the user running Vagrant.

Though if your aim is to have a multi-machine environment you may do so using config.vm.define.

Here's an example illustrating an environment with 2 machines, one called web and the other is databases:

config.vm.define 'web', primary: true do |web|
        web.vm.box = 'CentOS64'
        web.vm.hostname = 'vic-develop'
        web.vm.network 'private_network', ip: '192.168.50.10', virtualbox__intnet: true
        web.vm.synced_folder '../code', '/var/www/project', :mount_options => ["dmode=777,fmode=777"]

        web.vm.provision 'ansible' do |ansible|
            ansible.playbook = 'development-web.yml'
            ansible.sudo = true
        end
end

config.vm.define 'databases' do |db|
    db.vm.box = 'CentOS64'

    db.vm.network 'private_network', ip: '192.168.50.20', virtualbox__intnet: true
    db.vm.network :forwarded_port, guest: 3306, host: 8206

    db.vm.provision 'ansible' do |ansible|
        ansible.playbook = 'development-db.yml'
        ansible.sudo = true
    end
end

Then you will have all Vagrant commands available per machine, i.e. vagrant ssh web and vagrant provision databases.

查看更多
闹够了就滚
3楼-- · 2019-01-07 02:29

I solved this in a very simple way: when you start the vagrant box it shows the ssh address like this

SSH address: 127.0.0.1:2222

then you can connect to the box by using the vagrant user, the host and the port you get

ssh vagrant@127.0.0.1 -p 2222
查看更多
迷人小祖宗
4楼-- · 2019-01-07 02:34

In terminal run

vagrant ssh

In another terminal window/tab run

ps aux | grep ssh

There you will see the actual command executed by Vagrant, something like this:

ssh vagrant@127.0.0.1 -p 2222 -o Compression=yes -o DSAAuthentication=yes -o LogLevel=FATAL -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -i ~/.vagrant.d/less_insecure_private_key -o ForwardAgent=yes
查看更多
你好瞎i
5楼-- · 2019-01-07 02:36

If you just want the bare minimum command to connect to your box, you need to know the port that it's using (printed when doing vagrant up, or visible doing vagrant ssh-config) and where's your private SSH key (also visible when doing vagrant ssh-config)

Then it's just a matter of providing the key and port:

ssh -p 2222 -i $HOME/vagrantenv/.vagrant/machines/default/virtualbox/private_key vagrant@127.0.0.1

查看更多
虎瘦雄心在
6楼-- · 2019-01-07 02:37

Just pass the entire vagrant ssh-config as a config file to ssh with the -F configfile parameter. The host alias to connect to is defined on the first line in vagrant ssh-config; Host default means you can connect with ssh default.

I couldn't see an option to read the config file from the standard input, so went with the temp file route. Here's a one-liner that also cleans up the temporary $TMPDIR.vagrant-ssh-config file afterwards. It needs to be executed in the same directory as your Vagrantfile, assuming you vagrant box is up and running.

vagrant ssh-config > $TMPDIR.vagrant-ssh-config && ssh default -F $TMPDIR.vagrant-ssh-config ; rm $TMPDIR.vagrant-ssh-config

Note: on my Mac OSX system, $TMPDIR expands to /var/folders/46/yltlhtgx8m5cg68_w95wgvy41324gn/T/ (right now). Use another variable, or another folder, if it's not set on your system.

查看更多
何必那么认真
7楼-- · 2019-01-07 02:39

There is a way that replicates how a remote user might login to the system

  1. Edit the Vagrantfile for your instance adding in

config.vm.network "private_network", ip: "192.168.33.10"

This adds a private IP for the host (make it what you wish in the 192.168 range so long as its not already used

  1. Restart the instance with vagrant reload from the command line
  2. Copy the vagrant private_key file to some Linux equivalent you should have running on your box (e.g. Cygwin if on windows, I use windows 10) to your cygwin home directory, renaming it along the way to something describing the host the key is to be used for, e.g.

your_virtual_host_name.pem

  1. You'll find the key under .vagrant\machines\default\virtualbox\private_key

  2. Go to your home directory and do your usual Unix ssh, so

ssh -i your_virtual_hostname.pem username@192.168.33.10

where username, may well be vagrant if you have a standard box, look at the output of vagrant ssh-config for ssh standard details for the box.

That's it

查看更多
登录 后发表回答