Many virtual machines are installed using VirtualBox, and DHCP is used by default. However, with more and more virtual machines, this method is very inconvenient, so it is necessary to set the IP of the machine to static IP
Explanation of several items that need to be added:
IPADDR IP address
NETMASK subnet mask
NETWORK gateway address
Generally, if we want to change the host address to a static address or change the host name, several files that need to be modified include:
/etc/sysconfig/network set host name and network configuration
/etc/sysconfig/network-scripts/ifcfg-eth0 Set up for specific network card
/etc/resolv.conf set DNS
/etc/hosts set the specified [domain name resolution] (https://cloud.tencent.com/product/cns?from=10680) address
Generally, we only need to modify the configuration file of the network card. For example, my configuration file is as follows:
DEVICE=eth0
BOOTPROTO=static
TYPE=Ethernet
NAME="System etho0"
BROADCAST=192.168.56.255
HWADDR=08:00:27:24:F8:9B
IPADDR=192.168.56.101
IPV6INIT=yes
IPV6_AUTOCONF=yes
NETMASK=255.255.255.0
NETWORK=192.168.56.1
ONBOOT=yes
After the setting is complete, restart the network card: service network restart
We also have another way to set a static IP, that is through the ifconfig command. Usually, we use it to view some information of the current network card, and at the same time, it can also be used to set some network card information.
The modified command is as follows: ifconfig eth0 192.168.56.102
However, after this command is executed, only the address of the network card can be modified in the current session. Let's take a look at the configuration file of ifcfg-eth0, and it is still
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
HWADDR=08:00:27:43:73:2f
In other words, after restarting the server, the IP will still be obtained according to the method in the configuration file.
Therefore, if you need to modify the IP to a static IP, the best way is to modify the configuration file.
Reference materials:
1、 Problems encountered when setting parameters with ifconfig
2、 Manually set the IP of CentOS
Recommended Posts