これは、なにをしたくて書いたもの?
Vagrantで起動した仮想マシンにSSH接続(およびSFTPなど)をしたいと思いまして(vagrant ssh
ではなく)。
このような用途のためにvagrant ssh-config
というコマンドがあるようなので、試してみました。
※というか、1度使ったことがあるのですがさらっと流したので忘れていました
vagrant ssh-config
vagrant ssh-config
のドキュメントはこちら。
vagrant ssh-config - Command-Line Interface | Vagrant by HashiCorp
まんま今回の目的で書いた内容が書かれています。
This will output valid configuration for an SSH config file to SSH into the running Vagrant machine from ssh directly (instead of using vagrant ssh).
仮想マシンに接続するための、ssh_config
ファイルの内容を出力することができます。
オプションとしては--host
があり、Host
に相当する部分を設定できます。
環境
今回の環境は、こちら。
$ vagrant version Installed Version: 2.2.19 Latest Version: 2.2.19 You're running an up-to-date version of Vagrant!
仮想マシンの起動
今回、確認に使用する仮想マシンを作成、起動します。
$ vagrant init generic/ubuntu2004 $ vagrant up
確認。
$ vagrant ssh vagrant@ubuntu2004:~$ hostname ubuntu2004.localdomain
vagrant ssh-configの結果を使って、SSH接続する
vagrant ssh-config
を使ってみます。こんな感じで出力されます。
$ vagrant ssh-config Host default HostName 192.168.121.75 User vagrant Port 22 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile /path/to/.vagrant/machines/default/libvirt/private_key IdentitiesOnly yes LogLevel FATAL
ちなみに、HostName
に記載されているIPアドレスは仮想マシンをdestroy
して再度作り直すと変わるので
注意しましょう。
--host
オプションでホスト名を指定すると、Host
の部分が変化します。
$ vagrant ssh-config --host myserver Host myserver HostName 192.168.121.75 User vagrant Port 22 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile /path/to/.vagrant/machines/default/libvirt/private_key IdentitiesOnly yes LogLevel FATAL
今回は、default
のまま使うことにします。
この出力結果を、適当なファイルに保存。
$ vagrant ssh-config > /path/to/vagrant-ssh-config
ちなみに、vagrant ssh-config
は仮想マシンが停止している状態だと失敗します。
$ vagrant ssh-config The provider for this Vagrant-managed machine is reporting that it is not yet ready for SSH. Depending on your provider this can carry different meanings. Make sure your machine is created and running and try again. Additionally, check the output of `vagrant status` to verify that the machine is in the state that you expect. If you continue to get this error message, please view the documentation for the provider you're using.
では、仮想マシンに接続してみます。
こんな感じでssh
コマンドを実行すると、成功します。接続時のホスト名は、今回はdefault
ですね。
$ ssh -F vagrant-ssh-config default Last login: Sat Jan 1 11:12:54 2022 from 192.168.121.1 vagrant@ubuntu2004:~$
-F
オプションでssh_config
ファイルのパスを指定しています。
SFTPの場合は、こちら。
$ sftp -F vagrant-ssh-config default Connected to default. sftp>
SSH、SFTP、SCP(もう非推奨ですが)でまとめて書くと、こんな感じですね。
## ssh $ ssh -F vagrant-ssh-config default ## sftp $ sftp -F vagrant-ssh-config default ## scp $ scp -F vagrant-ssh-config default:/path/to/remote /path/to/local $ scp -F vagrant-ssh-config /path/to/local default:/path/to/remote
vagrant ssh-config
で作成したファイルそのものを使わない場合は、HostName
、User
、IdentityFile
、あとは
お好みでUserKnownHostsFile
、StrictHostKeyChecking
の値を確認して
$ vagrant ssh-config Host default HostName 192.168.121.75 User vagrant Port 22 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile /path/to/.vagrant/machines/default/libvirt/private_key IdentitiesOnly yes LogLevel FATAL
直接指定、ですね。
$ ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /path/to/.vagrant/machines/default/libvirt/private_key vagrant@192.168.121.75
UserKnownHostsFile
とStrictHostKeyChecking
はなくてもいいのですが、ローカルのknown_hosts
ファイルに
書き込まれるのが嫌だったのと、仮想マシンのフィンガープリントが変わる度にyes/no
を聞かれたくないかな、と。
こんなところでしょうか。
覚えておこうと思います。