网络基础 #

一、网络配置 #

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

bash
# 查看所有网络接口
ip addr
ip a

# 查看指定接口
ip addr show eth0

# 查看链路状态
ip link
ip link show eth0

# 启用/禁用接口
sudo ip link set eth0 up
sudo ip link set eth0 down

# 配置 IP 地址
sudo ip addr add 192.168.1.100/24 dev eth0

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

# 查看路由表
ip route
ip r

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

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

# 删除路由
sudo ip route del 10.0.0.0/24

1.2 ifconfig - 传统网络配置 #

bash
# 查看网络接口
ifconfig
ifconfig eth0

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

# 启用/禁用接口
sudo ifconfig eth0 up
sudo ifconfig eth0 down

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

# 修改 MAC 地址
sudo ifconfig eth0 hw ether 00:11:22:33:44:55

1.3 网络配置文件 #

Ubuntu/Debian:

bash
# /etc/netplan/01-netcfg.yaml
network:
  version: 2
  ethernets:
    eth0:
      dhcp4: no
      addresses:
        - 192.168.1.100/24
      gateway4: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4

# 应用配置
sudo netplan apply

CentOS/RHEL:

bash
# /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

# 重启网络
sudo systemctl restart network

1.4 DNS 配置 #

bash
# 查看 DNS 配置
cat /etc/resolv.conf

# 编辑 DNS 配置
sudo vim /etc/resolv.conf

# 添加 DNS 服务器
nameserver 8.8.8.8
nameserver 8.8.4.4
search example.com

# 测试 DNS 解析
nslookup google.com
dig google.com
host google.com

二、网络诊断 #

2.1 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

# 持续 ping 并记录时间
ping -D google.com

# 查看统计
ping -c 5 -q google.com

2.2 traceroute - 跟踪路由 #

bash
# 跟踪路由
traceroute google.com

# 使用 ICMP
traceroute -I google.com

# 使用 TCP
traceroute -T google.com

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

# 安装(如果没有)
sudo apt install traceroute

2.3 tracepath - 跟踪路径 #

bash
# 跟踪路径
tracepath google.com

# 指定端口
tracepath -p 80 google.com

2.4 mtr - 网络诊断工具 #

bash
# 安装
sudo apt install mtr

# 实时诊断
mtr google.com

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

# 显示 IP
mtr -n google.com

2.5 ss - 查看套接字 #

bash
# 查看所有套接字
ss

# 查看 TCP 连接
ss -t

# 查看 UDP 连接
ss -u

# 查看监听端口
ss -l

# 显示进程信息
ss -p

# 显示数字地址
ss -n

# 组合使用
ss -tlnp    # TCP 监听端口及进程
ss -tuln    # TCP 和 UDP 监听端口
ss -s       # 统计信息

# 查看指定端口
ss -tlnp | grep :80
ss -tlnp sport = :80

2.6 netstat - 网络统计 #

bash
# 查看所有连接
netstat -a

# 查看 TCP 连接
netstat -t

# 查看 UDP 连接
netstat -u

# 查看监听端口
netstat -l

# 显示进程信息
netstat -p

# 显示数字地址
netstat -n

# 组合使用
netstat -tlnp    # TCP 监听端口及进程
netstat -tuln    # TCP 和 UDP 监听端口
netstat -rn      # 路由表
netstat -i       # 网络接口统计
netstat -s       # 网络统计

2.7 lsof - 查看打开的文件 #

bash
# 查看端口占用
sudo lsof -i :80

# 查看所有网络连接
sudo lsof -i

# 查看 TCP 连接
sudo lsof -i TCP

# 查看 UDP 连接
sudo lsof -i UDP

# 查看指定用户的连接
sudo lsof -i -u username

# 查看指定进程的连接
sudo lsof -i -p PID

三、网络工具 #

3.1 curl - 数据传输工具 #

bash
# GET 请求
curl https://example.com

# 显示响应头
curl -i https://example.com

# 只显示响应头
curl -I https://example.com

# POST 请求
curl -X POST -d "key=value" https://example.com/api

# 发送 JSON
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://example.com/api

# 下载文件
curl -O https://example.com/file.zip
curl -o file.zip https://example.com/file.zip

# 断点续传
curl -C - -O https://example.com/file.zip

