跳转到内容

Linux 网络配置与管理 | 网络命令完全指南与故障排除

Linux Network Management

网络配置是 Linux 系统管理的核心技能之一。无论是服务器部署、容器编排还是日常开发,都离不开对网络的深入理解。本文将全面介绍 Linux 网络管理的各个方面。

Linux 常用网络操作

主机名操作

  • hostname:显示主机名
  • hostname XXX:修改主机名,不推荐,临时生效

永久生效修改主机名需要修改 /etc/sysconfig/network 文件

现代系统的主机名管理

bash
# 查看主机名
hostname
hostnamectl

# 临时修改主机名
hostname new-hostname

# 永久修改主机名(推荐)
sudo hostnamectl set-hostname new-hostname

# 修改静态主机名
sudo hostnamectl set-hostname "server.example.com" --static

# 修改相关文件
sudo vim /etc/hostname
sudo vim /etc/hosts

# 验证修改
hostname
hostname -f  # 完整限定域名

查询系统完整信息

bash
uname -a #显示完整的系统信息

uname 命令详解

bash
# 所有信息
uname -a

# 单独查询各项信息
uname -s    # 内核名称 (Linux)
uname -n    # 节点名称 (主机名)
uname -r    # 内核版本
uname -v    # 内核版本详细信息
uname -m    # 机器硬件名称 (x86_64)
uname -p    # 处理器类型
uname -i    # 硬件平台
uname -o    # 操作系统

# 示例输出
$ uname -a
Linux server 5.15.0-91-generic #101-Ubuntu SMP x86_64 GNU/Linux

IP 地址操作

bash
ip addr # 查看 IP 地址
vim /etc/sysconfig/network-scripts/ # 修改 IP 地址
service network restart # 重启网络服务

现代 IP 地址管理(ip 命令)

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

# 查看特定接口
ip addr show eth0
ip a show ens33

# 查看路由表
ip route show
ip r

# 查看 ARP 表
ip neigh show

# 临时添加 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 -s link show eth0

配置静态 IP(Ubuntu/Debian - Netplan)

yaml
# /etc/netplan/01-netcfg.yaml
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
bash
# 应用配置
sudo netplan apply

# 测试配置(120秒后自动回滚)
sudo netplan try

配置静态 IP(CentOS/RHEL)

bash
# 编辑网卡配置
sudo vim /etc/sysconfig/network-scripts/ifcfg-eth0

# 配置内容
TYPE=Ethernet
BOOTPROTO=static
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
# 或
sudo nmcli connection reload
sudo nmcli connection up eth0

域名映射

bash
vim /etc/hosts

/etc/hosts 文件详解

bash
# 查看当前配置
cat /etc/hosts

# 示例内容
127.0.0.1   localhost
127.0.1.1   myhostname
::1         localhost ip6-localhost ip6-loopback

# 添加自定义映射
192.168.1.100   server.example.com server
192.168.1.101   db.example.com db

# 编辑文件
sudo vim /etc/hosts

# 验证解析
ping server
getent hosts server

网络服务管理

bash
systemctl stauts network # 查看网络服务状态
systemctl start network # 启动网络服务
systemctl stop network # 停止网络服务
systemctl restart network # 重启网络服务
systemctl enable network # 设置开启启动

systemd 网络管理(现代系统)

bash
# Ubuntu/Debian (NetworkManager)
sudo systemctl status NetworkManager
sudo systemctl restart NetworkManager
sudo systemctl enable NetworkManager

# CentOS/RHEL (network)
sudo systemctl status network
sudo systemctl restart network
sudo systemctl enable network

# 使用 NetworkManager CLI
nmcli device status
nmcli connection show
nmcli connection up "Wired connection 1"
nmcli connection down "Wired connection 1"

# 查看网络连接
nmcli device wifi list
nmcli device wifi connect SSID password PASSWORD

传统网络服务管理

bash
# SysVinit 系统
sudo service networking restart
sudo service network-manager restart

# 查看网络接口
ifconfig  # 旧命令
ip addr   # 新命令(推荐)

# 重启特定接口
sudo ifdown eth0 && sudo ifup eth0

防火墙设置

bash
systemctl status firewalld #查看防火墙状态
systemctl start firewalld #启动防火墙
systemctl stop firewalld #关闭防火墙
systemctl is-enable firewalld #查看防火墙服务是否开机启动
systemctl enable firewalld #开机时启用防火墙服务
systemctl disable firewalld #开机时禁用防火墙服务
systemctl list-unit-files|grep enabled #查询已经启动的服务列表
systemctl --failed #查询启动失败的服务列表

firewalld 防火墙管理(CentOS/RHEL 7+)

bash
# 查看状态
sudo systemctl status firewalld
sudo firewall-cmd --state

# 启动/停止/重启
sudo systemctl start firewalld
sudo systemctl stop firewalld
sudo systemctl restart firewalld

# 开机自启
sudo systemctl enable firewalld
sudo systemctl disable firewalld

# 查看默认区域
sudo firewall-cmd --get-default-zone

# 查看所有区域
sudo firewall-cmd --get-zones

# 查看当前区域的规则
sudo firewall-cmd --list-all

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

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

# 移除端口/服务
sudo firewall-cmd --permanent --remove-port=3306/tcp

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

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

# 查看开放的服务
sudo firewall-cmd --list-services

UFW 防火墙管理(Ubuntu/Debian)

bash
# 安装 UFW
sudo apt install ufw

# 查看状态
sudo ufw status
sudo ufw status verbose

