Ubuntu 系统环境

Ubuntu 14.04.2 LTS  x86_64 GNU/Linux

 

0. 查看本机网卡和DNS

# ifconfig

eth1      Link encap:Ethernet  HWaddr f8:a9:63:55:83:8b  
          inet addr:
10.10.111.15  Bcast:10.10.111.255  Mask:255.255.255.0
          inet6 addr: fe80::faa9:63ff:fe55:838b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:25851 errors:0 dropped:152 overruns:0 frame:0
          TX packets:25318 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:9086092 (9.0 MB)  TX bytes:4059896 (4.0 MB)

lo        Link encap:Local Loopback  
          inet addr:
127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:155 errors:0 dropped:0 overruns:0 frame:0
          TX packets:155 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:13617 (13.6 KB)  TX bytes:13617 (13.6 KB)

 

# nm-tool

NetworkManager Tool

State: connected (global)

- Device: eth1  [Wired connection 1] -------------------------------------------
  Type:              Wired
  Driver:            r8169
  State:             connected
  Default:           yes
  HW Address:        F8:A9:63:55:83:8B

  Capabilities:
    Carrier Detect:  yes
    Speed:           100 Mb/s

  Wired Properties
    Carrier:         on

  IPv4 Settings:
    Address:        
10.10.111.15
    Prefix:          24 (255.255.255.0)
    Gateway:        
10.10.111.1

    DNS:             10.10.0.21
    DNS:             10.10.0.20
    DNS:             127.0.0.1

- Device: wlan1 ----------------------------------------------------------------
  Type:              802.11 WiFi
  Driver:            iwlwifi
  State:             unavailable
  Default:           no
  HW Address:        00:C2:C6:86:1E:92

  Capabilities:

  Wireless Properties
    WEP Encryption:  yes
    WPA Encryption:  yes
    WPA2 Encryption: yes

  Wireless Access Points 

 

1. 找到网络文件并修改

sudo vim /etc/network/interfaces

修改如下部分:

# interfaces(5) file used by ifup(8) and ifdown(8)
#auto lo
#iface lo inet loopback

auto eth1
iface eth1 inet static
address
10.10.111.177
gateway 10.10.110.1
netmask 255.255.255.0
network 10.10.111.0
broadcast
10.10.111.255

 

2. 修改dns解析

因为以前是dhcp解析,所以会自动分配dns服务器地址

查看自动分配dns服务器地址

# cat /etc/resolv.conf 
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver
10.10.0.20
nameserver 10.10.0.21
nameserver 127.0.0.1

而一旦设置为静态ip后就没有自动获取到的dns服务器了

要自己设置一个

sudo vim /etc/resolv.conf

写上一个公网的DNS

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
#nameserver 127.0.0.1

nameserver 10.10.0.20
nameserver 10.10.0.21
nameserver 8.8.8.8

注意:8.8.8.8是谷歌的DNS服务器,但是解析速度慢,还是找到一个国内的dns来用

重启电脑,使设置生效(重启电脑后,网络图标可能显示为不可用,实际是可联网)

 

3. 重启网卡(推荐重启电脑,重启网卡有时不管用

sudo  /etc/init.d/networking  restart

# sudo /etc/init.d/networking restart
stop: Job failed while stopping
start: Job is already running: networking

这里有点有问题,网卡配置没能生效,查看日志:

tail -f /var/log/upstart/networking.log      //查看错误日志

Stopping or restarting the networking job is not supported.
Use ifdown & ifup to reconfigure desired interface.

从以上日志内容看,传统的service重启和停止网络已经不再支持,需要使用ifdown & ifup来操作。

ifdown eth1                   //  ifdown: interface eth1 not configured
ifup eth1                       //  RTNETLINK answers: File exists   Failed to bring up eth1.

但是这样操作了,网卡仍然不起作用,最后查看,用如下命令:

sudo ifconfig eth1 down
sudo ifconfig eth1 up

即使这样,重启ubuntu后,发现又不能上网了,问题出在 /etc/resolv.conf

重启后,此文件配置的dns又被自动修改为默认值,所以需要永久性修改DNS。

修改如下文件并添加:

sudo vim /etc/resolvconf/resolv.conf.d/base
nameserver 10.10.0.20
nameserver 10.10.0.21
nameserver 8.8.8.8

保存后,重启电脑(注意:测试发现重启网络不生效)

查看本机网络IP地址

$ ifconfig
eth1      Link encap:Ethernet  HWaddr f8:a9:63:57:2a:b9  
          inet addr:
10.10.111.177  Bcast:10.10.111.255  Mask:255.255.255.0
          inet6 addr: fe80::faa9:63ff:fe57:2ab9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:971 errors:0 dropped:451 overruns:0 frame:0
          TX packets:141 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:83270 (83.2 KB)  TX bytes:16417 (16.4 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:137 errors:0 dropped:0 overruns:0 frame:0
          TX packets:137 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:12727 (12.7 KB)  TX bytes:12727 (12.7 KB)

 

 

请访问米扑代理的检测页面:

http://proxy.mimvp.com/check.php

 

 

补充知识:Ubuntu下修改为永久DNS的方法

上面介绍了安装好Ubuntu之后,设置了静态IP地址,但是有时再重启后就无法解析域名,因此想重新设置一下DNS

打开/etc/resolv.conf

cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN

内容是一段警告:说这个文件是resolvconf程序动态创建的,不要直接手动编辑,修改将被覆盖。

果不其然,修改后重启就失效了,搜索了Ubuntu下设置DNS的相关资料,总结出两个办法:

方法1

通过/etc/network/interfaces

vim /etc/network/interfaces

在它的最后增加一句:

dns-nameservers 8.8.8.8

8.8.8.8是Google提供的DNS服务,你也可以改成电信运营商的DNS。

sudo reboot

重启服务器后,DNS就生效了,这时候再看 /etc/resolv.conf,最下面就多了一行:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 8.8.8.8

 

方法2

修改配置文件

vim /etc/resolvconf/resolv.conf.d/base(这个文件默认是空的)

在里面插入:

nameserver 8.8.8.8
nameserver 8.8.4.4

如果有多个DNS就一行一个

修改好保存,然后执行

resolvconf -u

再看/etc/resolv.conf,最下面就多了2行:

cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 8.8.8.8
nameserver 8.8.4.4

可以看到我们的设置已经加上了,然后再ping一个域名,当时就可以解析了,无需重启。

 

说明:

CentOS下修改DNS很容易,直接修改/etc/resolv.conf,内容是:

nameserver 8.8.8.8
nameserver 8.8.4.4

保存就生效了,重启也没问题。