虚拟机访问外网

在配置文件中修改 1)配置设置服务状态

[root@room1pc32 桌面]# systemctl status NetworkManager	(图形的网络设备)
[root@room1pc32 桌面]# systemctl status network	(命令行网络设备)
[root@room1pc32 桌面]# systemctl stop NetworkManager
[root@room1pc32 桌面]#systemctl start network

2)修改配置文件

	[root@room1pc32 桌面]# cd /etc/sysconfig/network-scripts/
	[root@room1pc32 network-scripts]# cp ifcfg-enp2s0 ifcfg-br0
	[root@room1pc32 network-scripts]# vim ifcfg-br0
	...
	DEVICE=br0		#设备名
	TYPE=Bridge		#设备类型
	[root@room1pc32 network-scripts]# vim ifcfg-enp2s0
	...
	BRIDGE=br0

3)重启network

[root@room1pc32 network-scripts]# systemctl restart network
[root@room1pc32 /]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
				inet 172.40.51.132  netmask 255.255.255.0  broadcast 172.40.51.255
				inet6 fe80::76d4:35ff:fe5e:68bb  prefixlen 64  scopeid 0x20<link>
				ether 74:d4:35:5e:68:bb  txqueuelen 0  (Ethernet)
				RX packets 32  bytes 3174 (3.0 KiB)
				RX errors 0  dropped 0  overruns 0  frame 0
				TX packets 58  bytes 6350 (6.2 KiB)
				TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
				inet6 fe80::76d4:35ff:fe5e:68bb  prefixlen 64  scopeid 0x20<link>
				ether 74:d4:35:5e:68:bb  txqueuelen 1000  (Ethernet)
				RX packets 197261  bytes 196645342 (187.5 MiB)
				RX errors 0  dropped 0  overruns 0  frame 0
				TX packets 136297  bytes 11644709 (11.1 MiB)
				TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

这个时候在桥连接设备已经搭建好了,在你的KVM虚拟机上会多一块网卡设备 如图:

然后在虚拟机上配置 与真实机 同一网段的IP 就能实现虚拟机访问外网了。 也可以在虚拟机上 使用DHCP自动获取IP

[root@svr7 ~]# nmcli connection modify eth0 ipv4.method auto connection.autoconnect yes
[root@svr7 ~]# nmcli connection up eth0

卸载桥连接 首先要去删除桥连接配置文件,以及在网卡配置文件中添加的参数 然后执行以下步骤

[root@room1pc32 network-scripts]# systemctl restart network ; ifconfig br0 down ; brctl delbr br0 ; systemctl restart network 
[root@room1pc32 network-scripts]# ifconfig | head
enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
				inet 172.40.51.132  netmask 255.255.255.0  broadcast 172.40.51.255
				inet6 fe80::76d4:35ff:fe5e:68bb  prefixlen 64  scopeid 0x20<link>
				ether 74:d4:35:5e:68:bb  txqueuelen 1000  (Ethernet)
				RX packets 197424  bytes 196659751 (187.5 MiB)
				RX errors 0  dropped 0  overruns 0  frame 0
				TX packets 136387  bytes 11654032 (11.1 MiB)
				TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536

临时添加桥连接操作

[root@room1pc32 /]# brctl addbr br0
[root@room1pc32 /]# ifconfig br0 172.40.51.132/24
[root@room1pc32 /]# brctl addif br0 enp2s0
[root@room1pc32 /]# systemctl restart network
[root@room1pc32 /]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
				inet 172.40.51.132  netmask 255.255.255.0  broadcast 172.40.51.255
				inet6 fe80::3413:48ff:fe59:3da5  prefixlen 64  scopeid 0x20<link>
				ether 74:d4:35:5e:68:bb  txqueuelen 0  (Ethernet)
				RX packets 425  bytes 43846 (42.8 KiB)
				RX errors 0  dropped 0  overruns 0  frame 0
				TX packets 137  bytes 12221 (11.9 KiB)
				TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
				inet 172.40.51.132  netmask 255.255.255.0  broadcast 172.40.51.255
				inet6 fe80::76d4:35ff:fe5e:68bb  prefixlen 64  scopeid 0x20<link>
				ether 74:d4:35:5e:68:bb  txqueuelen 1000  (Ethernet)
				RX packets 197871  bytes 196713829 (187.6 MiB)
				RX errors 0  dropped 0  overruns 0  frame 0
				TX packets 136583  bytes 11672572 (11.1 MiB)
				TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0