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

后端 未结 16 1371
清酒与你
清酒与你 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:30

    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
    
    0 讨论(0)
  • 2020-11-29 15:31

    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
    
    0 讨论(0)
  • 2020-11-29 15:34

    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
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 15:37

    A lot of the other answers assume you have Vagrant installed.

    I have Vagrant installed on Windows 10, but I can't vagrant ssh because I'm using PuTTy as my SSH client, which vagrant won't accept.

    The ssh executable found in the PATH is a PuTTY Link SSH client. Vagrant is only compatible with OpenSSH SSH clients.

    However, in Windows 10 we also have Bash on Ubuntu on Windows. So, I just use that with the following command:

    ssh vagrant@127.0.0.1 -p2222 -i .vagrant/machines/default/virtualbox/private_key -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=Fatal
    

    It's easy enough to install Vagrant on Win10-Ubuntu but it also wants you to install Virtualbox for some reason, which I'd rather not do.

    N.B. I've tried with the ssh default -F vagrant-ssh-config method, but I just get

    Permission denied (publickey,password).

    I'm guessing this is because the IdentityFile path is a Windows path, whereas in Bash, it should begin with /mnt/c/. I suppose you could just write out the file and then modify it if that works better for you.

    0 讨论(0)
  • 2020-11-29 15:39

    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
    
    0 讨论(0)
提交回复
热议问题