How to configure a static IP address in a Linux virtual machine
Configuring a static IP address in a Linux virtual machine is a common requirement, especially for servers or development environments that require a stable network connection. The following are the detailed steps to configure a static IP address in a Linux virtual machine.
1. Select the network connection method
Before configuring a static IP, you need to determine the network connection mode of the virtual machine. Common connection modes include bridge mode, NAT mode, and host mode. Here, we choose NAT mode because it can ensure network communication between the virtual machine and the host machine, and can connect to the external network through the host machine's network.
2. Configure the virtual machine network adapter
- Open the virtual machine settings, find the Network Adapter option, and select NAT mode.
- Confirm your changes and save your settings.
3. View and configure subnet, gateway and subnet mask
- In the main interface of the virtual machine software (such as VMware), click the "Edit" menu and select "Virtual Network Editor".
- In the pop-up interface, select NAT mode, and then you can view and modify the subnet IP, subnet mask and gateway IP. For example, you can set the subnet IP to 192.168.88.0, the subnet mask to 255.255.255.0, and the gateway IP to 192.168.88.2 (the values here are only examples and may need to be adjusted according to specific circumstances during actual configuration).
- Confirm your changes and save your settings.
4. Configure network files in Linux system
- Open the Linux virtual machine and log in to the system.
- Open a terminal and enter the command to switch to the root user (or use the sudo command to obtain administrator privileges).
- Enter the command to enter the path where the network configuration file is located, usually
/etc/sysconfig/network-scripts/
. In this path, find a network configuration file similar toifcfg-ens33
(the file name may vary depending on the system). - Use a text editor (such as vim) to open the file and add or modify the following configuration information:
- BOOTPROTO=static (Set the BOOTPROTO value to static, indicating that a static IP address is used)
- IPADDR=192.168.88.XXX (set a static IP address, XXX is a custom number, make sure it is in the same network segment as the subnet IP and does not conflict with other devices)
- NETMASK=255.255.255.0 (set the subnet mask, which is consistent with the configuration in the virtual machine software)
- GATEWAY=192.168.88.2 (set the gateway IP address, which is consistent with the one configured in the virtual machine software earlier)
- DNS1=8.8.8.8 (optional, set the preferred DNS server address)
- DNS2=8.8.4.4 (optional, set the alternate DNS server address)
- Save and close the file.
- Restart the network service to make the configuration take effect. In CentOS system, you can use the command
systemctl restart network
to restart the network service.

5. Verify that the configuration is successful
- Open a terminal in the Linux virtual machine and enter the command
ifconfig
(orip addr
) to view the configuration information of the network interface. Confirm whether the IP address, subnet mask, and gateway are configured correctly. - Try to use the ping command to test whether the network connection is normal. For example, you can enter the command
ping www.xxx.com
to test whether the external network can be accessed normally.
Through the above steps, you can successfully configure a static IP address in the Linux virtual machine. Please note that the specific configuration method and parameters may vary depending on the virtual machine software and Linux distribution, so please refer to the relevant documents or tutorials for configuration during actual operation.
This article comes from online submissions and does not represent the analysis of kookeey. If you have any questions, please contact us