編譯自官方文檔
環(huán)境:Ubuntu 16.04 TLS
先說(shuō)測(cè)試的結(jié)果暮屡,從配置文件修改IP地址撤摸,重啟網(wǎng)絡(luò)服務(wù),新配置的IP地址從 ifconfig 命令的輸出看不到褒纲,看到IP地址沒(méi)有變化准夷,還是老的IP地址。那配置為什么沒(méi)有生效呢莺掠,別急衫嵌,使用 ip addr 可以看到新配置的IP,而且新的IP地址和老的IP地址都在彻秆,不過(guò)reboot后楔绞,老的IP地址就沒(méi)有了。這個(gè)跟 CentOS 是不一樣的唇兑,可能是 Ubuntu 的一個(gè)機(jī)制吧酒朵。
1.網(wǎng)卡配置
1.1, 以太網(wǎng)接口命名:enp0s3,..
lshw 查看網(wǎng)卡信息:
guli@guli-Ubuntu1:~$ lshw -c network
WARNING: you should run this program as super-user.
*-network
description: Ethernet interface
product: 82540EM Gigabit Ethernet Controller
vendor: Intel Corporation
physical id: 3
bus info: pci@0000:00:03.0
logical name: enp0s3
version: 02
serial: 08:00:27:5b:d8:f0
size: 1Gbit/s
capacity: 1Gbit/s
width: 32 bits
clock: 66MHz
capabilities: bus_master cap_list ethernet physical tp 10bt 10bt-fd 100bt 100bt-fd 1000bt-fd autonegotiation
configuration: autonegotiation=on broadcast=yes driver=e1000 driverversion=7.3.21-k8-NAPI duplex=full ip=192.168.5.132 latency=64 link=yes mingnt=255 multicast=yes port=twisted pair speed=1Gbit/s
resources: irq:10 memory:f0000000-f001ffff ioport:d010(size=8)
WARNING: output may be incomplete or inaccurate, you should run this program as super-user.
1.2,網(wǎng)卡邏輯名 enp0s3
在 /etc/udev/rules.d/70-persistent-net.rules 中進(jìn)行配置。
1.3,配置網(wǎng)卡的工具 ethtool
sudo apt-get install ethtool
查看 enp0s3 信息:
$ ethtool enp0s3
Settings for enp0s3:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
MDI-X: off (auto)
Cannot get wake-on-lan settings: Operation not permitted
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes
ethtool 的修改臨時(shí)生效扎附,重啟后丟失蔫耽,持久生效的配置,在 /etc/network/interfaces 中配置留夜,
如:pre-up ethtool ...
auto eth0
iface eth0 inet static
pre-up /sbin/ethtool -s eth0 speed 1000 duplex full
注匙铡,以上雖然配置為 static 模式图甜,但其實(shí)不是決定性的,如果有 DHCP 開(kāi)啟鳖眼,也會(huì)收到影響具则。
2,IP地址設(shè)置
本節(jié)講述IP地址和默認(rèn)網(wǎng)關(guān)的配置
2.1 臨時(shí)IP地址分配
ip, ifconfig, route 等命令的配置臨時(shí)生效具帮,重啟后丟失。
sudo ifconfig eth0 10.0.0.100 netmask 255.255.255.0
查看網(wǎng)絡(luò)配置:ifconfig eth0
配置默認(rèn)網(wǎng)關(guān):
sudo route add default gw 10.0.0.1 eth0
驗(yàn)證:
route -n
臨時(shí)配置 DNS: 可在 /etc/resolv.conf 中臨時(shí)添加 DNS 解析地址低斋,
nameserver 8.8.8.8
清除網(wǎng)卡的 IP 地址配置:
ip addr flush eth0
注蜂厅,這個(gè)操作不影響 /etc/resolv.conf 的內(nèi)容。
手動(dòng)清除膊畴,或者 reboot掘猿。reboot 可以重寫 /etc/resolv.conf(符號(hào)鏈接)
ll /etc/resolv.conf
lrwxrwxrwx 1 root root 29 3月 2 11:45 /etc/resolv.conf -> ../run/resolvconf/resolv.conf
2.2 DHCP client
使用 dhcp 方式獲取 IP地址
編輯 /etc/network/interfaces:
auto eth0
iface eth0 inet dhcp
然后執(zhí)行如下命令啟動(dòng) eth0:
sudo ifup eth0
要關(guān)閉 eth0,使用如下命令:
sudo ifdown eth0
2.3 靜態(tài)IP地址配置
配置示例:
auto eth0
iface eth0 inet static
address 10.0.0.100
netmask 255.255.255.0
gateway 10.0.0.1
然后執(zhí)行:
sudo ifup eth0
要關(guān)閉 eth0唇跨,使用如下命令:
sudo ifdown eth0
2.4 loopback
ifconfig lo
cat /etc/network/interface
auto lo
iface lo inet loopback
3. 域名解析
3.1 DNS client 配置
配置文件:/etc/resolv.conf
resolvconf 會(huì)自動(dòng)根據(jù)所在的網(wǎng)絡(luò)環(huán)境稠通,更新 /etc/resolv.conf,手動(dòng)的修改更改會(huì)被覆蓋买猖。
一套鉤子腳本會(huì)觸發(fā) resolvconf 更新 /etc/resolv.conf改橘。(比如 dhcp client hooks)。
/etc/network/interfaces 生成的名稱解析配置也會(huì)寫入 /etc/resolv.conf玉控。
如果要手動(dòng)配置 dns 服務(wù)器IP飞主,編輯 /etc/network/interfaces,示例如下:
iface eth0 inet static
address 192.168.3.3
netmask 255.255.255.0
gateway 192.168.3.1
dns-search example.com
dns-nameservers 192.168.3.45 192.168.8.10
dns-search example.com 是一種簡(jiǎn)化的配置高诺,例如:
ping server1
系統(tǒng)會(huì)自動(dòng)將 server1 替換成 FQDN(Fully Qualified Domain Name):
ping server1.example.com
向 DNS 服務(wù)器請(qǐng)求 server1.example.com 的域名解析碌识。
dns-search 可以配置多個(gè):
iface eth0 inet static
address 192.168.3.3
netmask 255.255.255.0
gateway 192.168.3.1
dns-search example.com sales.example.com dev.example.com
dns-nameservers 192.168.3.45 192.168.8.10
服務(wù)器依次向 DNS 服務(wù)器請(qǐng)求如下的域名解析。
1. server1.example.com
2. server1.sales.example.com
3. server1.dev.example.com
3.2 靜態(tài)域名解析
這是在 /etc/hosts 手動(dòng)配置域名解析虱而。其優(yōu)先級(jí)高于 DNS 查詢筏餐。
示例:
IP, hostnames, aliases and Fully Qualified Domain Names (FQDN's).
127.0.0.1 localhost
127.0.1.1 ubuntu-server
10.0.0.11 server1 server1.example.com vpn
10.0.0.12 server2 server2.example.com mail
10.0.0.13 server3 server3.example.com www
10.0.0.14 server4 server4.example.com file
3.3 Name Service Switch Configuration
這是用來(lái)選擇使用哪一種域名解析的方法,這里配置了 /etc/hosts 的高優(yōu)先級(jí)牡拇。
配置文件:/etc/nsswitch.conf
hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4
解釋:
? files first tries to resolve static hostnames located in /etc/hosts.(首先嘗試靜態(tài)文件解析)
? mdns4_minimal attempts to resolve the name using Multicast DNS. (嘗試多播DNS解析域名)
? [NOTFOUND=return] means that any response of notfound by the preceding (如果前一步失敗魁瞪,不再進(jìn)行解析嘗試)
mdns4_minimal process should be treated as authoritative and that the system
should not try to continue hunting for an answer.
? dns represents a legacy unicast DNS query. (單播 DNS 查詢)
? mdns4 represents a Multicast DNS query. (多播 DNS 查詢)
可以修改其中的順序:
hosts: files dns [NOTFOUND=return] mdns4_minimal mdns4
4.Bridging 橋接
橋接多個(gè)網(wǎng)卡。
一個(gè)場(chǎng)景:在多個(gè)網(wǎng)卡之間建立網(wǎng)橋惠呼,然后使用防火墻過(guò)濾網(wǎng)絡(luò)之間的流量佩番。
另一個(gè)場(chǎng)景:在一個(gè)網(wǎng)卡上建立網(wǎng)橋,使得虛擬機(jī)可以直接訪問(wèn)外網(wǎng)罢杉。
安裝網(wǎng)橋:
sudo apt install bridge-utils
這里演示后一種場(chǎng)景的配置:
編輯 /etc/network/interfaces:
auto lo
iface lo inet loopback
auto br0
iface br0 inet static
address 192.168.0.10
network 192.168.0.0
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1
bridge_ports eth0
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_stp off
啟動(dòng) bridge:
sudo ifup br0
brctl 查看橋接的信息趟畏,配置橋接包含的網(wǎng)卡。man brctl 可查看更多滩租。
參考:
https://help.ubuntu.com/community/Network http://manpages.ubuntu.com/manpages/man8/resolvconf.8.html http://manpages.ubuntu.com/manpages/man5/interfaces.5.html http://manpages.ubuntu.com/manpages/man8/dhclient.8.html http://manpages.ubuntu.com/manpages/man5/resolver.5.html http://oreilly.com/catalog/linag2/book/ch06.html
配置實(shí)例
環(huán)境:Ubuntu 16.04 TLS
ifconfig 顯示 IP 為 192.168.5.143
$ ifconfig
enp0s3 Link encap:Ethernet HWaddr 08:00:27:5b:d8:f0
inet addr:192.168.5.143 Bcast:192.168.5.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe5b:d8f0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:401 errors:0 dropped:0 overruns:0 frame:0
TX packets:245 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:37017 (37.0 KB) TX bytes:30774 (30.7 KB)
域名解析配置為:192.168.5.1
$ 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 192.168.1.1
修改配置:
$ cat interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto enp0s3
iface enp0s3 inet static
address 192.168.5.132
netmask 255.255.255.0
gateway 192.168.5.1
dns-nameserver 192.168.5.1
應(yīng)用配置:
sudo ifdown enp0s3; sudo ifup enp0s3
注赋秀,一次性執(zhí)行兩條命令利朵,可連續(xù)執(zhí)行。特別注意遠(yuǎn)程服務(wù)器不可以單步執(zhí)行猎莲,不然網(wǎng)卡關(guān)掉绍弟,ssh 就斷了。
ifconfig 看不出變化:
$ ifconfig
enp0s3 Link encap:Ethernet HWaddr 08:00:27:5b:d8:f0
inet addr:192.168.5.143 Bcast:192.168.5.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe5b:d8f0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:900 errors:0 dropped:0 overruns:0 frame:0
TX packets:544 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:79552 (79.5 KB) TX bytes:67798 (67.7 KB)
ip addr 可以看到新配置的IP:
$ ip addr
...
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:5b:d8:f0 brd ff:ff:ff:ff:ff:ff
inet 192.168.5.143/24 brd 192.168.5.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.168.5.132/24 brd 192.168.5.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe5b:d8f0/64 scope link
valid_lft forever preferred_lft forever
舊的IP也保留著著洼。
DNS 服務(wù)地址被更新了:
$ 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 192.168.5.1
保留舊的IP地址這點(diǎn)跟 CentOS 不太一樣樟遣。
就這些了哈。