---
title: "配置ubuntu系统IP"
author: "wadefall"
date: "2010-12-29T14:58:49Z"
tags: []
categories: ["操作系统"]
slug: "ubuntu-ip"
draft: false
lastmod: "2010-12-29T14:59:36Z"
id: 854868
url: "https://wadefall.iteye.com/blog/854868"
---

在虚拟机上安装了Ubuntu系统之后，要想上网或者与主机进行通信，首先需要配置系统的IP。可按照以下步骤来进行。
1.输入以下命令：ifconfig -a

![](http://dl.iteye.com/upload/attachment/378273/76bafaf9-7fb3-33ce-8a05-5963ef0cbf08.png)

可以看到我的系统中Ubuntu将网卡识别为eth2，因此我要对eth2设置IP、子网掩码、网关等。

2.在interfaces中配置设置IP、子网掩码、网关的值可以保存下来，在系统启动的时候自动加载。步骤如下：

1）输入命令：sudo gedit /etc/network/interfaces
或者直接找到该文件，写入下列内容：
auto lo
iface lo inet dhcp
auto <span style="color:red">eth2</span>

iface <span style="color:red">eth2</span> inet static 
address <span style="color:red">10.63.0.56</span>
netmask <span style="color:red">255.255.255.0</span>
gateway <span style="color:red">10.63.0.254</span>

在自己的主机中查看Ethernet的IP、子网掩码、网关值来设置上述值，IP要换一个可以用的，我的主机Ethernet IP是：10.63.0.58，我ping了一下10.63.0.56可以ping通，因此虚拟机中可以使用这个IP。红色部分应该换成不同主机对应的内容。

2)重新启动服务，可让设置的IP生效：
输入命令：sudo /etc/init.d/networking restart

3)设置DNS信息保存在/etc/resolv.conf中，一旦更改，立即生效。
输入命令：sudo gedit /etc/resolv.conf
在打开的文件中输入以下信息：
nameserver <span style="color:red">159.226.39.1</span> 

或者可以下面的命令启动网卡：
1) 要禁用网卡，输入：sudo ifdown <span style="color:red">eth2</span>
2)要启动网卡，输入：sudo ifup <span style="color:red">eth2</span>

这样IP地址就配置好了，这是永久保存的地址。如果要临时改变IP地址，可用下面的命令，但是系统重新启动后，还是会恢复使用interfaces中的配置。
1)设置IP地址和子网掩码：sudo ifconfig <span style="color:red">eth2 10.63.0.56</span> netmask <span style="color:red">255.255.255.0</span>
2)设置网关：sudo route add default gw <span style="color:red">10.63.0.254</span>
3)设置DNS，同上，输入命令：sudo gedit /etc/resolv.conf
在打开的文件中输入以下信息：
nameserver <span style="color:red">159.226.39.1</span>