本文档涵盖 TCP/IP 网络、网络管理和系统配置基础知识。Linux 可以支持多个网络设备。设备名称已编号,从零开始向上计数。例如,具有两个 NIC 的计算机将有两个标记为 eth0 和 eth1 的设备。
1: 本地 DNS 解析
/etc/hosts 文件包含需要再本地进行解析(而非通过 DNS)的主机列表
文件内容示例:
127.0.0.1 your-node-name.your-domain.com localhost.localdomain localhost
XXX.XXX.XXX.XXX node-name
hosts 文件的文件格式由 RFC 952 指定
2: 配置 DNS 服务器以进行域名解析
/etc/resolv.conf 文件包含用于域名解析的 DNS 服务器列表
文件内容示例:
nameserver 8.8.8.8 # 主域名服务器的 IP 地址
nameserver 8.8.4.4 # 第二个域名服务器的 IP 地址
如果是内部 DNS 服务器,您可以使用 dig 命令验证该服务器是否正确解析 DNS 名称:
$ dig google.com @your.dns.server.com +short
3: 查看并操作路由
使用 route 命令操作 IP 路由表
显示路由表
$ route # Displays list or routes and also resolves host names
$ route -n # Displays list of routes without resolving host names for faster results
添加/删除路由
Option | Description |
add or del | 添加或删除一条路由 |
-host x.x.x.x | 添加指向由 IP 地址标识的单个主机的路由 |
-net x.x.x.x | 添加至网络地址所标识的网络的路由 |
gw x.x.x.x | 指定网关 |
netmask x.x.x.x | 指定网络的子网掩码 |
default | 添加默认路由 |
示例:
添加路由到主机
$ route add -host x.x.x.x eth1
添加路由到网络
$ route add -net 2.2.2.0 netmask 255.255.255.0 eth0
或者,您也可以使用 cidr 格式添加网络路由
route add -net 2.2.2.0/24 eth0
添加默认网关
$ route add default gw 2.2.2.1 eth0
删除一条路由
$ route del -net 2.2.2.0/24
使用IP操作IP路由表
查看路由表
$ ip route show # List routing table
添加/删除路由
Option | Description |
add or del or change or append or replace | 更改一条路由 |
show or flush | 该命令显示路由表的内容或将其删除 |
restore | 从标准输入恢复路由表信息 |
get | 该命令获取通往目的地的一个路由并完全按照内核所看到的方式打印其内容 |
示例:
将默认网关设置为 1.2.3.254
$ ip route add default via 1.2.3.254
在设备 eth0 上添加默认路由经过本地网关 192.168.1.1 访问所有地址
$ ip route add default via 192.168.1.1 dev eth0
4: 为网络上的其他系统配置主机名
您可以在 Linux(或 macOS)系统,使用主机名作为标识符和你主机的IP地址一起绑定到网络中的其他系统。您可以这样配置它:
整个系统范围生效。您应该修改 /etc/hosts 文件。您只需向该文件添加一个新行,其中包含:
- 远程系统的 IP 地址
- 一个或多个空格作为分隔
- 标识符(主机名)
如果想针对单个用户生效。您就应该修改 ~/.hosts 文件若这个文件不存在就创建它。它不像系统范围那么简单。您可以在此处看到说明。
例如,您可以使用 cat Unix 工具添加此行。假设您想对本地网络中 IP 地址为 192.168.1.44 的 PC 进行 ping,并且您想仅通过 remote_pc 引用该 IP 地址。那么您必须在 shell 上写入:
$ sudo cat 192.168.1.44 remote_pc
然后你只需通过以下方式即可进行 ping:
$ ping remote_pc
5: Interface details
Ifconfig
列出计算机上所有可用的接口
$ ifconfig -a
List the details of a specific interface
列出特定接口的详细信息
用法: $ ifconfig
示例:
$ ifconfig eth0
eth0 Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xx
inet addr:x.x.x.x Bcast:x.x.x.x Mask:x.x.x.x
inet6 addr: xxxx::xxx:xxxx:xxxx:xxxx/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4426618 errors:0 dropped:1124 overruns:0 frame:0
TX packets:189171 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:382611580 (382.6 MB) TX bytes:36923665 (36.9 MB)
Interrupt:16 Memory:fb5e0000-fb600000
Ethtool - 查询网络驱动程序和硬件设置
用法: $ ethtool
示例:
$ ethtool eth0
Settings for eth0:
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: 1
Transceiver: internal
Auto-negotiation: on
MDI-X: on (auto)
Supports Wake-on: pumbg
Wake-on: g
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes
IP - 显示/操作路由、设备、路由策略和隧道
用法: $ ip { link | ... | route | macsec } (please see man ip for full list of objects)
示例:
列出网络接口
$ ip link show
将接口 eth0 重命名为 wan
$ ip link set dev eth0 name wan
启动(或关闭)接口 eth0
$ ip link set dev eth0 up
列出接口的地址
$ ip addr show
Add (or del) ip and mask (255.255.255.0)
添加(或 del)ip 和掩码 (255.255.255.0)
$ ip addr add 1.2.3.4/24 brd + dev eth0
6: 向网络接口设备添加 IP
接口的 IP 地址可以通过 DHCP 或静态分配获得
DHCP
如果您连接到正在运行 DHCP 服务器的网络,则 dhclient 命令可以获取接口的 IP 地址
$ dhclient
或者,您可以更改 /etc/network/interfaces 文件,以便在系统启动时,启动网络接口设备并获取 DHCP 分配给机器的 IP
auto eth0
iface eth0 inet dhcp
使用 /etc/network/interfaces 文件的静态配置(永久更改)
如果你想静态配置接口设置(永久更改),你可以在 /etc/network/interfaces 文件中进行。
示例:
auto eth0 # Bring up the interface on boot
iface eth0 inet static
address 10.10.70.10
netmask 255.255.0.0
gateway 10.10.1.1
dns-nameservers 10.10.1.20
dns-nameservers 10.10.1.30
即使在系统重启后,这些更改仍然存在。
使用 ifconfig 实用程序的静态配置(临时更改)
可以使用 ifconfig 实用程序将静态 IP 地址添加到接口中,如下所示
$ ifconfig / up
示例:
$ ifconfig eth0 10.10.50.100/16 up