CLOVER🍀

That was when it all began.

Vagrantで起動した仮想マシンに、SSH接続する

これは、なにをしたくて書いたもの?

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で作成したファイルそのものを使わない場合は、HostNameUserIdentityFile、あとは
お好みでUserKnownHostsFileStrictHostKeyCheckingの値を確認して

$ 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

UserKnownHostsFileStrictHostKeyCheckingはなくてもいいのですが、ローカルのknown_hostsファイルに
書き込まれるのが嫌だったのと、仮想マシンのフィンガープリントが変わる度にyes/noを聞かれたくないかな、と。

こんなところでしょうか。

覚えておこうと思います。