网络基础 #

网络接口配置 #

ifconfig - 配置网络接口 #

bash
# 查看所有网络接口
$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::a00:27ff:fe8e:8aaa  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:8e:8a:aa  txqueuelen 1000  (Ethernet)
        RX packets 10000  bytes 1000000 (1.0 MB)
        TX packets 5000  bytes 500000 (500.0 KB)

# 查看指定接口
$ ifconfig eth0

# 启用接口
$ sudo ifconfig eth0 up

# 禁用接口
$ sudo ifconfig eth0 down

# 配置 IP 地址
$ sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0

# 添加虚拟 IP
$ sudo ifconfig eth0:1 192.168.1.101 netmask 255.255.255.0

# 修改 MTU
$ sudo ifconfig eth0 mtu 9000

# 启用混杂模式
$ sudo ifconfig eth0 promisc

ip - 新一代网络配置工具 #

bash
# 查看所有接口
$ ip addr
$ ip a

# 查看指定接口
$ ip addr show eth0

# 添加 IP 地址
$ sudo ip addr add 192.168.1.100/24 dev eth0

# 删除 IP 地址
$ sudo ip addr del 192.168.1.100/24 dev eth0

# 启用接口
$ sudo ip link set eth0 up

# 禁用接口
$ sudo ip link set eth0 down

# 查看路由
$ ip route
$ ip r

# 添加路由
$ sudo ip route add default via 192.168.1.1

# 添加静态路由
$ sudo ip route add 10.0.0.0/8 via 192.168.1.1

# 删除路由
$ sudo ip route del default

# 查看 ARP 表
$ ip neigh
$ ip n

# 查看 TCP 连接
$ ss -t

# 查看 UDP 连接
$ ss -u

ip 命令速查 #

text
┌─────────────────────────────────────────────────────────────┐
│                    ip 命令速查                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   对象          命令                                        │
│   ─────────────────────────────────────────────────────     │
│   link          ip link show                               │
│   addr          ip addr show                               │
│   route         ip route show                              │
│   neigh         ip neigh show                              │
│                                                             │
│   常用操作:                                                 │
│   ip a                   显示所有地址                       │
│   ip a show eth0         显示 eth0 地址                     │
│   ip r                   显示路由表                         │
│   ip link set eth0 up    启用 eth0                          │
│   ip link set eth0 down  禁用 eth0                          │
│                                                             │
└─────────────────────────────────────────────────────────────┘

网络诊断 #

ping - 测试连通性 #

bash
# 测试连通性
$ ping google.com
$ ping 8.8.8.8

# 指定次数
$ ping -c 5 google.com

# 指定间隔
$ ping -i 2 google.com

# 指定包大小
$ ping -s 1000 google.com

# 设置 TTL
$ ping -t 64 google.com

# 洪水 ping
$ ping -f google.com

# 显示时间戳
$ ping -D google.com

# 指定源地址
$ ping -I eth0 google.com

traceroute - 跟踪路由 #

bash
# 跟踪路由
$ traceroute google.com

# 使用 TCP
$ traceroute -T google.com

# 指定端口
$ traceroute -p 80 google.com

# 指定最大跳数
$ traceroute -m 30 google.com

# 显示 IP 地址
$ traceroute -n google.com

# 使用 ICMP
$ traceroute -I google.com

mtr - 网络诊断工具 #

bash
# 安装
$ sudo apt install mtr

# 运行 mtr
$ mtr google.com

# 报告模式
$ mtr -r -c 10 google.com

# 显示 IP 地址
$ mtr -n google.com

# 指定包大小
$ mtr -s 100 google.com

# 使用 TCP
$ mtr -T google.com

# 使用 UDP
$ mtr -u google.com

DNS 查询 #

nslookup - DNS 查询 #

bash
# 查询域名
$ nslookup google.com

# 指定 DNS 服务器
$ nslookup google.com 8.8.8.8

# 查询特定记录
$ nslookup -type=mx gmail.com
$ nslookup -type=ns gmail.com
$ nslookup -type=txt gmail.com

# 反向查询
$ nslookup 8.8.8.8

dig - DNS 查询工具 #

bash
# 查询域名
$ dig google.com

# 指定 DNS 服务器
$ dig @8.8.8.8 google.com

# 查询特定记录
$ dig google.com A
$ dig google.com AAAA
$ dig google.com MX
$ dig google.com NS
$ dig google.com TXT
$ dig google.com CNAME

# 反向查询
$ dig -x 8.8.8.8

# 简短输出
$ dig +short google.com

# 显示 DNS 服务器
$ dig +trace google.com

# 只显示答案
$ dig +answer google.com

# 查询所有记录
$ dig google.com ANY

host - DNS 查询 #

bash
# 查询域名
$ host google.com

# 查询特定记录
$ host -t mx gmail.com
$ host -t ns gmail.com
$ host -t txt gmail.com

# 反向查询
$ host 8.8.8.8

# 详细输出
$ host -v google.com

网络信息 #

hostname - 主机名 #

bash
# 查看主机名
$ hostname

# 查看完整域名
$ hostname -f

# 查看 IP 地址
$ hostname -I

# 查看 DNS 域名
$ hostname -d

# 修改主机名
$ sudo hostnamectl set-hostname new-hostname

# 查看主机信息
$ hostnamectl

curl - 获取网络信息 #

bash
# 获取公网 IP
$ curl ifconfig.me
$ curl ipinfo.io/ip
$ curl icanhazip.com

# 获取 IP 信息
$ curl ipinfo.io

# 检查端口
$ curl -v telnet://example.com:80

# 检查 HTTP 头
$ curl -I https://google.com

# 测试响应时间
$ curl -o /dev/null -s -w "Time: %{time_total}s\n" https://google.com

网络配置文件 #

常见配置文件 #

text
┌─────────────────────────────────────────────────────────────┐
│                    网络配置文件                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   /etc/hosts          本地主机名解析                         │
│   /etc/resolv.conf    DNS 配置                              │
│   /etc/hostname       主机名                                │
│   /etc/network/       网络配置目录(Debian)                 │
│   /etc/sysconfig/network-scripts/  网络配置(RHEL)          │
│   /etc/services       服务端口映射                          │
│   /etc/protocols      协议号                                │
│                                                             │
└─────────────────────────────────────────────────────────────┘

/etc/hosts #

bash
# 查看 hosts 文件
$ cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       server01
192.168.1.100   webserver
192.168.1.101   dbserver

# 添加主机映射
$ echo "192.168.1.100 myserver" | sudo tee -a /etc/hosts

/etc/resolv.conf #

bash
# 查看 DNS 配置
$ cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
search example.com

# 临时修改 DNS
$ echo "nameserver 1.1.1.1" | sudo tee /etc/resolv.conf

小结 #

通过本节学习,你应该掌握:

  1. 网络接口配置:ifconfig、ip
  2. 网络诊断:ping、traceroute、mtr
  3. DNS 查询:nslookup、dig、host
  4. 网络信息:hostname、curl
  5. 配置文件:hosts、resolv.conf

下一步,我们将学习网络工具命令。

最后更新:2026-04-11