Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
llthw-zh
提交
d1d0fe42
L
llthw-zh
项目概览
OpenDocCN
/
llthw-zh
通知
0
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
llthw-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d1d0fe42
编写于
8月 25, 2017
作者:
W
wizardforcel
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ex25
上级
dcd9b87a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
192 addition
and
0 deletion
+192
-0
ex25.md
ex25.md
+192
-0
未找到文件。
ex25.md
0 → 100644
浏览文件 @
d1d0fe42
# 练习 25:网络:配置文件,`/etc/network/interfaces`
> 原文:[Exercise 25. Networking: configuration files, /etc/network/interfaces](https://archive.fo/ckUKJ)
> 译者:[飞龙](https://github.com/wizardforcel)
> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
> 自豪地采用[谷歌翻译](https://translate.google.cn/)
从命令行配置网络接口是很好的,但现在是时候学习如何让
`vm1`
自动配置网络接口。为此,你将了解
`/etc/network/interfaces`
配置文件:
```
user1@vm1:~$ cat /etc/network/interfaces
# 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
#(1) (2)
auto lo
#(3) (4)(5) (6)
iface lo inet loopback
# The primary network interface
#(7) (8)
allow-hotplug eth0
#(9) (10) (11) (12)
iface eth0 inet dhcp
```
像往常一样,字段及其描述:
| 字段 | 描述 |
| --- | --- |
| (1) | 自动配置界面。 |
| (2) | 接口名称。 |
| (3) | 接口配置的开始 |
| (4) | 要配置的接口名称 |
| (5) | 此接口使用 TCP/IP 网络,IPv4。 |
| (6) | 它是回送接口。默认回送地址将自动分配给它。 |
| (7) | 在可用时自动配置接口(请在这里考虑 usb-modem)。 |
| (8) | 接口名称。 |
| (9) | 接口配置的开始 |
| (10) | 要配置的接口名称 |
| (11) | 此接口使用 TCP/IP 网络,IPv4。 |
| (12) | 此接口通过 DHCP 自动获取其参数。 |
其他包含网络配置的重要文件,但我们在这里不会碰到他们:
+
`/etc/hosts`
- 操作系统中使用的计算机文件,用于将主机名映射到 IP 地址。
`hosts`
文件是一个纯文本文件,通常按照惯例命名为
`hosts`
。
+
`/etc/hostname`
- 分配给连接到计算机网络的设备的标签,并用于识别各种形式的电子通信设备。
+
`/etc/resolv.conf`
- 各种操作系统中的计算机文件,用于配置域名系统( DNS)解析器库。该文件是纯文本文件,通常由网络管理员或管理系统配置任务的应用创建。
`resolvconf`
程序是 linux 机器上的这样的程序,它管理
`resolv.conf`
文件。
让我们回忆之前练习的
`tap0`
。如果你重新启动
`vm1`
, 它就会消失。当然,你可以通过重新输入相关命令来启用它,但是让我们想象一下,你需要在重新启动后自动使用该命令。
现在,你将学习如何使用
`/etc/network/interfaces`
文件来配置接口。
## 这样做
```
1: ip a s
2: sudo vim /etc/network/interfaces
```
现在将这些东西添加到配置文件末尾:
```
3: auto tap0
4: iface tap0 inet static
5: address 10.1.1.2
6: netmask 255.255.255.0
7: tunctl_user uml-net
8:
9: allow-hotplug tap1
10: iface tap1 inet static
11: address 10.1.1.3
12: netmask 255.255.255.0
```
现在键入
`:wq<ENTER>`
并继续:
```
13: sudo /etc/init.d/networking start
14: ip a s
15: sudo tunctl -t tap1 -u uml-net
16: ip a s
```
## 你会看到什么
```
user1@vm1:~$ ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:d4:45:68 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
inet6 fe80::a00:27ff:fed4:4568/64 scope link
valid_lft forever preferred_lft forever
user1@vm1:~$ sudo vim /etc/network/interfaces
# 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
allow-hotplug eth0
iface eth0 inet dhcp
auto tap0
iface tap0 inet static
address 10.2.2.2
netmask 255.255.255.0
tunctl_user uml-net
allow-hotplug tap1
iface tap1 inet static
address 10.3.3.3
netmask 255.255.255.0
~
"/etc/network/interfaces" 21L, 457C written 21,1-8 Bot
user1@vm1:~$ sudo /etc/init.d/networking start
Configuring network interfaces...Set 'tap0' persistent and owned by uid 104 done.
user1@vm1:~$ ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:d4:45:68 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
inet6 fe80::a00:27ff:fed4:4568/64 scope link
valid_lft forever preferred_lft forever
3: tap0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 500
link/ether 46:63:30:70:b5:21 brd ff:ff:ff:ff:ff:ff
inet 10.2.2.2/24 brd 10.2.2.255 scope global tap0
inet6 fe80::4463:30ff:fe70:b521/64 scope link
valid_lft forever preferred_lft forever
user1@vm1:~$ sudo tunctl -t tap1 -u uml-net
Set 'tap1' persistent and owned by uid 104
user1@vm1:~$ ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:d4:45:68 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
inet6 fe80::a00:27ff:fed4:4568/64 scope link
valid_lft forever preferred_lft forever
3: tap0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 500
link/ether 46:63:30:70:b5:21 brd ff:ff:ff:ff:ff:ff
inet 10.2.2.2/24 brd 10.2.2.255 scope global tap0
inet6 fe80::4463:30ff:fe70:b521/64 scope link
valid_lft forever preferred_lft forever
4: tap1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 500
link/ether 8a:ed:90:33:93:55 brd ff:ff:ff:ff:ff:ff
inet 10.3.3.3/24 brd 10.3.3.255 scope global tap1
inet6 fe80::88ed:90ff:fe33:9355/64 scope link
valid_lft forever preferred_lft forever
user1@vm1:~$
```
## 解释
1.
打印当前接口配置。
1.
编辑
`/etc/network/interfaces`
。
1.
自动配置
`tap0`
。
1.
为
`tap0`
设置以下 IPv4 静态参数。
1.
将 IP 地址
`10.2.2.2`
添加给
`tap0`
。
1.
为此 IP 地址指定网络掩码、参数“广播”和“网络”自动 从这个网络掩码导出。
1.
指定拥有
`tap0`
接口的用户。
1.
由于可读性的空行。
1.
在
`tap1`
接口出现在系统中时,添加以下参数。
1.
为
`tap1`
设置以下 IPv4 静态参数。
1.
将 IP 地址
`10.3.3.3`
添加给tap1。
1.
为此 IP 地址指定网络掩码。
1.
应用网络配置更改。
1.
打印当前接口配置。你可以看到
`tap0`
被添加到接口列表中。
1.
添加
`tap1`
伪接口。
1.
打印当前接口配置。你可以看到
`/etc/network/interfaces中指定的参数自动应用于它。
## 附加题
+ 说明如何导出“网络”和“广播”参数。
+ 尝试这个:`
ping kitty
`。预期会失败。现在添加一个条目到`
/etc/hosts
`,以便你能够成功执行`
ping
`
。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录