当前位置:首页>Linux>Linux网络配置与故障排查(详细)

Linux网络配置与故障排查(详细)

  • 2026-09-02 20:00:35
Linux网络配置与故障排查(详细)

Linux网络配置与故障排查

      在Linux系统管理中,你是否曾经遇到过网络连接失败、无法访问特定服务、或者网络性能低下的问题?      网络配置和故障排查是Linux系统管理的重要技能,掌握这些技能不仅能保证网络稳定运行,还能快速解决网络问题。      本文将详细介绍Linux网络配置的核心命令和故障排查技巧,帮助你成为网络管理专家。

1. 网络配置工具

1.1 基础网络配置

ifconfig命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 查看网络接口ifconfig# 查看特定接口ifconfig eth0# 启用网络接口ifconfig eth0 up# 禁用网络接口ifconfig eth0 down# 配置IP地址ifconfig eth0 192.168.1.100 netmask 255.255.255.0

ip命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 查看网络接口ip addr show# 查看路由表ip route show# 配置IP地址ip addr add 192.168.1.100/24 dev eth0# 启用网络接口ip link set eth0 up# 禁用网络接口ip link set eth0 down

1.2 高级网络配置

NetworkManager管理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 查看NetworkManager状态nmcli status# 查看网络连接nmcli connection show# 添加网络连接nmcli connection add type ethernet ifname eth0 con-name eth0# 配置静态IPnmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.dns 8.8.8.8# 启用网络连接nmcli connection up eth0

netplan配置:

1
2
3
4
5
6
7
8
9
10
11

# /etc/netplan/01-netcfg.yamlnetwork:  version: 2  renderer: networkd  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]

1
2
3
4
5

# 应用netplan配置sudo netplan apply# 查看配置状态sudo netplan --debug apply

2. 网络服务配置

2.1 SSH服务配置

SSH服务管理:

1
2
3
4
5
6
7
8
9
10
11

# 启动SSH服务sudo systemctl start ssh# 停止SSH服务sudo systemctl stop ssh# 重启SSH服务sudo systemctl restart ssh# 启用SSH服务sudo systemctl enable ssh

SSH配置文件:

1
2
3
4
5
6
7
8
9

# 编辑SSH配置文件sudo nano /etc/ssh/sshd_config# 常用配置项Port 22PermitRootLogin noPasswordAuthentication noPubkeyAuthentication yesAllowUsers user1 user2

SSH密钥管理:

1
2
3
4
5
6
7
8

# 生成SSH密钥ssh-keygen -t rsa -b 4096# 复制公钥到远程主机ssh-copy-id user@remote_host# 手动添加公钥cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

2.2 网络服务配置

Nginx服务配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 安装Nginxsudo apt install nginx# 启动Nginx服务sudo systemctl start nginx# 配置Nginxsudo nano /etc/nginx/nginx.conf# 测试配置sudo nginx -t# 重载配置sudo systemctl reload nginx

Apache服务配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 安装Apachesudo apt install apache2# 启动Apache服务sudo systemctl start apache2# 配置Apachesudo nano /etc/apache2/apache2.conf# 启用模块sudo a2enmod rewrite# 重启Apachesudo systemctl restart apache2

3. 网络故障排查

3.1 基础网络测试

ping命令:

1
2
3
4
5
6
7
8
9
10
11

# 测试网络连通性ping 8.8.8.8# 测试特定次数ping -c 4 8.8.8.8# 设置超时时间ping -W 2 8.8.8.8# 快速模式ping -f 8.8.8.8

traceroute命令:

1
2
3
4
5
6
7
8
9
10
11

# 路由跟踪traceroute 8.8.8.8# 使用ICMPtraceroute -I 8.8.8.8# 使用TCPtraceroute -T 8.8.8.8# 设置超时时间traceroute -w 2 8.8.8.8

netstat命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 查看网络连接netstat -an# 查看监听端口netstat -ln# 查看TCP连接netstat -t# 查看UDP连接netstat -u# 查看进程IDnetstat -p

3.2 高级网络排查

tcpdump命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 捕获网络数据包tcpdump -i eth0# 捕获特定主机tcpdump -i eth0 host 192.168.1.1# 捕获特定端口tcpdump -i eth0 port 80# 保存到文件tcpdump -i eth0 -w capture.pcap# 从文件读取tcpdump -r capture.pcap

ss命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 查看网络连接ss -tulpn# 查看TCP连接ss -t# 查看UDP连接ss -u# 查看监听端口ss -l# 查看进程IDss -p

nmap命令:

1
2
3
4
5
6
7
8
9
10
11

# 扫描主机nmap 192.168.1.1# 扫描端口nmap -p 1-1000 192.168.1.1# 扫描服务nmap -sV 192.168.1.1# 扫描操作系统nmap -O 192.168.1.1

4. 网络安全配置

4.1 防火墙配置

ufw防火墙:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 启用防火墙sudo ufw enable# 禁用防火墙sudo ufw disable# 查看防火墙状态sudo ufw status# 添加规则sudo ufw allow 22/tcp# 删除规则sudo ufw delete allow 22/tcp# 拒绝规则sudo ufw deny 80/tcp

iptables防火墙:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 查看规则sudo iptables -L# 添加规则sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT# 删除规则sudo iptables -D INPUT -p tcp --dport 22 -j ACCEPT# 保存规则sudo iptables-save > /etc/iptables/rules.v4# 加载规则sudo iptables-restore < /etc/iptables/rules.v4

4.2 SSH安全加固

SSH安全配置:

1
2
3
4
5
6
7
8
9
10
11

