如何解决Linux虚拟机设置静态IP上网的问题?

设定 IP

在虚拟机终端输入:$sudo vi /etc/network/interfaces

  1. 可看见有如下内容:
auto lo
iface lo inet loopback
  1. 添加以下内容
auto eth0
iface eth0 inet static
address 192.168.0.221  
netmask 255.255.255.0
gateway 192.168.0.1
  1. 保存,并关闭 gedit。
  2. 这样,IP 并没有立即生效。需要执行$sudo /etc/init.d/networking restart

这时候就能 ping 到局域网中的电脑了。

但是上不了 Internet,是因为没有设置 DNS 的原因 。
DNS 信息保存在/etc/resolv.conf中,一旦更改,立即生效。
$sudo vi /etc/resolv.conf

nameserver 192.168.1.1
nameserver 8.8.8.8

保存并关闭,现在就可以上网了。

需要注意的问题:
Ubuntu 12.04 每次重启后,/etc/resolv.conf 里面设置的 nameserver 就被清空

如果想自己定义 nameserver一直有效,那么有以下 2 个办法:

  • 在网卡的配置文件里面加:
iface eth0 inet static
address 192.168.3.3
netmask 255.255.255.0
gateway 192.168.3.1
s dns-nameservers  5 192.168.3.45  192.168.8.10
dns-search foo.org bar.com
  • 修改 resolvconf 服务的配置文件 :
/etc/resolvconf/resolv.conf.d/base
清空原内容并写入以下内容
r nameserver  8.8.8.8

这样,在机器重启或 resolvconf服务重启 都可以保证配置会写到/etc/resolv.conf 里面

其实/etc/resolv.conf 里面一直都有提示,只是没注意看:
"DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN"

网卡设置相关命令:

(1)查看网卡信息: ifconfig
(2)设定一个网卡 IP:ifconfig eth0 192.168.1.10 netmask 255.255.255.0
(3)重启网卡使设定生效:sudo /etc/init.d/networking restart
(4)更改 MAC 地址:ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
(5)查看路由相关信息:route -n

DNS 服务器设置(Ubuntu)

  1. 两个免费好用的 DNS 服务器
googleDNS: 8.8.8.8; 8.8.8.4
openDNS : 208.67.220.220; 208.67.222.222
  1. 查看本机 DNS 服务器
查看 DNS server:nslookup www.baidu.com
发布了23 篇原创文章 · 获赞 37 · 访问量 4535

猜你喜欢

转载自blog.csdn.net/ywsydwsbn/article/details/105173667