通常、LinuxマシンのIPアドレスはip
を使用して見つけることができます コマンドまたはifconfig
古いLinuxディストリビューションのコマンド。物理マシンであるか仮想マシンであるかは関係ありません。システムにアクセスできる場合は、ip
を使用してそのIPアドレスを簡単に表示できます。 またはifconfig
コマンド。ログインせずに、VagrantマシンのIPアドレスをどのように見つけますか?
VagrantボックスのIPアドレスを見つける1つの方法は、最初にコマンドを使用してssh経由でVagrantマシンにログインすることです。
$ vagrant ssh
次に、use ip
を実行します またはifconfig
またはhostname
コマンド:
$ ip a
または、
$ ifconfig
または、
$ hostname -I
これは、VagrantマシンのIPアドレスを見つける通常の方法です。
ただし、必須ではありません。 Vagrantで実行されているVMのローカルネットワークIPアドレスを、SSHで接続しなくても実際に取得できます。
まず、コマンドを使用して実行中のVagrant仮想マシンのIDを見つける必要があります:
$ vagrant global-status
出力例:
id name provider state directory ---------------------------------------------------------------------- f4904ad default libvirt shutoff /home/sk/Vagrant/Archlinux 831f9c0 default libvirt shutoff /home/sk/Vagrant/Gentoo 3587422 default libvirt shutoff /home/sk/Vagrant/Rhel8 b2279ad default libvirt running /home/sk/Vagrant/Almalinux8 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"
上記の出力でわかるように、AlmaLinux vagrantボックスは現在実行中であり、そのIDは b2279ad です。 。
このVagrantマシンのIPアドレスを見つけるには、次のコマンドを実行するだけです。
$ vagrant ssh-config b2279ad
出力例:
Host default HostName 192.168.122.143 User vagrant Port 22 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile /home/sk/Vagrant/Almalinux8/.vagrant/machines/default/libvirt/private_key IdentitiesOnly yes LogLevel FATAL

上記の出力からわかるように、Vagrantで実行されているAlmaLinux8マシンのIPアドレスは192.168.122.143です。 。
同様に、実行中の他のVagrantボックスのIPアドレスも見つけることができます。
Vagrantコマンドの使用法の詳細については、次のガイドを参照してください:
- Vagrantチュートリアル–LinuxでVagrantを使い始める
ボーナスのヒント
Stackoverflowでの議論に出くわしました。そのスレッドでVagrantボックスのIPアドレスを見つけるために与えられた多くの解決策があります。
最も簡単な方法の1つは、Vagrantマシンのプロジェクトディレクトリに移動して、次のコマンドを実行することです。
$ vagrant ssh -c "hostname -I | cut -d' ' -f2" 2>/dev/null
これがお役に立てば幸いです。
関連記事:
- Vagrantでネットワークを構成する方法
- KVM仮想マシンのIPアドレスを見つける方法