本文整理 Linux 网络诊断、远程连接和软件管理的常用命令,是服务器运维的必备工具集。
一、网络诊断
ping — 网络连通性测试
ping 8.8.8.8 # 持续 ping
ping -c 5 8.8.8.8 # 发送 5 个包后停止
ping -i 0.5 baidu.com # 每 0.5 秒发一个包典型输出解读:
64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=12.3 mstime越小越好。局域网通常 < 1ms,外网通常 10-50ms
curl — HTTP 请求工具
curl https://api.example.com # GET 请求
curl -O https://example.com/file.zip # 下载文件
curl -o output.txt https://example.com # 指定输出文件名
curl -I https://example.com # 只获取响应头
curl -v https://example.com # 详细输出(调试用)
curl -X POST -d '{"key":"value"}' -H 'Content-Type: application/json' \
https://api.example.com/endpoint # POST JSON 数据实用示例:
# 检测 API 响应时间
curl -w "HTTP %{http_code}, Time: %{time_total}s\n" -o /dev/null -s \
https://example.com
# 查看请求全程(DNS解析、TCP连接、TLS握手等)
curl -w "\n---\nDNS: %{time_namelookup}s\nTCP: %{time_connect}s\nTLS: %{time_appconnect}s\nTotal: %{time_total}s\n" \
-o /dev/null -s https://example.comwget — 文件下载
wget https://example.com/file.zip # 下载文件
wget -O output.zip https://example.com/file # 指定输出文件名
wget -c https://example.com/bigfile.zip # 断点续传
wget -P /download/path/ https://example.com # 指定下载目录
wget -r -np https://example.com/dir/ # 递归下载目录对比: curl 更灵活(支持各种协议和方法),wget 更擅长下载(递归、断点续传)。
telnet — 端口连通性测试
telnet 192.168.1.1 22 # 测试 22 端口是否开放
telnet smtp.example.com 25 # 测试 SMTP 端口💡 连接成功会进入空屏或显示服务 banner,按
Ctrl+]再q退出。
某些系统需安装:apt install telnet/yum install telnet
nc — 网络瑞士军刀
nc -zv host 80 # 端口扫描
nc -l -p 9999 # 监听端口(测试用)
echo "test" | nc host 9999 # 发送数据实用示例: 测试端口连通性
nc -zv -w 3 192.168.1.100 22 80 443 # 测试多个端口,超时 3 秒ss — 套接字统计(推荐)
ss -tlnp # 查看所有 TCP 监听端口
ss -ulnp # 查看所有 UDP 监听端口
ss -tunap # 查看所有连接(TCP/UDP)
ss -s # 连接统计概览
ss -tlnp | grep :80 # 查找占用 80 端口的进程💡
ss比旧的netstat更快,信息更全,现代 Linux 推荐使用ss。
netstat — 网络状态查看(旧工具)
netstat -tlnp # TCP 监听端口
netstat -ulnp # UDP 监听端口
netstat -rn # 路由表
netstat -i # 网络接口统计ip — IP 配置管理
ip addr # 查看 IP 地址
ip link set eth0 up/down # 启用/禁用接口
ip route # 查看路由表
ip neigh # 查看 ARP 表💡
ip是ifconfig的现代替代品,推荐使用。
traceroute — 路由追踪
traceroute baidu.com # 追踪到目标的每一跳
traceroute -n baidu.com # 不解析主机名
mtr baidu.com # 增强版(实时动态追踪)💡
mtr结合了ping和traceroute的功能,实时显示各节点的丢包率和延迟。
nslookup / dig — DNS 查询
nslookup example.com # DNS 域名解析
dig example.com # 详细 DNS 查询
dig example.com A # 查询 A 记录
dig example.com MX # 查询 MX 记录
dig +short example.com # 精简输出
dig @8.8.8.8 example.com # 指定 DNS 服务器💡
dig比nslookup功能更强,信息更完整,建议优先使用。
hostname — 查看/设置主机名
hostname # 查看主机名
hostname newname # 临时修改
hostnamectl set-hostname newname # 永久修改(systemd 系统)二、远程连接
ssh — 远程登录
ssh user@host # 远程登录
ssh -p 2222 user@host # 指定端口
ssh -i ~/.ssh/id_rsa user@host # 指定密钥文件
ssh -J jumpuser@jump_host user@target # 跳板机(Jump Host)SSH 配置技巧(~/.ssh/config):
Host myserver
HostName 192.168.1.100
User root
Port 22
IdentityFile ~/.ssh/mykey以后只需 ssh myserver 即可连接。
scp — 安全复制
scp file.txt user@host:/path/ # 本地到远程
scp user@host:/path/file.txt ./ # 远程到本地
scp -r dir/ user@host:/path/ # 递归复制目录
scp -P 2222 file.txt user@host:/path/ # 指定端口
scp -C file.txt user@host:/path/ # 启用压缩传输rsync — 高性能同步
rsync -av source/ dest/ # 本地同步
rsync -avz dir/ user@host:/path/ # 远程同步(压缩传输)
rsync -av --delete source/ dest/ # 删除目标端多余文件
rsync -av --progress bigfile user@host:/path/ # 显示进度
rsync -av -e "ssh -p 2222" dir/ user@host:/path/ # 指定 SSH 端口💡
rsync比scp更强大:增量同步、断点续传、权限保留、压缩传输。
ssh-keygen — SSH 密钥管理
ssh-keygen -t ed25519 # 生成 ED25519 密钥(推荐)
ssh-keygen -t rsa -b 4096 # 生成 RSA 4096 密钥
ssh-keygen -p # 修改密钥密码
ssh-copy-id user@host # 将公钥复制到远程主机💡 ED25519 比 RSA 更安全、更快速,是当前推荐算法。生成的公钥在
~/.ssh/id_ed25519.pub。
screen / tmux — 终端复用器
# screen
screen -S session_name # 创建会话
screen -ls # 列出会话
screen -r session_name # 重新连接
Ctrl+A + D # 分离会话
# tmux
tmux new -s session_name # 创建会话
tmux ls # 列出会话
tmux attach -t session_name # 重新连接
tmux new -s work -d # 后台创建会话💡 远程服务器上跑长时间任务一定要用
tmux或screen,否则 SSH 断开后任务会终止。
三、软件管理
Debian/Ubuntu (apt)
apt update # 更新软件包索引
apt upgrade # 升级所有可升级包
apt install nginx # 安装软件
apt remove nginx # 卸载(保留配置文件)
apt purge nginx # 完全卸载(含配置文件)
apt autoremove # 自动清理无用的依赖
apt search keyword # 搜索软件包
apt show nginx # 查看包信息
apt list --installed # 列出已安装包
apt list --upgradable # 列出可升级包CentOS/RHEL (yum/dnf)
# yum(CentOS 7 及之前)
yum install nginx # 安装
yum remove nginx # 卸载
yum update # 升级
yum search keyword # 搜索
yum list installed # 查看已安装
yum info nginx # 查看信息
# dnf(CentOS 8+ / Fedora,比 yum 更快)
dnf install nginx
dnf remove nginx
dnf updatedpkg — Debian 包管理器
dpkg -i package.deb # 安装 .deb 文件
dpkg -r package # 卸载
dpkg -l # 列出已安装
dpkg -L package # 查看包安装的文件
dpkg -S /path/to/file # 查看文件属于哪个包rpm — Red Hat 包管理器
rpm -ivh package.rpm # 安装
rpm -e package # 卸载
rpm -qa # 列出所有已安装
rpm -ql package # 查看包安装的文件
rpm -qf /path/to/file # 查看文件属于哪个包四、进程与服务实战
Nginx 服务管理
# 安装
apt install nginx # Debian/Ubuntu
yum install nginx # CentOS/RHEL
# 管理
systemctl start nginx # 启动
systemctl stop nginx # 停止
systemctl restart nginx # 重启
systemctl reload nginx # 重载配置(推荐,不中断连接)
systemctl enable nginx # 开机自启
systemctl disable nginx # 禁用开机自启
# 检查
nginx -t # 测试配置语法
nginx -s reload # 重载配置(无 systemctl 时)
systemctl status nginx # 查看状态
journalctl -u nginx -f # 实时查看日志Docker 容器管理
# 安装 Docker
curl -fsSL https://get.docker.com | sh
# 基本命令
docker ps # 查看运行中的容器
docker ps -a # 查看所有容器
docker images # 查看镜像
docker pull nginx # 拉取镜像
docker run -d -p 80:80 nginx # 运行容器
docker stop container_id # 停止容器
docker start container_id # 启动容器
docker rm container_id # 删除容器
docker rmi image_id # 删除镜像
docker logs -f container_id # 查看容器日志
docker exec -it container_id bash # 进入容器日志管理
# 日志轮转配置(/etc/logrotate.d/nginx)
/var/log/nginx/*.log {
daily # 每天轮转
rotate 7 # 保留 7 份
compress # 压缩旧日志
delaycompress
missingok
notifempty
create 640 www-data adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}五、综合排查实战
服务无法访问排查流程
# 1. 服务是否运行
systemctl status nginx
# 2. 端口是否监听
ss -tlnp | grep 80
# 3. 防火墙是否放行
iptables -L -n | grep 80
ufw status
# 4. 本地连通性
curl -I http://localhost:80
# 5. 外部连通性
curl -I http://your-domain.com
# 6. DNS 是否正常
dig your-domain.com
# 7. 路由是否可达
traceroute your-domain.com带宽与连接排查
# 网络吞吐量测试(服务端需装 iperf3)
iperf3 -s # 服务端
iperf3 -c server_ip # 客户端
# 查看连接数
ss -s
netstat -ant | grep :80 | wc -l
# 查看各 IP 连接数
netstat -ant | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head
# 实时带宽监控
iftop # 需安装:apt install iftop
nethogs # 按进程查看带宽(需安装)小结: 网络与软件管理命令是服务器运维的核心技能。遇到网络问题时,遵循「本地 → 端口 → 防火墙 → DNS → 路由」的排查顺序可以快速定位问题。建议在本地搭建虚拟机实际操作练习,熟能生巧。
系列回顾: