Configuring a static IP address in Lima using the socket_vmnet network mode provides a stable and consistent network environment for your virtual machines (VMs). This guide offers a step-by-step approach to achieve this configuration, ensuring high performance and reliable connectivity.
Before beginning the configuration, ensure the following prerequisites are met:
v0.12.0 or higher.bash
xcode-select --install
The socket_vmnet tool is essential for enabling advanced network configurations in Lima. You can install it using Homebrew or by building from the source.
Homebrew simplifies the installation process:
brew install socket_vmnet
This command installs the socket_vmnet binary and its dependencies, making it readily available on your system.
If you prefer building from the source for more control or the latest features, follow these steps:
git clone https://github.com/lima-vm/socket_vmnet.git
cd socket_vmnet
make
sudo make PREFIX=/opt/socket_vmnet install.bin
Important: Ensure that socket_vmnet is installed in a secure directory such as /opt/socket_vmnet to prevent security vulnerabilities.
For more details, refer to the socket_vmnet GitHub Repository.
To allow Lima to run socket_vmnet with the necessary privileges without prompting for a password, you need to configure the sudoers file.
limactl sudoers | sudo tee /etc/sudoers.d/lima
sudo install -o root /etc/sudoers.d/lima /etc/sudoers.d/lima
This configuration ensures that Lima can start and stop the socket_vmnet daemon seamlessly.
To make the socket_vmnet binary accessible from any directory, add it to your system's PATH:
echo 'export PATH="/opt/socket_vmnet/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
This ensures that the socket_vmnet command can be executed without specifying its full path.
Lima uses a networks.yaml file to define network configurations. This file specifies how virtual machines connect to networks, including static IP assignments.
The configuration file is typically located at ~/.lima/_config/networks.yaml. If it doesn't exist, create it:
mkdir -p ~/.lima/_config
vim ~/.lima/_config/networks.yaml
Add the following configuration to define the socket_vmnet network:
networks:
- lima: shared
mode: socket_vmnet
gateway: 192.168.105.1
netmask: 255.255.255.0
dhcp:
start: 192.168.105.2
end: 192.168.105.100
- lima: bridged
mode: bridged
interface: en0
- lima: host
mode: host
gateway: 192.168.106.1
dhcpEnd: 192.168.106.254
netmask: 255.255.255.0
shared mode utilizes NAT to share the host's network.bridged mode connects the VM directly to the host's physical network interface.host mode creates a host-only network for private communication between the host and VM.To reserve IPs for static assignment, configure the DHCP range to exclude the desired static IPs. For example, setting dhcpEnd to 192.168.105.100 reserves 192.168.105.101 to 192.168.105.254 for static assignments.
networks:
shared:
mode: shared
gateway: 192.168.105.1
dhcpEnd: 192.168.105.100
netmask: 255.255.255.0
Save and exit the editor after making these changes.
With the network configurations in place, start the Lima instance using the defined network mode:
limactl start --name=default --network=lima:shared
This command initiates a Lima instance named default using the shared network configuration defined in networks.yaml.
To assign a static IP address to your VM, follow these steps:
Each VM must have a unique MAC address. You can specify this in the Lima configuration or during VM startup:
limactl start --name=default --network=lima:shared --device virtio-net-pci,netdev=net0,mac=de:ad:be:ef:00:01
Ensure that the MAC address does not conflict with other devices on the network.
Reserve a specific IP address for the VM by creating a bootptab file:
%%
hostname hwtype hwaddr ipaddr bootfile
tmp-vm01 1 de:ad:be:ef:00:01 192.168.105.100
This configuration binds the MAC address de:ad:be:ef:00:01 to the IP address 192.168.105.100.
Reload the DHCP daemon to apply the changes:
sudo /bin/launchctl kickstart -kp system/com.apple.bootpd
Within the VM, set the network interface to use the static IP address by editing the network configuration files. For a Linux VM, modify /etc/sysconfig/network-scripts/ifcfg-eth0:
DEVICE=eth0
BOOTPROTO=static
HWADDR=de:ad:be:ef:00:01
IPV6INIT=yes
NM_CONTROLLED=yes
ONBOOT=yes
TYPE=Ethernet
UUID=7a47775a-8bca-4daf-80b4-965da12756a8
IPADDR=192.168.105.100
NETMASK=255.255.255.0
GATEWAY=192.168.105.1
After saving the configuration, restart the network service:
sudo service network restart
After setting up the static IP, verify the configuration to ensure everything is functioning correctly:
Inside the VM, run the following command to verify the IP address:
ip addr
Ensure that the static IP 192.168.105.100 is correctly assigned.
From within the VM, test connectivity to an external host:
ping baidu.com
If DNS resolution fails, update the DNS settings by editing /etc/resolv.conf:
nameserver 8.8.8.8
nameserver 8.8.4.4
Then, restart the network service again:
sudo service network restart
If you encounter issues during the configuration, refer to the following troubleshooting steps:
bootptab file.socket_vmnet daemon is running:sudo /opt/socket_vmnet/bin/socket_vmnet --vmnet-gateway=192.168.105.1 /var/run/socket_vmnet
socket_vmnet and DHCP services are not blocked:sudo /usr/libexec/ApplicationFirewall/socketfilterfw --remove /usr/libexec/bootpd
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add /usr/libexec/bootpd
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --unblock /usr/libexec/bootpd
networks.yaml is correctly set up.socket_vmnet located typically at /var/log/socket_vmnet for any error messages.networks.yaml.socket_vmnet is running and properly configured.socket_vmnet is installed in a secure directory like /opt/socket_vmnet and not in user-writable locations such as /usr/local/bin.Lima supports various network modes simultaneously, allowing for versatile network setups:
networks:
- lima: shared
mode: shared
gateway: 192.168.105.1
dhcpEnd: 192.168.105.100
netmask: 255.255.255.0
- lima: bridged
mode: bridged
interface: en0
- lima: host
mode: host
gateway: 192.168.106.1
dhcpEnd: 192.168.106.254
netmask: 255.255.255.0
This configuration allows VMs to connect through NAT, bridge directly to the host's network, and have a host-only network for private communication.
To reserve specific IP addresses for certain VMs while allowing DHCP to assign others, adjust the DHCP range accordingly:
networks:
shared:
mode: shared
gateway: 192.168.105.1
dhcpEnd: 192.168.105.200
netmask: 255.255.255.0
This setup permits DHCP to assign IPs up to 192.168.105.200, leaving 192.168.105.201 to 192.168.105.254 for static assignments.
Start your Lima VM with the configured network settings:
limactl start --name=default
Ensure that the VM boots up without network issues and acquires the static IP address as configured.
From the host machine, you can access the VM using the assigned static IP:
ssh user@192.168.105.100
Replace user with the appropriate username for your VM.
For more detailed information and advanced configurations, refer to the following resources:
Configuring a static IP address in Lima using the socket_vmnet network mode enhances the stability and reliability of your virtual machine's network setup. By following this comprehensive guide, you can ensure that your VMs maintain consistent connectivity, facilitating seamless development, testing, and deployment workflows.
For any further assistance, consult the official documentation or seek support from the Lima and socket_vmnet communities through their respective GitHub repositories.