# 设置超时
curl --connect-timeout 10 https://example.com

# 跟随重定向
curl -L https://example.com

# 设置 User-Agent
curl -A "Mozilla/5.0" https://example.com

# 使用代理
curl -x http://proxy:8080 https://example.com

# 上传文件
curl -T file.txt ftp://example.com/upload/

3.2 wget - 下载工具 #

bash
# 下载文件
wget https://example.com/file.zip

# 指定文件名
wget -O myfile.zip https://example.com/file.zip

# 断点续传
wget -c https://example.com/file.zip

# 后台下载
wget -b https://example.com/file.zip

# 限速下载
wget --limit-rate=1M https://example.com/file.zip

# 递归下载网站
wget -r -np -k https://example.com

# 下载多个文件
wget -i urls.txt

# 镜像网站
wget -m https://example.com

3.3 nc - 网络工具 #

bash
# 测试端口连通性
nc -zv example.com 80

# 扫描端口范围
nc -zv example.com 1-1000

# 传输文件
# 发送端
nc -l 1234 < file.txt
# 接收端
nc example.com 1234 > file.txt

# 简单聊天
# 服务端
nc -l 1234
# 客户端
nc example.com 1234

# 端口转发
nc -l 8080 -c "nc example.com 80"

3.4 telnet - 远程登录 #

bash
# 测试端口
telnet example.com 80

# 远程登录
telnet example.com

3.5 nmap - 端口扫描 #

bash
# 安装
sudo apt install nmap

# 扫描主机
nmap example.com

# 扫描端口
nmap -p 80,443 example.com

# 扫描端口范围
nmap -p 1-1000 example.com

# 扫描所有端口
nmap -p- example.com

# 服务版本检测
nmap -sV example.com

# 操作系统检测
nmap -O example.com

# 扫描网段
nmap -sP 192.168.1.0/24

四、防火墙 #

4.1 ufw - Ubuntu 防火墙 #

bash
# 查看状态
sudo ufw status

# 启用防火墙
sudo ufw enable

# 禁用防火墙
sudo ufw disable

# 允许端口
sudo ufw allow 80
sudo ufw allow 443
sudo ufw allow 8080/tcp

# 拒绝端口
sudo ufw deny 80

# 允许 IP
sudo ufw allow from 192.168.1.100

# 允许 IP 访问指定端口
sudo ufw allow from 192.168.1.100 to any port 22

# 删除规则
sudo ufw delete allow 80

# 重置规则
sudo ufw reset

# 查看规则编号
sudo ufw status numbered

# 删除指定规则
sudo ufw delete 1

4.2 firewalld - CentOS 防火墙 #

bash
# 查看状态
sudo firewall-cmd --state

# 查看所有规则
sudo firewall-cmd --list-all

# 开放端口
sudo firewall-cmd --add-port=80/tcp
sudo firewall-cmd --add-port=80/tcp --permanent

# 移除端口
sudo firewall-cmd --remove-port=80/tcp
sudo firewall-cmd --remove-port=80/tcp --permanent

# 开放服务
sudo firewall-cmd --add-service=http
sudo firewall-cmd --add-service=http --permanent

# 重载配置
sudo firewall-cmd --reload

# 查看开放的端口
sudo firewall-cmd --list-ports

4.3 iptables - 底层防火墙 #

bash
# 查看规则
sudo iptables -L
sudo iptables -L -n -v

# 允许端口
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# 拒绝端口
sudo iptables -A INPUT -p tcp --dport 80 -j DROP

# 允许 IP
sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT

# 拒绝 IP
sudo iptables -A INPUT -s 192.168.1.100 -j DROP

# 删除规则
sudo iptables -D INPUT 1

# 清空规则
sudo iptables -F

# 保存规则
sudo iptables-save > /etc/iptables/rules.v4

# 恢复规则
sudo iptables-restore < /etc/iptables/rules.v4

五、远程连接 #

5.1 SSH 基础 #

bash
# 连接远程主机
ssh user@hostname
ssh user@192.168.1.100

# 指定端口
ssh -p 2222 user@hostname

# 使用密钥
ssh -i ~/.ssh/id_rsa user@hostname

# 执行远程命令
ssh user@hostname 'command'

# 复制文件到远程
scp file.txt user@hostname:/path/

