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

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

    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.

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

    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

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

    My Env. is Win7 + Centos. The answer with most agreement doesn't work for me. After failing after trying ssh -p [port] [usrname]@127.0.01 , I just use XShell to add a new session with the vagrant port and user name.

    It works.

    Maybe Xshell is a candinate.

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

    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

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

    0 讨论(0)
提交回复
热议问题