Skip to content

2015 03 29 ubuntu server network config

hanyong edited this page Mar 29, 2015 · 1 revision

ubuntu 服务器网络配置

本文基于 ubuntu 12.04, 参考文档: https://help.ubuntu.com/12.04/serverguide/network-configuration.html .

网卡配置文件 /etc/network/interfaces, 我在一台 vps 服务器上的配置示例如下:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
	address 10.163.98.48
	netmask 255.255.240.0
#	network 10.163.96.0
#	broadcast 10.163.111.255
#	gateway 10.163.111.247
	up route add -net 172.16.0.0 netmask 255.240.0.0 gw 10.163.111.247 dev eth0
	up route add -net 100.64.0.0 netmask 255.192.0.0 gw 10.163.111.247 dev eth0
	up route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.163.111.247 dev eth0
	# dns-* options are implemented by the resolvconf package, if installed
	dns-nameservers 10.202.72.118 10.202.72.116
	dns-search oolap.com

auto eth1
iface eth1 inet static
	address 120.27.32.184
	netmask 255.255.252.0
	gateway 120.27.35.247

这个配置文件语法可以参考 man interfaces. auto <网卡名> 表示系统启动时自动启动该网卡. iface eth0 inet static 表示网卡 eth0 使用静态 IP 配置, 随后一段内容是该网卡配置的详细信息. 如果使用 DHCP 动态 IP 配置, 则只需要 iface eth0 inet dhcp 一行即可. 配置好这个文件后系统启动时即会自动配置好网卡信息. 也可直接使用命令行操作网卡配置.

跟网络配置相关的命令主要有 3 个, 我们最常用的 ifconfig 不带任何参数, 不需要 sudo 权限, 是查看所以已启动的网卡信息. ifupifdown 跟一个网卡名参数可以启动和关闭在 /etc/network/interfaces 文件中配置的指定网卡, 需要 sudo 权限. 如 sudo ifup eth0. ifconfig 也可以跟一个网卡名参数查看指定网卡信息, 如 ifconfig eth0. ifconfig 还可以直接动态配置和修改指定网卡信息, 这时需要 sudo 权限, 具体可以参考文档man ifconfig. ifconfig 也包含 updown 操作, 如 sudo ifconfig eth0 up. 跟 ifupifdown 不同, 这只是拉起了网卡设备, 而没有进行相应的网络配置. 猜想 ifupifdown 应该是在 ifconfig 的基础上封装的高级命令, 其调用 ifconfig 根据 /etc/network/interfaces 配置对网卡进行操作.

继续看配置文件, 接下来的 address, netmask, gateway 分别是配置静态 IP 地址, 子网掩码和网关, 我们就略过不说了. 子网地址 network 和广播地址 broadcast 可以直接根据 IP 地址和子网掩码推断出来, 所以我把这两行也注释了.

接下来 up route add -net 172.16.0.0 netmask 255.240.0.0 gw 10.163.111.247 dev eth0 这一行是在网卡启动时调用 route 命令配置路由信息. 我们可以使用 netstat -rn 命令查看路由信息 (或者 route -n, 两者输出基本相同, 稍有差异). 如在上述 vps 网卡配置好启动后可看到路由信息如下.

$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         120.27.35.247   0.0.0.0         UG        0 0          0 eth1
10.0.0.0        10.163.111.247  255.0.0.0       UG        0 0          0 eth0
10.163.96.0     0.0.0.0         255.255.240.0   U         0 0          0 eth0
100.64.0.0      10.163.111.247  255.192.0.0     UG        0 0          0 eth0
120.27.32.0     0.0.0.0         255.255.252.0   U         0 0          0 eth1
172.16.0.0      10.163.111.247  255.240.0.0     UG        0 0          0 eth0

上述配置文件中可看到我还注释了网卡 eth0 的网关 gateway 这一行配置. 这是因为在这台 vps 上有两个网卡, eth0 是内网网卡, eth1 是外网网卡. 但一台机器上只能有一个默认网关, 默认网关表示所有没有配置路由规则的 IP 地址都通过该网关到达. 我们要访问外网, 所有外网 IP 地址都应该通过外网网卡 eth1 的网关到达, 所以我们设置 eth1 的网关为默认网关, eth0 就不能设置网关 gateway. eth1 下的这一句网关设置 gateway 120.27.35.247, 等价于 up route add -net 0.0.0.0 netmask 0.0.0.0 gw 120.27.35.247 dev eth1 .

