Linux 系统环境

Ubuntu 14.04.2 LTS  x86_64 GNU/Linux

CentOS Linux release 7.5.1804 (Core)

Ubuntu 与 CentOS 同属于Linux 系统,但是二者配置网卡的方式不同,配置文件也不尽相同。

 

一、Ubuntu 系统

0. Ubuntu 查看本机网卡和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来用,参见米扑博客:公共 DNS 服务器 IP 地址

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

 

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)

 

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

https://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,参见米扑博客:公共 DNS 服务器 IP 地址

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 系统

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

vim /etc/resolv.conf

# vim /etc/resolv.conf                         
# Generated by NetworkManager
nameserver 114.114.114.114
nameserver 8.8.8.8

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

 

CentOS重启后resolv.conf被重置的解决方案

修改 /etc/resolv.conf 文件后,重启服务器后,有时 DNS 会被重置还原,如何解决呢?

1)修改 /etc/sysconfig/network-scripts/ifcfg-eth0 文件

# vim /etc/sysconfig/network-scripts/ifcfg-eth0
UUID=
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
TYPE=Ethernet
DEFROUTE=yes
IPV6INIT=yes
IPV4_FAILURE_FATAL=no
PEERDNS=yes
NAME=eth0

IPADDR=192.168.137.26
NETMASK=255.255.255.0
GATEWAY=192.168.137.1
DNS1=114.114.114.114
DNS2=8.8.8.8

 

2)重启网卡

命令重启已经被淘汰,不要使用命令:service network restart (已舍弃)

命令推荐使用

# ifdown ppp0
# ifup ppp0

 

3)查看 /etc/resolv.conf 文件自动生成的DNS

# cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 114.114.114.114
nameserver 8.8.8.8

 

还有一种简单方法,在网卡 ifcfg-eth0 文件里,修改参数 PEERDNS=no,然后在 /etc/resolv.conf 文件里直接配置 DNS,这样重启服务器后,修改后的 /etc/resolv.conf DNS 也是永久的,不会被重置。

 

查看 /etc/sysconfig/network 文件

# vim /etc/sysconfig/network
# Created by anaconda

 

修改静态DNS,需要在 ifcfg-eth0 文件(ifcfg-eth1文件一般为空)里添加两行,例如:

DNS1=114.114.114.114 
DNS2=8.8.8.8

查看 /etc/sysconfig/network-scripts/ifcfg-eth0 文件

# vim /etc/sysconfig/network-scripts/ifcfg-eth0
UUID=
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
TYPE=Ethernet
DEFROUTE=yes
IPV6INIT=yes
IPV4_FAILURE_FATAL=no
PEERDNS=yes
NAME=eth0

IPADDR=192.168.137.26
NETMASK=255.255.255.0
GATEWAY=192.168.137.1
DNS1=114.114.114.114
DNS2=8.8.8.8

 

查看 /etc/sysconfig/network-scripts/ifcfg-eth1 文件

# vim /etc/sysconfig/network-scripts/ifcfg-eth1

 

查看 ifconfig 命令

# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.137.26  netmask 255.255.255.0  broadcast 192.168.137.255
        inet6 fe80::215:5dff:fe09:64b3  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:09:64:b3  txqueuelen 1000  (Ethernet)
        RX packets 79214  bytes 8765241 (8.3 MiB)
        RX errors 0  dropped 835  overruns 0  frame 0
        TX packets 613  bytes 77201 (75.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::215:5dff:fe09:64b5  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:09:64:b5  txqueuelen 1000  (Ethernet)
        RX packets 108802  bytes 11866450 (11.3 MiB)
        RX errors 0  dropped 12  overruns 0  frame 0
        TX packets 313  bytes 10790 (10.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 720  bytes 65674 (64.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 720  bytes 65674 (64.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ppp0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST>  mtu 1492
        inet 221.202.153.166  netmask 255.255.255.255  destination 221.202.153.1
        ppp  txqueuelen 3  (Point-to-Point Protocol)
        RX packets 27  bytes 1283 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 37 (37.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

 

补充:

当然,默认情况下 /etc/resolv.conf 都是由系统自动生成的,例如

1、CentOS 服务器拨号(pppoe)

# cat /etc/resolv.conf    
# Generated by NetworkManager
nameserver 114.114.114.114
nameserver 8.8.8.8
nameserver 192.168.2.1

 

2、CentOS 服务器(阿里云)

# cat /etc/resolv.conf 
options timeout:2 attempts:3 rotate single-request-reopen
; generated by /usr/sbin/dhclient-script
nameserver 100.100.2.138
nameserver 100.100.2.136

 

3、Ubuntu 服务器

# 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 127.0.1.1

 

4、MacOS 服务器

$ cat /etc/resolv.conf 
#
# macOS Notice
#
# This file is not consulted for DNS hostname resolution, address
# resolution, or the DNS query routing mechanism used by most
# processes on this system.
#
# To view the DNS configuration used by this system, use:
#   scutil --dns
#
# SEE ALSO
#   dns-sd(1), scutil(8)
#
# This file is automatically generated.
#
nameserver 192.168.31.1

 

 

参考推荐

公共 DNS 服务器 IP 地址

公用DNS服务器代理汇总

Linux/Windows 设置本地DNS域名解析Hosts

windows/linux修改hosts文件

Ubuntu配置网络命令