Generally, we can find the IP address of a Linux machine using
ip command or
ifconfig command in the older Linux distributions. It doesn't matter whether it is a physical or a virtual machine. If we have access to the system, we could easily view its IP address with
ifconfig commands. How do you find a Vagrant machine IP address, without logging into it?
One way to find the IP address of a Vagrant box is to first log in to the Vagrant machine via ssh using command:
$ vagrant ssh
And then run use
$ ip a
$ hostname -I
This is the usual way to find the IP address of a Vagrant machine.
However, it is not necessary though. We can actually obtain the local network IP address for a VM running on Vagrant, without even having to ssh into it.
Find Vagrant machine IP address
First, you need to find the ID of the running Vagrant virtual machine using command:
$ 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"
As you see in the above output, AlmaLinux vagrant box is currently running and its ID is b2279ad.
To find this Vagrant machine's IP address, simply run:
$ 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
As you can see in the above output, the IP address of AlmaLinux 8 machine running on Vagrant is 192.168.122.143.
Similarly, you can find the other running Vagrant boxes' IP address as well.
For more details about Vagrant command usage, refer the following guide:
I came across a discussion in Stack overflow. There are many solutions given to find the IP address of Vagrant box in that thread.
One of the easiest way is to go to the Vagrant machine's project directory and run the following command:
$ vagrant ssh -c "hostname -I | cut -d' ' -f2" 2>/dev/null
Hope this helps.