Use Vmware Fusion virtual machine to configure static ip of Centos on mac

Time:2022-8-5

1. Background

This article briefly records, inmac armuse in architectureVmware Fusionunder the virtual machineCentos7How to configurestatic ipaddress. If usingdhcpDynamic allocation of static IP addresses, then the IP addresses may change, so you need to use static IP addresses.

Second, the realization of the function

  1. Internally the virtual machine uses a static ip address.
  2. The virtual machine can access the external network.

3. Implementation steps

1. Set the network of the virtual machine to nat

2. Modify the network adapter network of the virtual machine

3. Configure static ip

To configure static ip, we know that we generally need to configure the following three parameters.
ip address:IPADDR, Subnet mask:NETMASKand gateway:GATEWAY

3.1 Get the subnet mask

existmacExecute the following command on the computer

➜  ~ cat /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf | grep netmask
netmask = 255.255.255.0

You can see that the obtained subnet mask is255.255.255.0

3.2 Get the gateway

existmacExecute the following command on the computer

➜  ~ cat /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf | grep "ip ="  -B 1
# NAT gateway address
ip = 192.168.121.2

You can see that the obtained gateway is192.168.121.2

3.3 Configure Centos static ip

existInside the virtual machineExecute the following command

vim /etc/sysconfig/network-scripts/ifcfg-ens160

ifcfg-ens160How did this value come about?
Can be executed in a virtual machineip addrcommand to get.

The specific configuration is as follows:

BOOTPROTO=static # configure static ip
IPADDR=192.168.121.136 # Configured ip address
NETMASK=255.255.255.0 # subnet mask
GATEWAY=192.168.121.2 # Gateway
DNS1=8.8.8.8 # Configure dns address

3.4 Restart the network

systemctl restart network

3.5 Check whether the ip is valid

[[email protected] ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether ee:fc:d8:1c:7d:5d brd ff:ff:ff:ff:ff:ff
    inet 192.168.121.136/24 brd 192.168.121.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::ecfc:d8ff:fe1c:7d5d/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
[[email protected] ~]#

inet 192.168.121.136/24 brd 192.168.121.255 scope global noprefixroute ens160You can see that our ip address configuration takes effect.

3.6 Determine whether you can access the external network

[[email protected] ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=128 time=34.4 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=128 time=30.7 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=4 ttl=128 time=30.2 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=5 ttl=128 time=39.7 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=6 ttl=128 time=36.6 ms
^C
--- www.a.shifen.com ping statistics ---
6 packets transmitted, 5 received, 16% packet loss, time 5080ms
rtt min/avg/max/mdev = 30.244/34.363/39.793/3.600 ms
[[email protected] ~]#

You can see that you can access the external network normally.

4. Slow access to ssh

After configuring a static ip address, it may be possible to passsshConnecting to a virtual machine is slow,check informationIt is found that the solution can be solved by the following solutions.

Willvirtual machineof/etc/ssh/sshd_configmedium toUseDNSChange this value tonoThat's it.