# 禁用root登录sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config# 禁用密码登录sudo sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config# 修改SSH端口sudo sed -i 's/Port 22/Port 2222/' /etc/ssh/sshd_config# 重启SSH服务sudo systemctl restart ssh

SSH访问控制:

1
2
3
4
5
6
7
8

# 使用hosts.allowecho "sshd: 192.168.1.0/24" | sudo tee -a /etc/hosts.allow# 使用hosts.denyecho "sshd: ALL" | sudo tee -a /etc/hosts.deny# 使用TCP Wrapperssudo apt install tcpd

5. 网络监控工具

5.1 网络监控命令

iftop命令:

1
2
3
4
5
6
7
8
9
10
11

# 监控网络流量iftop# 按端口排序iftop -P# 显示主机名iftop -n# 显示端口iftop -N

nethogs命令:

1
2
3
4
5
6
7
8

# 监控进程网络使用nethogs# 按设备监控nethogs eth0# 持续监控nethogs -d 5

vnstat命令:

1
2
3
4
5
6
7
8
9
10
11

# 查看网络统计vnstat# 查看详细统计vnstat -d# 查看月度统计vnstat -m# 查看小时统计vnstat -h

5.2 系统网络监控

netstat监控:

1
2
3
4
5
6
7
8

# 监控网络连接watch netstat -an# 监控特定端口watch netstat -ln | grep 80# 监控连接数netstat -an | grep ESTABLISHED | wc -l

ss监控:

1
2
3
4
5
6
7
8

# 监控网络连接watch ss -tulpn# 监控特定端口watch ss -ln | grep 80# 监控连接数ss -t | grep ESTAB | wc -l

6. 网络性能优化

6.1 网络参数调优

TCP参数调优:

1
2
3
4
5
6
7
8
9
10
11
12
13

# 调整TCP窗口大小echo "net.core.rmem_max = 16777216" | sudo tee -a /etc/sysctl.confecho "net.core.wmem_max = 16777216" | sudo tee -a /etc/sysctl.conf# 调整TCP缓冲区echo "net.ipv4.tcp_rmem = 4096 87380 16777216" | sudo tee -a /etc/sysctl.confecho "net.ipv4.tcp_wmem = 4096 65536 16777216" | sudo tee -a /etc/sysctl.conf# 调整TCP连接数echo "net.core.somaxconn = 65535" | sudo tee -a /etc/sysctl.conf# 应用配置sudo sysctl -p

网络接口调优:

1
2
3
4
5
6
7
8

# 调整MTUsudo ifconfig eth0 mtu 9000# 调整网卡队列长度sudo ethtool -G eth0 rx 4096 tx 4096# 启用网卡多队列sudo ethtool -L eth0 combined 8

6.2 网络服务优化

Nginx优化:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 调整worker进程数worker_processes auto;# 调整连接数worker_connections 4096;# 启用keepalivekeepalive_timeout 65;keepalive_requests 1000;# 启用gzipgzip on;gzip_comp_level 6;gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

Apache优化:

1
2
3
4
5
6
7
8
9

# 调整worker进程数StartServers 5MinSpareServers 5MaxSpareServers 10MaxRequestWorkers 150MaxConnectionsPerChild 1000# 启用压缩LoadModule deflate_module modules/mod_deflate.so

7. 网络连接故障处理

7.1 常见网络故障

网络连接失败:

1
2
3
4
5
6
7
8
9
10
11

# 检查网络接口ip addr show# 检查路由表ip route show# 检查DNS解析nslookup example.com# 检查防火墙sudo ufw status

服务无法访问:

1
2
3
4
5
6
7
8
9
10
11

# 检查服务状systemctl status nginx# 检查端口监听netstat -ln | grep 80# 检查防火墙规则sudo ufw status# 检查SELinux状态sestatus

8. 网络服务故障排查

8.1 Nginx故障排查

常见问题:

1
2
3
4
5
6
7
8
9
10
11

# 检查Nginx配置sudo nginx -t# 查看Nginx日志sudo tail -f /var/log/nginx/error.log# 检查端口占用sudo netstat -ln | grep 80# 检查权限问题sudo ls -la /var/log/nginx/

解决方案:

1
2
3
4
5
6
7
8

# 修改配置文件权限sudo chown -R www-data:www-data /var/www/html/# 修改日志文件权限sudo chmod 755 /var/log/nginx/# 重启Nginx服务sudo systemctl restart nginx

8.2 MySQL故障排查

常见问题:

1
2
3
4
5
6
7
8
9
10
11

# 检查MySQL服务状态systemctl status mysql# 查看MySQL日志sudo tail -f /var/log/mysql/error.log# 检查端口占用sudo netstat -ln | grep 3306# 检查权限问题sudo ls -la /var/lib/mysql/

解决方案:

1
2
3
4
5
6
7
8

# 修改配置文件权限sudo chown -R mysql:mysql /etc/mysql/# 修改数据目录权限sudo chmod 755 /var/lib/mysql/# 重启MySQL服务sudo systemctl restart mysql

总结

      Linux网络配置与故障排查是系统管理的重要技能,掌握这些技能不仅能保证网络稳定运行,还能快速解决网络问题。

关键要点回顾:

  1. 1. 网络配置:掌握ifconfig、ip、NetworkManager、netplan等配置工具
  2. 2. 服务配置:熟练配置SSH、Nginx、Apache等网络服务
  3. 3. 故障排查:掌握ping、traceroute、netstat、tcpdump等排查工具
  4. 4. 安全配置:了解防火墙配置和SSH安全加固
  5. 5. 性能优化:掌握网络参数调优和服务优化技巧

最新文章

随机文章