How to configure a Linux CentOS7 virtual machine with a static IP and allow access to the Internet

2023博客之星评选已开启--成为城市领跑者

Preface

When we successfully installed the CentOS image on our virtual machine, but at this time, the virtual machine has not yet configured IP information. For the convenience of subsequent development, we need to set
a static IP.

1. Start VMnet8 on the local computer

On the local computer, right-click on the network -> select "Change Adapter Options" -> Enable VMnet8.

Insert image description here
Insert image description here

2. Configure static IP in Linux

Open the Vmware virtual machine. This article uses Centos7 as an example:

1. NAT mode setting

 在Centos7虚拟机关机状态下(开机状态也行),点击Centos这台虚拟机的Vmware编辑按钮,选择【虚拟网络编辑器】那一项;
 进行子网IP 和 NAT设置:
 比如:

Subnet IP: 192.168.86.0

Subnet mask: 255.255.255.0

Gateway IP: 192.168.86.2

Insert image description here

Insert image description here
Insert image description here

2. Start the virtual machine and log in as the root user

Open the Vmware virtual machine, start Centos7, and log in as root.

If you are not logged in as the root user, you need to enter the command to switch users:

su root

3. Execute the command to set a static IP

① Modify the network card configuration file

vi /etc/sysconfig/network-scripts/ifcfg-ens33

Insert image description here

② Modify the file content
and execute: vi /etc/sysconfig/network-scripts/ifcfg-ens33 Delete the original configuration and modify it to the following configuration.

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
IPADDR=192.168.86.129
NETMASK=255.255.255.0
GATEWAY=192.168.86.2
DNS1=8.8.8.8
DNS2=4.2.2.2
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=2c2371f1-ef29-4514-a568-c4904bd11c82
DEVICE=ens33
ONBOOT=true

After entering the editor, copy the above content or modify it yourself, then 按esc退出编辑状态,再输入:wq! save and edit ( 注意有冒号)

注意:
BOOTPROTO is set to static static
IPADDR设置一个新的ip地址
NETMASK is set to subnet mask
GATEWAY is set to gateway
ONBOOT is set to true whether to activate the network card when the system starts

DNS1=8.8.8.8
DNS2=4.2.2.2
DNS1=8.8.8.8 and DNS2=4.2.2.2 means you are configuring a DNS server for a Linux system. DNS (Domain Name System) is a distributed database used on the Internet to convert domain names into IP addresses.
DNS1=8.8.8.8 means that you are using Google's public DNS server, which is one of the largest DNS servers in the world, with fast and stable speed.
DNS2=4.2.2.2 means that you are using the public DNS server of Level 3 Communications Company, which is also one of the largest DNS servers in the world, with fast and stable speed.
You can replace these IP addresses with those of other public DNS servers, such as OpenDNS (208.67.222.222 and 208.67.220.220) or Cloudflare (1.1.1.1 and 1.0.0.1). These public DNS servers usually have better performance and security.
It should be noted that in some cases, you may need to add a blank line between the IP addresses of the DNS servers to separate them.

4. Restart the network card

① Restart the network card (normal)

systemctl restart network

② Restart the network card (abnormal)

Restart the network service systemctl restart network and report an error

Prompt me to use the command “systemctl status network” 或者 “journalctl -xe” to see the cause of the problem

Enter according to the prompts journalctl -xe and find an error: failed to start LSB:Bring up/down

Insert image description here

③ Solution: Disable NetworkManager

# 停止 NetworkManager
systemctl stop NetworkManager

# 禁止 NetworkManager 开机自启 
systemctl disable NetworkManager

然后重启网络服务 systemctl restart network ,虚拟机就可以联网了

Insert image description here

5. View ip

ifconfig

Insert image description here
As shown in the picture above, the static IP we configured works, currently it is 192.168.23.129

6. Ping the virtual machine from the local computer cmd window

It can be seen that the host machine and the virtual machine are interoperable.

Insert image description here

7. Virtual machine pings local computer

It can be seen that the virtual machine and the local computer are interoperable

Insert image description here

8. Configure browser access inside the virtual machine

To access the network in the virtual machine, add a NAT network card
1) [Virtual Machine] – [Settings] – [Add]

Insert image description here

Set to NAT mode, as shown below.
Insert image description here
At this time, we access through the browser of the virtual machine

https://www.baidu.com/

Insert image description here
It can be seen that we can access the external network through NAT mode.

Guess you like

Origin blog.csdn.net/y2020520/article/details/131177497