# 从远程复制文件
scp user@hostname:/path/file.txt ./

# 复制目录
scp -r directory/ user@hostname:/path/

# 使用 rsync 同步
rsync -avz directory/ user@hostname:/path/

5.2 SSH 密钥管理 #

bash
# 生成密钥对
ssh-keygen -t rsa -b 4096
ssh-keygen -t ed25519

# 生成密钥并指定文件名
ssh-keygen -t rsa -f ~/.ssh/mykey

# 复制公钥到远程
ssh-copy-id user@hostname
ssh-copy-id -i ~/.ssh/id_rsa.pub user@hostname

# 手动复制公钥
cat ~/.ssh/id_rsa.pub | ssh user@hostname 'cat >> ~/.ssh/authorized_keys'

# 设置权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 644 ~/.ssh/authorized_keys

5.3 SSH 配置文件 #

bash
# 编辑配置文件
vim ~/.ssh/config

# 配置示例
Host myserver
    HostName 192.168.1.100
    User username
    Port 2222
    IdentityFile ~/.ssh/mykey

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3

# 使用别名连接
ssh myserver

5.4 SSH 隧道 #

bash
# 本地端口转发
ssh -L 8080:localhost:80 user@hostname

# 远程端口转发
ssh -R 8080:localhost:80 user@hostname

# 动态端口转发(SOCKS 代理)
ssh -D 1080 user@hostname

# 后台运行
ssh -fNL 8080:localhost:80 user@hostname

六、网络监控 #

6.1 iftop - 带宽监控 #

bash
# 安装
sudo apt install iftop

# 监控网络流量
sudo iftop

# 指定接口
sudo iftop -i eth0

# 显示端口
sudo iftop -P

# 显示字节
sudo iftop -B

6.2 nethogs - 进程流量监控 #

bash
# 安装
sudo apt install nethogs

# 监控进程流量
sudo nethogs

# 指定接口
sudo nethogs eth0

# 刷新间隔
sudo nethogs -d 2

6.3 tcpdump - 抓包工具 #

bash
# 抓取所有包
sudo tcpdump

# 抓取指定接口
sudo tcpdump -i eth0

# 抓取指定端口
sudo tcpdump port 80

# 抓取指定主机
sudo tcpdump host 192.168.1.100

# 抓取 TCP 包
sudo tcpdump tcp

# 保存到文件
sudo tcpdump -w capture.pcap

# 从文件读取
sudo tcpdump -r capture.pcap

# 显示 ASCII
sudo tcpdump -A

# 显示十六进制
sudo tcpdump -XX

# 组合使用
sudo tcpdump -i eth0 -nn -s0 -v port 80

七、实践练习 #

7.1 练习一:网络配置 #

bash
# 1. 查看网络接口
ip addr

# 2. 查看路由表
ip route

# 3. 查看 DNS 配置
cat /etc/resolv.conf

# 4. 测试 DNS 解析
nslookup google.com

7.2 练习二:网络诊断 #

bash
# 1. 测试连通性
ping -c 5 google.com

# 2. 跟踪路由
traceroute google.com

# 3. 查看端口
ss -tlnp

# 4. 查看网络连接
netstat -tuln

7.3 练习三:防火墙配置 #

bash
# 1. 查看防火墙状态
sudo ufw status

# 2. 允许 SSH
sudo ufw allow 22

# 3. 允许 HTTP
sudo ufw allow 80

# 4. 启用防火墙
sudo ufw enable

# 5. 查看规则
sudo ufw status numbered

7.4 练习四:SSH 连接 #

bash
# 1. 生成密钥对
ssh-keygen -t ed25519

# 2. 复制公钥到远程
ssh-copy-id user@hostname

# 3. 测试免密登录
ssh user@hostname

# 4. 配置 SSH 别名
vim ~/.ssh/config

八、小结 #

本章学习了 Linux 网络基础命令,包括网络配置、网络诊断、防火墙和远程连接。

关键要点:

  1. ip 是新一代网络配置工具
  2. pingtraceroutemtr 用于网络诊断
  3. ssnetstat 用于查看网络连接
  4. ufwfirewalld 用于防火墙管理
  5. SSH 是安全的远程连接工具

下一章预告: 软件包管理 - 学习不同发行版的软件包管理工具。

最后更新:2026-03-27