资源描述
Ubuntu 10~11中配置ip地址(配有详细图)
一、直接修改Ubuntu IP系统配置文件
Ubuntu IP的网络配置文件是:/etc/network/interfaces打开后里面可设置DHCP或手动设置静态ip。前面auto eth0,让网卡开机自动挂载。
1.以DHCP方式配置网卡
(1)打开要编辑的文件interfaces 它的的目录是/etc/network/interfaces
sudo vi /etc/network/interfaces
或者:sudo gedit /etc/network/interfaces 此时便可以在文本编辑器中编写文件中的内容,要方便多
(2)将文件里的内容改为:
auto eth0
iface eth0 inet dhcp
(3)使用下面的命令使网络设置生效
sudo /etc/init.d/networking restart
(4)出了上面那种直接修改配置文件,如果要动态获取ip的话,还可以直接使用命令:
sudo dhclient eth0
使用了此句后,网卡eth0的ip地址被设定为10.0.2.15,此时虚拟机可以ping通主机,且可以与主机共享上网。(但是此时主机ping不通虚拟机)
2. 为网卡配置静态Ubuntu IP地址
(1)打开要编辑的文件interfaces 它的的目录是/etc/network/interfaces
sudo vi /etc/network/interfaces
或者:sudo gedit /etc/networking/interfaces 此时便可以在文本编辑器中编写文件中的内容,要方便多
(2)将文件里的内容改为:
auto eth1
iface eth1 inet static
address 10.10.103.77
netmask 255.255.255.0
gateway 10.10.103.20
(3)配置DNS
sudo gedit /etc/resolv.conf //使用此语句打开配置DNS的文件,修改即可
nameserver 202.119.230.8
(4)使用下面的命令使网络设置生效
sudo /etc/init.d/networking restart
或者执行:sudo ifup eth1
执行此命令后,虚拟机的网卡eth1的ip地址被设定为10.10.103.77,此ip地址与主机ip地址在同一个网段中,此时主机便可以ping通虚拟机了。(执行的前提是已经给此虚拟机设置了NAT和桥接方式)
二、实现主机和虚拟机之间相互通信
预备知识:
host-only模式下,主机相当于一台双网卡的电脑,物理网卡是外网,vmnet1是内网,此时虚拟机和主机通信的前提就是vmnet1的ip和虚拟机的ip在同一网段。 桥接模式下,主机和虚机相当于是两台接在同一交换机上的电脑,此时,虚拟机的ip地址应该和主机的物理网卡ip在同一网段,此时就能通信,当然包括也包括ping。如果不通,可能跟防火墙有关系。
采用的方法是:设置两个虚拟网卡,其中一个用NAT连接,以连接外网;一个用桥接,用来与主机互相通信。
步骤:
1、在启动虚拟机之前,在“Network”中默认设置第一块虚拟网卡(即用NAT方式)
,然后切换到Adapter2启用第二块虚拟网卡,并更改为桥接连接方式:
2、启动虚拟机,编辑eth1,在里面设置以太网接口eth1的IP地址、子网掩码和网关,这一步最为重要。eth1这块网卡是桥接用的,即是与你主机互相通信用的,所以这里设置的IP要与主机的IP地址同个网段,网关和子网掩码就和主机的设置一致,然后设置IP为静态。保存退出后,执行 sudo ifup eth1 后便可以和主机互ping了。
3、使用 sudo dhclient eth0 命令来动态分配eth0网卡的地址,可以看到设置后此IP为专有IP地址,用来NAT连接的,通过NAT转换之后就能上外网了。
4、用ifconfig查看网络接口信息,可以看到三个网卡,eth0,eth1,lo。
三、使用命令设置Ubuntu IP地址(未实现!!)
(1)修改配置文件blacklist.conf禁用IPV6:sudo vi /etc/modprobe.d/blacklist.conf
(2)在文档最后添加 blacklist ipv6,然后查看修改结果:cat /etc/modprobe.d/blacklist.conf
(3)设置Ubuntu IP(设置网卡eth0的IP地址和子网掩码)sudo ifconfig eth0 192.168.2.1 netmask 255.255.255.0
(4)Ubuntu IP设置网关sudo route add default gw 192.168.2.254
(5)Ubuntu IP设置DNS修改/etc/resolv.conf,在其中加入nameserver DNS的地址1 和 nameserver DNS的地址2 完成。
(6)重启网络服务(若不行,请重启ubuntu:sudo reboot):sudo /etc/init.d/networking restart
(7)查看当前Ubuntu IP:ifconfig
四、ifconfig用法(未看)
无论是Linux 自动安装还是我们手工安装,Linux 都会向你询问有关网络的问题并配置相关的软件。这个用于配置网卡的基本命令就是ifconfig。
在执行ifconfig 命令后,系统将在内核表中设置必要的参数,这样Linux 就知道如何与网络上的网卡通
信。ifconfig 命令有以下两种格式:
[php]view plaincopyprint?
1 ※ifconfig <span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 153, 0); font-family: inherit; font-style: normal; font-weight: normal;">[</span><strong>interface</strong><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 153, 0); font-family: inherit; font-style: normal; font-weight: normal;">]</span>
2 ※ifconfig <strong>interface</strong> <span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 153, 0); font-family: inherit; font-style: normal; font-weight: normal;">[</span>aftype<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 153, 0); font-family: inherit; font-style: normal; font-weight: normal;">]</span> option <span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">|</span> address …
ifconfig 的第一种格式(或使用不带任何参数的ifconfig 命令)可以用来查看当前系统的网络配置情况。
在刚刚安装完系统之后,实际上是在没有网卡或者网络连接的情况下使用Linux,但通过ifconfig 可以使用回绕方式工作,使计算机认为自己工作在网络上。
现在我们运行一下ifconfig 命令,不带参数的ifconfig 命令可以显示当前启动的网络接口,其输出结果
为:
[php]view plaincopyprint?
3 <span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 153, 0); font-family: inherit; font-style: normal; font-weight: normal;">[</span>root<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">@</span>machine1 <span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">/</span>sbin<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 153, 0); font-family: inherit; font-style: normal; font-weight: normal;">]</span><em>#ifconfig
4 </em>eth0 <span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(153, 0, 0); font-family: inherit; font-style: normal; font-weight: normal;">Link</span> encap<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span>Ethernet Hwaddr <span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">52</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">54</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span>AB<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span>DD<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span>6F<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">61</span>
5 inet addr<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span>210<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">.</span>34<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">.</span>6<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">.</span>89 Bcast<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span>210<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">.</span>34<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">.</span>6<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">.</span>127 Mask<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span>255<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">.</span>255<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">.</span>255<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">.</span>128
6 UP BROADCAST RUNNING MULTICAST MTU<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">1500</span> Metric<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">1</span>
7 RX packets<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">46299</span> errors<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">0</span> dropped<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">0</span> overruns<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">0</span> frame<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">189</span>
8 TX packets<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">3057</span> errors<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">0</span> dropped<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">0</span> overruns<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">0</span> carrier<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">0</span>
9 collisions<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">0</span> txqueuelen<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">100</span>
10 Interrupt<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(204, 102, 204); font-family: inherit; font-style: normal; font-weight: normal;">5</span> Base address<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span><span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(32, 128, 128); font-family: inherit; font-style: normal; font-weight: normal;">0xece0</span>
11 lo <span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(153, 0, 0); font-family: inherit; font-style: normal; font-weight: normal;">Link</span> encap<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span>Local Loopback
12 inet addr<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">:</span>127<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 153, 51); font-family: inherit; font-style: normal; font-weight: normal;">.</span>0<span style="border-width: 0px; margin: 0px; padding: 0px; text-align: left; color: rgb(51, 1
展开阅读全文