格式:node
vagrant ssh [options] [name|id] [-- extra ssh args]
這將SSH導入正在運行的vagrant機器,並容許你訪問機器的shell。shell
userdeMacBook-Pro:~ user$ vagrant ssh -h Usage: vagrant ssh [options] [name|id] [-- extra ssh args] Options: -c, --command COMMAND Execute an SSH command directly 直接執行SSH命令 -p, --plain Plain mode, leaves authentication up to user 普通模式,將身份驗證留給用戶 -t, --[no-]tty Enables tty when executing an ssh command (defaults to true) 在執行ssh命令時啓用tty(默認爲true) -h, --help Print this help 打印幫助信息
1.ubuntu
1)在一個簡單的vagrant項目中,建立的實例將被命名爲default。即不使用實例的名字和使用default獲得的結果是同樣的,如:ssh
userdeMacBook-Pro:~ user$ vagrant ssh Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64) * Documentation: https://help.ubuntu.com/ New release '14.04.5 LTS' available. Run 'do-release-upgrade' to upgrade to it. Welcome to your Vagrant-built virtual machine. Last login: Mon Dec 24 07:37:13 2018 from 10.0.2.2 vagrant@precise64:~$ logout Connection to 127.0.0.1 closed.
使用default:ide
userdeMacBook-Pro:~ user$ vagrant ssh default Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64) * Documentation: https://help.ubuntu.com/ New release '14.04.5 LTS' available. Run 'do-release-upgrade' to upgrade to it. Welcome to your Vagrant-built virtual machine. Last login: Mon Dec 24 11:26:36 2018 from 10.0.2.2 vagrant@precise64:~$ logout Connection to 127.0.0.1 closed.
可見在同一個項目中都是打開同一個實例ui
2)在多機器設置中,你可使用vagrant狀態中顯示的名稱登陸到每一個vm,如:this
$ vagrant status Current machine states: node1 running (virtualbox) node2 running (virtualbox) This environment represents multiple VMs. The VMs are all listed above with their current state.
$ vagrant ssh node1 Welcome to your Vagrant-built virtual machine. Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2 vagrant@precise64:~$ logout Connection to 127.0.0.1 closed.
$ vagrant ssh node2 Welcome to your Vagrant-built virtual machine. Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2 vagrant@precise64:~$ logout Connection to 127.0.0.1 closed.
3)在運行來自不一樣項目的機器的系統中,你可使用流浪全局狀態中列出的id:spa
userdeMacBook-Pro:~ user$ vagrant global-status id name provider state directory ------------------------------------------------------------------------ 547b2b8 default virtualbox running /Users/user The above shows information about all known Vagrant environments on this machine. This data is cached and may not be completely up-to-date (use "vagrant global-status --prune" to prune invalid entries). To interact with any of the machines, you can go to that directory and run Vagrant, or you can use the ID directly with Vagrant commands from any directory. For example: "vagrant destroy 1a2b3c4d"
而後使用上面顯示的id-547b2b8運行機器:命令行
userdeMacBook-Pro:~ user$ vagrant ssh 547b2b8 Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64) * Documentation: https://help.ubuntu.com/ New release '14.04.5 LTS' available. Run 'do-release-upgrade' to upgrade to it. Welcome to your Vagrant-built virtual machine. Last login: Mon Dec 24 11:26:47 2018 from 10.0.2.2 vagrant@precise64:~$ logout Connection to 127.0.0.1 closed.
若是在命令行中找到--(兩個連字符),則在此以後的任何參數都將直接傳遞到ssh可執行文件中。這容許你傳遞任意命令,以便執行諸如反向隧道深刻到ssh程序中的操做。vagrant
2.SSH客戶機使用
vagrant將嘗試使用安裝在主機上的本地SSH客戶機。在POSIX機器上,必須安裝SSH客戶機,並在路徑上可用
對於Windows安裝,安裝程序鏡像中提供了SSH客戶機。若是在當前路徑上沒有找到SSH客戶機,vagrant將使用它提供的SSH客戶機。根據用於運行vagrant程序的本地環境,安裝程序提供的SSH客戶機可能沒法正常工做。例如,當使用cygwin或msys2 shell時,SSH客戶機在交互運行時將沒法按預期工做。安裝爲當前工做環境構建的SSH包將解決這個問題。
3.後臺執行
若是你指定的命令在後臺運行(例如將&追加到shell命令中),它幾乎會當即終止。這是由於,當Vagrant執行該命令時,它將在shell上下文中執行該命令,當shell退出時,全部子進程也將退出。
爲了不這種狀況,你須要將流程從shell中分離出來。請谷歌瞭解如何爲你的shell執行此操做。其中一種方法是nohup命令。
4.Pageant on Windows
SSH可執行文件將沒法訪問Windows上的Pageant。雖然vagrant能夠經過內部庫訪問Pageant,可是SSH可執行文件不支持Pageant。這意味着,當使用vagrant ssh命令時,Pageant中的鍵不可用於轉發。