接下来的 dns-nameserversdns-search, 表示启动该网卡时进行对应的 dns 解析设置. 如注释所说, dns 解析设置是由 resolvconf 这个软件包实现的. dns 解析配置文件是 /etc/resolv.conf, resolvconf 软件包将该文件修改为指向其自动生成的一个文件的软连接. 网卡配置包含 dns 解析配置时, resolvconf 软件包自动将对应的 dns 配置添加到其自动生成的 dns 配置文件.

$ ll /etc/resolv.conf 
lrwxrwxrwx 1 root root 29  3月 28 12:42 /etc/resolv.conf -> ../run/resolvconf/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
nameserver 10.202.72.118
nameserver 10.202.72.116
search oolap.com

最后再提一下, 桌面系统和其他软件可能以其他方式来管理网卡配置, 所以 ifconfig 看到的网卡信息可能比 /etc/network/interfaces 多. 如 vmware 自己管理虚拟网卡配置, ifconfig 可以看到 vmnet8 这个 vmware NAT 网络的网卡, 但其配置不在 /etc/network/interfaces 中. ubuntu 桌面系统使用 GUI 管理网卡配置, 其 /etc/network/interfaces 配置文件中只有如下两行内容.

$ cat /etc/network/interfaces 
auto lo
iface lo inet loopback

下面再补记一些操作笔记. 上述 vps 上, 我们停止网卡 eth0, 可看到路由信息变化如下.

$ sudo ifdown eth0

$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         120.27.35.247   0.0.0.0         UG        0 0          0 eth1
120.27.32.0     0.0.0.0         255.255.252.0   U         0 0          0 eth1

作为一个错误的配置示范, 我们取消注释 eth0gateway 配置, 再执行 ifup.

$ sudo ifup eth0
RTNETLINK answers: File exists
Failed to bring up eth0.

$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         120.27.35.247   0.0.0.0         UG        0 0          0 eth1
10.163.96.0     0.0.0.0         255.255.240.0   U         0 0          0 eth0
120.27.32.0     0.0.0.0         255.255.252.0   U         0 0          0 eth1

可以看到操作失败了, 但是 eth0 已经被拉起来了, ifconfig 也可以看到 eth0 的配置, 路由规则多了一条配置, 但是其没有被正确配置, 我们尝试 ping 一下其他内网 IP ping 10.163.96.1 发现也是 ping 不通的.

尝试用 ifdown 停止网卡:

$ sudo ifdown eth0
ifdown: interface eth0 not configured

$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         120.27.35.247   0.0.0.0         UG        0 0          0 eth1
10.163.96.0     0.0.0.0         255.255.240.0   U         0 0          0 eth0
120.27.32.0     0.0.0.0         255.255.252.0   U         0 0          0 eth1

$ sudo ifconfig eth0 down

$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         120.27.35.247   0.0.0.0         UG        0 0          0 eth1
120.27.32.0     0.0.0.0         255.255.252.0   U         0 0          0 eth1

ifdown 也不能正确停止网卡和清除路由, 但是 ifconfig eth0 down 可以停止网卡.

我们修正配置(注释掉 eth0gateway 配置), 再尝试启动网卡:

$ sudo ifup eth0
RTNETLINK answers: File exists
Failed to bring up eth0.

$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         120.27.35.247   0.0.0.0         UG        0 0          0 eth1
120.27.32.0     0.0.0.0         255.255.252.0   U         0 0          0 eth1

还是操作失败了. 这时网卡设置处于一个错误的中间状态, 不能恢复. 这时需要重启电脑可以解决问题. 系统重启后会自动根据配置重新设置好网卡信息.

有时 ifupifdown 提示如下错误:

$ sudo ifup eth0
Ignoring unknown interface eth0=eth0.

通常是因为配置文件有误, ifup, ifdown 没有找到对应的网卡配置. 如果配置的网卡不存在, 则会提示如下错误:

$ sudo ifup eth2
Cannot find device "eth2"
Failed to bring up eth2.

阿里云 vps 可以通过管理控制终端直接操作, 跟在本机操作虚拟机类似, 除了有点卡, 还有重启电脑时会暂时断开连接, 但是在电脑启动界面 (操作系统启动前) 就能连上去了. 这样很好, 我们可以通过管理控制终端对 vps 进行各种操作, 包括重装系统, 但是重装的系统不能安装虚拟机支持软件和驱动 (类似 vmware-tools), windows 发现找不到网卡和附加磁盘. 以上测试在重装官方 ubuntu server 12.04 的 vps 上完成. 阿里云 vps 好像修改了一些细节设置, 如修改了 /etc/resolv.conf 软连接. 重装系统后我们可以按 ubuntu 官方文档学习和测试, 避免掉进如设置 dns-search 不生效等被阿里云改过的坑里.

Clone this wiki locally