Lima is an open-source virtualization tool designed to run Linux virtual machines (VMs) on macOS. It serves as a lightweight alternative to Docker Desktop, leveraging the native Virtualization.framework or QEMU as backend options. Networking is a critical component of Lima, determining how the host macOS system interacts with the guest Linux VMs and external networks. This guide delves into the various network configurations available in Lima, focusing particularly on host networking, and provides detailed instructions on setting up and managing these networks effectively.
By default, Lima employs user-mode networking, also known as slirp. This mode is enabled out of the box and offers a simple setup without requiring administrative privileges.
192.168.5.15
, residing within the 192.168.5.0/24
subnet.192.168.5.2
, which is also reachable using the hostname host.lima.internal
.The Host Network Mode allows the Lima VM to share the host macOS's network interface, effectively bridging the VM directly to the host's network. This configuration provides the VM with a unique IP address within the same network as the host, facilitating seamless communication and higher network performance.
networks.yaml
ConfigurationTo enable host networking, edit the networks.yaml
file located typically at ~/.lima/_config/networks.yaml
. Add or modify the network configuration as follows:
networks:
host:
mode: host
gateway: 192.168.106.1
dhcpEnd: 192.168.106.254
netmask: 255.255.255.0
This configuration sets up a host network with a specified gateway and DHCP range. The mode: host
directive is crucial for enabling host networking.
socket_vmnet
socket_vmnet
is a helper utility that facilitates shared networking by bridging the host's network with the VM. To install it, execute the following commands:
git clone https://github.com/lima-vm/socket_vmnet.git
cd socket_vmnet
sudo make PREFIX=/opt/socket_vmnet install
Ensure that socket_vmnet
is installed in a secure and appropriate location, avoiding package managers like Homebrew for better control and security.
For Lima to manage the socket_vmnet
daemon, password-less sudo access is required. Update the sudoers file using the following command:
limactl sudoers | sudo tee /etc/sudoers.d/lima
Warning: Modifying the sudoers file can compromise system security if done incorrectly. Ensure the commands are executed precisely.
Initiate the Lima VM referencing the host network configuration:
limactl start --network=host
If using a specific configuration file (e.g., docker.yaml
), ensure the network mode is set to host
within that file.
After starting the VM, confirm that the Lima VM has successfully obtained an IP address within the host's network range:
limactl shell ifconfig
Replace <instance_name>
with your actual Lima instance name. The VM should display its network interfaces with the assigned IP addresses.
socket_vmnet
Shared networking leverages socket_vmnet
to bridge the host macOS network with the Lima VM, offering enhanced performance and flexibility compared to user-mode networking.
socket_vmnet
: As detailed above, clone the repository, build, and install it.networks.yaml
: Ensure that the host network is correctly defined with the appropriate gateway and netmask settings.Bridged networking connects the VM directly to the physical network, allowing it to obtain its own IP address from the network's DHCP server. This mode offers the highest level of network transparency and is ideal for scenarios requiring the VM to be an independent entity on the network.
With the transition from vde_vmnet
to socket_vmnet
in Lima v0.12, networking performance saw a significant boost, increasing from 0.31 Gbps to 1.23 Gbps in bridged mode.
Port forwarding allows services running inside the Lima VM to be accessible from the host macOS or external networks. This is essential for web servers, APIs, and other networked applications.
lima.yaml
Configuration File:
Add or modify the portForwards
section to map guest ports to host ports.
portForwards:
- guestPort: 80
hostPort: 8080
This example maps port 80 on the guest VM to port 8080 on the host.
Apply the port forwarding changes by restarting the Lima instance:
limactl stop
limactl start
From the macOS host or an external client, access the forwarded service using localhost:8080
.
curl http://localhost:8080
To access services running on the macOS host from within the Lima VM, use the gateway IP address 192.168.5.2
or the hostname host.lima.internal
.
If a web server is running on the host at localhost:8080
, it can be accessed from the VM using http://192.168.5.2:8080
:
curl http://192.168.5.2:8080
This ensures that applications within the VM can interact with services on the host seamlessly.
To view all available networks and their configurations, use the following command:
limactl list
This command displays running Lima instances along with their associated network settings.
To add a new network, define it in the networks.yaml
file and restart the Lima VM:
networks:
- shared:
mode: shared
varRun: /private/var/run/lima
sudoers: /private/etc/sudoers.d/lima
After updating the configuration:
limactl restart
To remove an existing network, delete its configuration from the networks.yaml
file and restart the Lima VM.
This ensures that the network is no longer active and resources are freed appropriately.
File sharing is integral for seamless development workflows, enabling easy access to files between the host and the VM.
Lima automatically mounts the $HOME
directory from macOS into the VM under /mnt/lima-guestagent
.
For advanced setups, additional directories can be mounted using the mounts
section in the lima.yaml
configuration:
mounts:
- location: "/path/to/host-directory"
writable: true
This allows specific directories from the host to be accessible and writable within the VM, enhancing flexibility and productivity.
host
or shared
) in the networks.yaml
file.ip addr show
and ip route show
to verify proper IP assignment and routing.portForwards
section in the lima.yaml
file for accuracy.socket_vmnet
is correctly installed and accessible.limactl sudoers | sudo tee /etc/sudoers.d/lima
.lima0
) are active and operational.limactl list
: Lists all running Lima instances and their configurations.limactl shell <instance_name>
: Opens a shell session within the specified Lima VM.ping <host>
: Tests connectivity to the host.curl <address>
: Tests access to specific services or addresses.Lima offers robust and flexible networking configurations tailored to various use cases on macOS. Whether utilizing the default user-mode networking for simplicity or configuring host and shared networking with socket_vmnet
for enhanced performance and flexibility, Lima stands as a powerful alternative to traditional VM and container tools. Properly configuring host networking ensures seamless integration between the macOS host and Linux VMs, facilitating efficient development workflows and optimized network performance. By following the steps and guidelines outlined in this guide, users can effectively manage and troubleshoot Lima's networking capabilities to suit their specific needs.