#网络基础
#概述
本章介绍 Linux 网络配置和管理的基础知识,包括网络接口、IP 地址配置、DNS 设置等。
#网络配置查看
#ip 命令
ip 是现代 Linux 网络配置的主要工具。
# 查看所有网络接口
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 ...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 ...
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 ...
# 查看 IP 地址
$ ip addr
$ ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>
inet 192.168.1.100/24 brd 192.168.1.255 scope global eth0
inet6 fe80::1/64 scope link
# 查看特定接口
$ ip addr show eth0
# 查看路由表
$ ip route
$ ip r
default via 192.168.1.1 dev eth0 proto dhcp
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100
# 查看 ARP 缓存
$ ip neigh
192.168.1.1 dev eth0 lladdr aa:bb:cc:dd:ee:ff REACHABLE#ifconfig(传统工具)
# 安装
$ sudo apt install net-tools
# 查看所有接口
$ ifconfig
# 查看特定接口
$ ifconfig eth0#网络接口信息
# 查看网络接口统计
$ ip -s link
# 查看接口详情
$ ethtool eth0
# 查看无线信息
$ iwconfig wlan0#配置 IP 地址
#临时配置
# 添加 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
# 添加默认网关
$ sudo ip route add default via 192.168.1.1
# 删除默认网关
$ sudo ip route del default#永久配置(Netplan - Ubuntu)
Ubuntu 18.04+ 使用 Netplan 配置网络。
# /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: true# 静态 IP 配置
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4# 应用配置
$ sudo netplan apply
# 测试配置
$ sudo netplan try#永久配置(NetworkManager)
# 查看连接
$ nmcli connection show
# 查看设备
$ nmcli device status
# 配置静态 IP
$ sudo nmcli connection modify "Wired connection 1" \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "8.8.8.8,8.8.4.4" \
ipv4.method manual
# 重新激活连接
$ sudo nmcli connection up "Wired connection 1"
# 配置 DHCP
$ sudo nmcli connection modify "Wired connection 1" ipv4.method auto#永久配置(传统方式 - Debian)
# /etc/network/interfaces
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
# 或 DHCP
auto eth0
iface eth0 inet dhcp# 重启网络
$ sudo systemctl restart networking#DNS 配置
#/etc/resolv.conf
$ cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
search example.com#systemd-resolved
# 查看 DNS 状态
$ resolvectl status
# 查看当前 DNS
$ resolvectl dns#/etc/hosts
本地主机名解析:
$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 mycomputer
192.168.1.10 server.local server
# 添加自定义解析
$ sudo echo "192.168.1.20 myserver" >> /etc/hosts#主机名配置
# 查看主机名
$ hostname
$ hostnamectl
# 设置主机名
$ sudo hostnamectl set-hostname newhostname
# 或编辑文件
$ sudo vim /etc/hostname#网络诊断工具
#ping - 测试连通性
# 基本使用
$ ping google.com
PING google.com (142.250.185.206) 56(84) bytes of data.
64 bytes from 142.250.185.206: icmp_seq=1 ttl=115 time=10.5 ms
# 指定次数
$ ping -c 4 google.com
# 指定间隔
$ ping -i 2 google.com
# 指定数据包大小
$ ping -s 1000 google.com#traceroute - 路由追踪
# 安装
$ sudo apt install traceroute
# 追踪路由
$ traceroute google.com
# 使用 ICMP
$ sudo traceroute -I google.com
# 使用 TCP
$ sudo traceroute -T google.com#mtr - 综合诊断
# 安装
$ sudo apt install mtr
# 运行(交互模式)
$ mtr google.com
# 报告模式
$ mtr -r -c 10 google.com#dig - DNS 查询
# 安装
$ sudo apt install dnsutils
# 查询 A 记录
$ dig google.com
# 查询特定记录类型
$ dig google.com MX
$ dig google.com NS
$ dig google.com TXT
# 简短输出
$ dig +short google.com
# 指定 DNS 服务器
$ dig @8.8.8.8 google.com
# 反向查询
$ dig -x 8.8.8.8#nslookup - DNS 查询
$ nslookup google.com
$ nslookup google.com 8.8.8.8#host - 简单 DNS 查询
$ host google.com
$ host -t MX google.com#端口和连接
#ss - 套接字统计
# 查看所有连接
$ ss
# 查看监听端口
$ ss -l
# 查看 TCP 连接
$ ss -t
# 查看 UDP 连接
$ ss -u
# 显示进程信息
$ ss -p
# 显示数字格式
$ ss -n
# 常用组合
$ ss -tlnp # TCP 监听端口,显示进程
$ ss -tunap # 所有 TCP/UDP,显示进程
# 过滤特定端口
$ ss -tlnp | grep :80
$ ss -tlnp sport = :80#netstat(传统工具)
# 安装
$ sudo apt install net-tools
# 查看监听端口
$ netstat -tlnp
# 查看所有连接
$ netstat -anp
# 查看路由表
$ netstat -r#lsof - 查看端口占用
# 查看端口占用
$ sudo lsof -i :80
$ sudo lsof -i :22
# 查看特定进程的网络连接
$ sudo lsof -i -p 1234#网络测试工具
#curl - HTTP 请求
# GET 请求
$ curl https://example.com
# 显示响应头
$ curl -I https://example.com
# 显示详细信息
$ curl -v https://example.com
# POST 请求
$ curl -X POST -d "key=value" https://example.com
# JSON 数据
$ curl -X POST -H "Content-Type: application/json" \
-d '{"key":"value"}' https://example.com
# 下载文件
$ curl -O https://example.com/file.zip
$ curl -o newname.zip https://example.com/file.zip
# 跟随重定向
$ curl -L https://example.com
# 保存 Cookie
$ curl -c cookies.txt https://example.com
# 使用 Cookie
$ curl -b cookies.txt https://example.com#wget - 下载工具
# 下载文件
$ wget https://example.com/file.zip
# 指定文件名
$ wget -O newname.zip https://example.com/file.zip
# 后台下载
$ wget -b https://example.com/file.zip
# 断点续传
$ wget -c https://example.com/file.zip
# 限速下载
$ wget --limit-rate=1m https://example.com/file.zip
# 递归下载
$ wget -r https://example.com/#nc (netcat) - 网络瑞士军刀
# 端口扫描
$ nc -zv 192.168.1.1 1-1000
# 测试端口
$ nc -zv google.com 80
# 作为服务器
$ nc -l 8080
# 作为客户端
$ nc 192.168.1.1 8080
# 传输文件
# 接收端
$ nc -l 8080 > received_file
# 发送端
$ nc 192.168.1.1 8080 < file_to_send#防火墙
#UFW(Ubuntu)
# 启用防火墙
$ sudo ufw enable
# 禁用防火墙
$ sudo ufw disable
# 查看状态
$ sudo ufw status
$ sudo ufw status verbose
# 允许端口
$ sudo ufw allow 22
$ sudo ufw allow 80/tcp
$ sudo ufw allow 443
# 允许服务
$ sudo ufw allow ssh
$ sudo ufw allow http
$ sudo ufw allow https
# 拒绝端口
$ sudo ufw deny 23
# 删除规则
$ sudo ufw delete allow 80
# 允许特定 IP
$ sudo ufw allow from 192.168.1.100
# 允许子网
$ sudo ufw allow from 192.168.1.0/24#iptables
# 查看规则
$ 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 23 -j DROP
# 保存规则
$ sudo iptables-save > /etc/iptables.rules
# 恢复规则
$ sudo iptables-restore < /etc/iptables.rules#网络配置文件
| 文件 | 说明 |
|---|---|
/etc/hosts | 本地主机名解析 |
/etc/hostname | 主机名 |
/etc/resolv.conf | DNS 配置 |
/etc/network/interfaces | 网络接口配置(Debian) |
/etc/netplan/*.yaml | Netplan 配置(Ubuntu) |
/etc/sysconfig/network-scripts/ | 网络配置(RHEL) |
#小结
本章介绍了 Linux 网络基础:
- 查看配置:
ip、ifconfig - 配置网络:Netplan、NetworkManager
- DNS 配置:resolv.conf、hosts
- 诊断工具:
ping、traceroute、dig - 端口查看:
ss、netstat、lsof - 网络测试:
curl、wget、nc - 防火墙:
ufw、iptables
网络配置是服务器管理的重要技能,熟练掌握这些工具将帮助你诊断和解决网络问题。
上一章:服务管理
下一章:SSH 远程连接