# 启用/禁用
sudo ufw enable
sudo ufw disable

# 默认策略
sudo ufw default deny incoming
sudo ufw default allow outgoing

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

# 允许服务
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https

# 允许特定 IP
sudo ufw allow from 192.168.1.100
sudo ufw allow from 192.168.1.0/24

# 拒绝访问
sudo ufw deny 3306/tcp

# 删除规则
sudo ufw delete allow 80/tcp
sudo ufw delete allow ssh

# 重置防火墙
sudo ufw reset

iptables 防火墙(传统方式)

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

# 允许 SSH
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 允许 HTTP/HTTPS
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 允许已建立的连接
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 拒绝其他所有输入
sudo iptables -P INPUT DROP

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

# 保存规则(CentOS)
sudo service iptables save

网络诊断工具

ping - 测试连通性

bash
# 基本用法
ping google.com

# 指定次数
ping -c 4 google.com

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

# 快速 ping
ping -c 4 -i 0.2 google.com

# IPv6
ping6 google.com
ping -6 google.com

traceroute - 追踪路由

bash
# 安装
sudo apt install traceroute     # Debian/Ubuntu
sudo yum install traceroute     # CentOS/RHEL

# 使用
traceroute google.com
traceroute -n google.com  # 不解析域名

# 替代方案
tracepath google.com
mtr google.com  # 更强大的工具

nslookup/dig - DNS 查询

bash
# nslookup
nslookup google.com
nslookup -type=MX google.com

# dig(更强大)
dig google.com
dig google.com A
dig google.com MX
dig google.com NS
dig +short google.com

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

netstat/ss - 网络连接查看

bash
# netstat(旧命令)
netstat -tuln          # 查看监听端口
netstat -an            # 查看所有连接
netstat -s             # 查看统计信息

# ss(新命令,推荐)
ss -tuln               # 查看监听端口
ss -tunap              # 查看所有连接及进程
ss -s                  # 查看摘要统计

# 查看特定端口
ss -tlnp | grep :80
lsof -i :80

curl/wget - HTTP 测试

bash
# curl
curl -I https://example.com       # 查看响应头
curl -v https://example.com       # 详细输出
curl -o file.html https://example.com  # 下载文件

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

网络性能测试

iperf3 - 网络带宽测试

bash
# 安装
sudo apt install iperf3

# 服务器端
iperf3 -s

# 客户端
iperf3 -c server_ip
iperf3 -c server_ip -t 30  # 测试 30 秒
iperf3 -c server_ip -R     # 反向测试(下载)

speedtest-cli - 网速测试

bash
# 安装
pip install speedtest-cli

# 使用
speedtest-cli
speedtest-cli --simple

网络安全最佳实践

1. 禁用不必要的服务

bash
# 查看监听端口
sudo ss -tlnp

# 禁用不需要的服务
sudo systemctl disable telnet
sudo systemctl disable ftp

2. 更改 SSH 默认端口

bash
# 编辑 SSH 配置
sudo vim /etc/ssh/sshd_config

# 修改端口
Port 2222

# 重启 SSH
sudo systemctl restart sshd

# 更新防火墙
sudo ufw allow 2222/tcp
sudo ufw delete allow 22/tcp

3. 配置 Fail2Ban

bash
# 安装
sudo apt install fail2ban

# 配置
sudo vim /etc/fail2ban/jail.local

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600

# 启动
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

4. 网络监控

bash
# 实时监控
iftop           # 类似 top 的网络流量监控
nethogs         # 按进程查看带宽
bmon            # 带宽监控
vnstat          # 长期流量统计

# 安装
sudo apt install iftop nethogs bmon vnstat

常见问题排查

问题 1:无法连接网络

bash
# 诊断步骤
ping 127.0.0.1              # 测试本地网络栈
ping 192.168.1.1            # 测试网关
ping 8.8.8.8                # 测试外网
ping google.com             # 测试 DNS

# 检查网络接口
ip addr show
ip route show

# 检查 DNS
cat /etc/resolv.conf
nslookup google.com

# 重启网络
sudo systemctl restart NetworkManager

问题 2:DNS 解析失败

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

# 修改 DNS
sudo vim /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

# 或使用 systemd-resolved
sudo systemd-resolve --set-dns=8.8.8.8 --interface=eth0

问题 3:端口被占用

bash
# 查找占用端口的进程
sudo lsof -i :80
sudo ss -tlnp | grep :80
sudo netstat -tlnp | grep :80

# 终止进程
sudo kill -9 PID

问题 4:防火墙阻止连接

bash
# 检查防火墙状态
sudo ufw status
sudo firewall-cmd --list-all

# 临时关闭防火墙测试
sudo ufw disable
sudo systemctl stop firewalld

# 添加规则
sudo ufw allow 8080/tcp
sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload

总结

Linux 网络管理涉及多个方面:

  1. IP 配置:静态/动态 IP、路由、DNS
  2. 防火墙:firewalld、UFW、iptables
  3. 服务管理:systemd、NetworkManager
  4. 网络诊断:ping、traceroute、dig、ss
  5. 安全加固:端口管理、Fail2Ban、监控

关键命令速查:

bash
ip addr show              # 查看 IP
ip route show             # 查看路由
sudo ufw allow 80/tcp     # 开放端口
sudo systemctl restart NetworkManager  # 重启网络
ping -c 4 google.com      # 测试连通性
ss -tlnp                  # 查看监听端口

下一步学习:

掌握网络管理,让你的 Linux 系统畅通无阻!🌐