pátek 21. března 2025

Remote kernels - when having multiple network interfaces

For Mathematica Kernels Version 13 and later

kernel = KernelConfiguration["ssh://192.168.213.7", 
     "KernelCommand" -> "/opt/Wolfram/Mathematica/13.1/Executables/MathKernel",
     "KernelCount" -> 10]

Remote kernels

LaunchKernels[kernel];
ParallelEvaluate[{$KernelID, $MachineName, $Version}]

Local kernels

LaunchKernels[4];
ParallelEvaluate[{$KernelID, $MachineName, $Version}] 

Close remote kernels and then close all kernels.

$KernelCount
CloseKernels["RemoteKernels"];
$KernelCount
CloseKernels[]; $KernelCount

For Mathematica Kernels Before Version 13

The Mathematica kernel uses SSH to start the remote kernel by providing the local IP address and two randomly selected ports. These values are passed to the remote kernel(s) as command-line parameters. The remote kernel then attempts to connect back to the specified address and ports.

This setup works only if:

  1. The required ports are open on the firewall.
  2. The local IP address is accessible from the remote machine.

The main difficulty arises when the local machine has multiple IP addresses. In such cases, it is not possible to specify which IP address Mathematica should use for the link, the FrontEnd simly selects one. Then, for example, the address used by local virtual machines (e.g., 192.168.120.xx/24) may not be accessible from the remote machine.

However, it appears that the Mathematica front end selects the IP address of the last interface (alphabetically) when sorted. For instance, if you have:

  • eth1 with 10.0.0.1
  • wifi0 with 192.168.0.2

Then the remote kernel will be launched like this:

ssh 10.0.0.2 MathKernel ... -linkname 46683@192.168.0.2,34039@192.168.0.2 -subkernel

—even though there’s no reason to expect that the remote machine (10.0.0.2) has a route to your local Wi-Fi network.

Solutions (Root Access Required on Local or Remote Machine)

1) [Untested] Add a Route on the Remote Machine

ip route add 192.168.0.1 via 10.0.0.1

2) Rename Interfaces so the Desired One is Last Alphabetically

When running the remote kernel through a VPN (e.g., WireGuard), this renaming can be quite simple.

Alternative: Use a Virtual Interface Bound to a Specific IP

If your connection has multiple IP addresses, you can assign one of them to a dedicated virtual interface using macvlan. This allows you to control how Mathematica selects the IP.

Original State:
3: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether xx:f6:4b brd ff:ff:ff:ff:ff:ff
    altname enp0s31f6
    inet 1.2.3.4/23 brd 1.2.3.255 scope global eno1
       valid_lft forever preferred_lft forever
    inet 192.168.214.5/24 brd 192.168.214.255 scope global eno1
       valid_lft forever preferred_lft forever
Then run:
ip addr del 192.168.214.5/24 dev eno1
ip link add zz1 link eno1 type macvlan mode bridge
ip addr add 192.168.214.5/24 dev zz1
ip link set zz1 up
Resulting Configuration:
3: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether xx:f6:4b brd ff:ff:ff:ff:ff:ff
    altname enp0s31f6
    inet 1.2.3.4/23 brd 1.2.3.255 scope global eno1
       valid_lft forever preferred_lft forever

9: zz1@eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether xx:b9:ec brd ff:ff:ff:ff:ff:ff
    inet 192.168.214.5/24 scope global zz1
       valid_lft forever preferred_lft forever

Finally, open the firewall — and voilà!

The code for /etc/network/interfaces

auto zz1
iface zz1 inet static
	address 192.168.214.5/24
        pre-up ip link add zz1 link eno1 type macvlan mode bridge
        post-down ip link delete zz1
#	post-up iptables -A INPUT -i zz1@eno1 -s 192.168.214.5/24 -j ACCEPT