---
title: "vitrualbox虚拟机下centos系统设置固定IP"
author: "雨落千木"
date: "2019-08-12T18:11:19Z"
tags: []
categories: ["操作系统"]
slug: "vitrualbox-centos-ip"
draft: false
lastmod: "2019-08-12T18:11:19Z"
id: 2443485
url: "https://350083890.iteye.com/blog/2443485"
---

在virtualbox中安装多台centos虚拟机，每次重启虚拟机都要查看本机的IP，尤其是在集群的环境下，重启虚拟机或者重启电脑都要重新修改集群配置或者是hosts配置，因此我们需要设置固定IP来节省这种麻烦。下面我们来设置固定IP.  
第一步：  
在virtualbox中，管理（或者工具）——》全局设定——》网络——》中，添加一个NAT网络：

![](https://upload-images.jianshu.io/upload_images/4265589-500a24b4233287a5.png?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F967%2Fformat%2Fwebp)

 

 

第二步：  
在virtulabox中，管理——》主机网络管理——》创建，添加host-only.

 

![](https://upload-images.jianshu.io/upload_images/4265589-8373f0dcd9afd55c.png?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F963%2Fformat%2Fwebp)

 

第三步：  
在安装好的centos虚拟机中点击设置（这里以我的zknode1为例）：设置——》网络——》添加网卡1和网卡2，分别是前面创建的Nat网络和host-only网络。

 

![](https://upload-images.jianshu.io/upload_images/4265589-e90683b973eb0b5e.png?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F877%2Fformat%2Fwebp)

![](https://upload-images.jianshu.io/upload_images/4265589-1050228232eb0025.png?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F877%2Fformat%2Fwebp)

 

第四步：  
完成以上设置后启动Centos虚拟机，进行网络配置。  
编辑ifcfg-enp0s3这个文件：

 

 

```
vim /etc/sysconfig/network-scripts/ifcfg-enp0s3
```

 ![](https://upload-images.jianshu.io/upload_images/4265589-4830b2188b03c91e.png?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F604%2Fformat%2Fwebp)

修改后重启网络：service network restart,  
这时候可以ping一下百度试试可以连接互联网了。  
第五步：  
做完第四步的配置，这时候还是动态IP，我们需要添加第二个网卡来设置静态IP。  
复制ifcfg-enp0s3，改成ifcfg-enp0s8

 

```
cp ifcfg-enp0s3 ifcfg-enp0s8
```

 

 

 

编辑ifcfg-enp0s8文件：

![](https://upload-images.jianshu.io/upload_images/4265589-33227e576ce82a6f.png?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F613%2Fformat%2Fwebp)

 

注意：

1. IPADDR范围为第二步创建的host-only的范围，一般是（192.168.224.101——192.168.224.255）
2. UUID需要执行uuidgen命令来重新生成一个  
   同样，设置完ip后重启网络：

```
service network restart
```

 完成！！！

这时候静态IP也有了

![](https://upload-images.jianshu.io/upload_images/4265589-b12be0a084ab9319.png?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F1000%2Fformat%2Fwebp)

 

在虚拟机外使用xshell连接：

![](https://upload-images.jianshu.io/upload_images/4265589-ed6c4444609539e0.png?imageMogr2%2Fauto-orient%2Fstrip%7CimageView2%2F2%2Fw%2F986%2Fformat%2Fwebp)