There are two different factors in determining what is and isn't within a ROS 2 network. The first is the domain ID, and the second is the subnet.
The domain ID is controlled by setting the "ROS_DOMAIN_ID" environment variable or (within MATLAB) by providing the domain ID as an argument. If not provided, and the environment variable is unset, MATLAB assumes the default domain ID of 0 should be used. This is the default domain used by Fast-RTPS, MATLAB's ROS 2 DDS implementation, but other DDS implementations may use a different default domain ID. It probably wouldn't hurt to set the same ROS_DOMAIN_ID value in your VM and on MATLAB, just to ensure they are on the same network.
To clarify, introspection functions (like "ros2 topic list") only apply within the specified domain ID. You cannot introspect into multiple ROS 2 networks at the same time.
The other factor, the subnet, is determined by the IP address and subnet mask of the machines themselves. By default, the subnet mask for a system is typically 255.255.255.0, meaning that the subnet is defined as all machines with an IP address that have the first three "segments" identical. So if your machine has IP address 220.127.116.11, and another has IP 18.104.22.168, then those are on the same subnet. However, a machine with IP 22.214.171.124 would not be on the same subnet.
Verify that both your host computer and your VM are on the same subnet (ipconfig on Windows, ifconfig on Unix). If they are not on the same subnet, you make ROS 2 consider them viable targets for inclusion in a network by creating a DEFAULT_FASTRTPS_PROFILES.xml file in your active directory, as detailed here. Make sure the domain ID specified in that file matches the one you use for your network, and you create the file on both machines.
Another issue we have seen with VMs is the Network Adapter connection setting used. Generally, if you are running MATLAB on the host computer that the VM is running on as well, we've seen NAT mode to work well (the VM should share an IP with the host computer, and therefore should be on the same subnet by default). However, if you have a VM running ROS 2, and want to connect to that network from a different physical machine (or multiple machines), you may need Bridged mode (which gives the VM its own IP, and makes it possible to not be on the same subnet).
Other suggested troubleshooting steps:
- Ensure that you have sourced correctly the setup script from the ROS 2 install on the VM
- !ping from within MATLAB the VM's IP address
- Ping from the VM the host machine's IP address
- Try both NAT and Bridged mode, regardless of your setup
- Try creating a node on MATLAB, and see if "ros2 node list" on the VM is able to see it
- After your first call in MATLAB to an introspection command ("ros2 ...") on a specific domain ID, give it a few seconds, and then try again. The introspection calls create an introspection node the first time they are used, and they may not have picked up all the meta-communication from the network when just created.
- "Reset" the introspection node by calling it for a different (unrelated) domain ID, then call it again for the correct domain ID (repeat the point above if necessary).
Hope this helps give some clarity and next-steps.