How to ssh to vagrant without actually running “vagrant ssh”?

后端 未结 16 1574
清酒与你
清酒与你 2020-11-29 15:07

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

What

16条回答
  •  长情又很酷
    2020-11-29 15:35

    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.

提交回复
热议问题