Linux 网络管理
网络命令
netstat - 显示网络连接状态
命令参数解释表格:
| 参数 | 说明 | 示例 |
|---|---|---|
-t | 显示 TCP 连接 | netstat -t |
-u | 显示 UDP 连接 | netstat -u |
-l | 显示监听端口 | netstat -l |
-n | 显示数字地址 | netstat -n |
-p | 显示进程信息 | netstat -p |
-a | 显示所有连接 | netstat -a |
输出参数解释表格:
| 输出字段 | 说明 | 示例 |
|---|---|---|
| Proto | 协议类型 | tcp |
| Recv-Q | 接收队列 | 0 |
| Send-Q | 发送队列 | 0 |
| Local Address | 本地地址 | 127.0.0.1:8080 |
| Foreign Address | 远程地址 | 0.0.0.0:* |
| State | 连接状态 | LISTEN |
| PID/Program name | 进程 ID/程序名 | 1234/nginx |
指令输出示例:
$ netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 1234/nginx
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 5678/sshd
tcp6 0 0 :::80 :::* LISTEN 9012/apache2
ping - 测试网络连通性
命令参数解释表格:
| 参数 | 说明 | 示例 |
|---|---|---|
-c | 指定发送次数 | ping -c 4 8.8.8.8 |
-i | 指定发送间隔(秒) | ping -i 2 8.8.8.8 |
-s | 指定数据包大小 | ping -s 1000 8.8.8.8 |
-t | 设置 TTL 值 | ping -t 64 8.8.8.8 |
-W | 设置超时时间 | ping -W 5 8.8.8.8 |
输出参数解释表格:
| 输出字段 | 说明 | 示例 |
|---|---|---|
| icmp_seq | ICMP 序列号 | 1 |
| ttl | 生存时间 | 64 |
| time | 响应时间 | 2.5 ms |
| bytes | 数据包大小 | 64 |
指令输出示例:
$ ping -c 4 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=64 time=2.5 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=64 time=2.3 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=64 time=2.4 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=64 time=2.6 ms
--- 8.8.8.8 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 2.300/2.450/2.600/0.100 ms
软件包管理命令
apt - Debian/Ubuntu 包管理
命令参数解释表格:
| 参数 | 说明 | 示例 |
|---|---|---|
update | 更新包列表 | apt update |
upgrade | 升级包 | apt upgrade |
install | 安装包 | apt install nginx |
remove | 删除包 | apt remove nginx |
purge | 完全删除包 | apt purge nginx |
search | 搜索包 | apt search nginx |
show | 显示包信息 | apt show nginx |
list | 列出包 | apt list --installed |
指令输出示例:
$ apt update
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Fetched 379 kB in 2s (189 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
$ apt install nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
nginx-common nginx-core
The following NEW packages will be installed:
nginx nginx-common nginx-core
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,234 kB of archives.
After this operation, 3,456 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
yum - CentOS/RHEL 包管理
命令参数解释表格:
| 参数 | 说明 | 示例 |
|---|---|---|
install | 安装包 | yum install nginx |
remove | 删除包 | yum remove nginx |
update | 更新包 | yum update |
search | 搜索包 | yum search nginx |
info | 显示包信息 | yum info nginx |
list | 列出包 | yum list installed |
clean | 清理缓存 | yum clean all |
指令输出示例:
$ yum install nginx
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.16.1-1.el7 will be installed
--> Finished dependency resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
nginx x86_64 1:1.16.1-1.el7 epel 1.5 M
Transaction Summary
================================================================================
Install 1 Package
Total download size: 1.5 M
Installed size: 5.2 M
Is this ok [y/d/N]: y
远程连接命令
ssh - 安全远程连接
命令参数解释表格:
| 参数 | 说明 | 示例 |
|---|---|---|
-p | 指定端口号 | ssh -p 2222 user@host |
-i | 指定私钥文件 | ssh -i ~/.ssh/id_rsa user@host |
-X | 启用 X11 转发 | ssh -X user@host |
-L | 本地端口转发 | ssh -L 8080:localhost:80 user@host |
-D | 动态端口转发 | ssh -D 1080 user@host |
-t | 强制分配伪终端 | ssh -t user@host |
-v | 详细模式 | ssh -v user@host |
指令输出示例:
$ ssh user@192.168.1.100
user@192.168.1.100's password:
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 5.4.0-74-generic x86_64)
$ ssh -p 2222 -i ~/.ssh/id_rsa user@host
# 使用指定端口和私钥连接
$ ssh -L 8080:localhost:80 user@host
# 本地8080端口转发到远程80端口
scp - 安全文件传输
命令参数解释表格:
| 参数 | 说明 | 示例 |
|---|---|---|
-r | 递归复制目录 | scp -r dir/ user@host:/path/ |
-P | 指定端口号 | scp -P 2222 file user@host:/path/ |
-i | 指定私钥文件 | scp -i ~/.ssh/id_rsa file user@host:/path/ |
-v | 详细模式 | scp -v file user@host:/path/ |
-C | 启用压缩 | scp -C file user@host:/path/ |
-p | 保留文件属性 | scp -p file user@host:/path/ |
指令输出示例:
$ scp file.txt user@192.168.1.100:/home/user/
user@192.168.1.100's password:
file.txt 100% 25 0.0KB/s 00:00
$ scp -r documents/ user@192.168.1.100:/home/user/
user@192.168.1.100's password:
file1.txt 100% 25 0.0KB/s 00:00
file2.txt 100% 30 0.0KB/s 00:00
rsync - 远程同步
命令参数解释表格:
| 参数 | 说明 | 示例 |
|---|---|---|
-a | 归档模式 | rsync -a source/ dest/ |
-v | 详细模式 | rsync -v file dest/ |
-z | 压缩传输 | rsync -z file dest/ |
-r | 递归复制 | rsync -r dir/ dest/ |
-P | 显示进度 | rsync -P file dest/ |
-n | 试运行 | rsync -n -a source/ dest/ |
--delete | 删除目标多余文件 | rsync -a --delete source/ dest/ |
-e | 指定远程 shell | rsync -e "ssh -p 2222" file user@host:/path/ |
指令输出示例:
$ rsync -avz /local/dir/ user@host:/remote/dir/
sending incremental file list
./
file1.txt
file2.txt
sent 1234 bytes received 567 bytes 3602.00 bytes/sec
total size is 8901 speedup is 4.94
$ rsync -avz --delete /backup/ user@host:/backup/
sending incremental file list
deleting old_file.txt
./
new_file.txt
sent 2345 bytes received 678 bytes 6046.00 bytes/sec
total size is 12345 speedup is 2.04
文件传输命令
wget - 下载文件
命令参数解释表格:
| 参数 | 说明 | 示例 |
|---|---|---|
-c | 断点续传 | wget -c http://example.com/file |
-O | 指定输出文件名 | wget -O filename http://example.com/file |
-P | 指定下载目录 | wget -P /downloads/ http://example.com/file |
-r | 递归下载 | wget -r http://example.com/ |
-np | 不跟随父目录链接 | wget -r -np http://example.com/ |
-q | 静默模式 | wget -q http://example.com/file |
-b | 后台下载 | wget -b http://example.com/file |
--limit-rate | 限制下载速度 | wget --limit-rate=1m http://example.com/file |
指令输出示例:
$ wget http://example.com/file.txt
--2023-12-15 14:30:00-- http://example.com/file.txt
Resolving example.com... 93.184.216.34
Connecting to example.com|93.184.216.34|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1234 (1.2K) [text/plain]
Saving to: 'file.txt'
100%[======================================>] 1,234 --.-K/s in 0.1s
2023-12-15 14:30:00 (12.3 KB/s) - 'file.txt' saved [1234/1234]
$ wget -c -O backup.tar.gz http://example.com/backup.tar.gz
# 断点续传并指定输出文件名
curl - 传输数据
命令参数解释表格:
| 参数 | 说明 | 示例 |
|---|---|---|
-o | 保存到文件 | curl -o file http://example.com/file |
-O | 使用远程文件名 | curl -O http://example.com/file.txt |
-L | 跟随重定向 | curl -L http://example.com/redirect |
-H | 添加 HTTP 头 | curl -H "Authorization: Bearer token" http://api.com |
-d | 发送 POST 数据 | curl -d "name=value" http://api.com |
-X | 指定 HTTP 方法 | curl -X POST http://api.com |
-k | 忽略 SSL 证书 | curl -k https://example.com |
-s | 静默模式 | curl -s http://example.com |
-w | 自定义输出格式 | curl -w "%{http_code}" http://example.com |
指令输出示例:
$ curl -o file.txt http://example.com/file
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1234 100 1234 0 0 12340 0 --:--:-- --:--:-- --:--:-- 12340
$ curl -H "Content-Type: application/json" -d '{"key":"value"}' http://api.com
{"status":"success","message":"Data received"}
$ curl -w "HTTP Status: %{http_code}\nTime: %{time_total}s\n" http://example.com
HTTP Status: 200
Time: 0.123s
系统监控命令
htop - 交互式进程查看器
命令参数解释表格:
| 参数 | 说明 | 示例 |
|---|---|---|
-p | 指定进程 ID | htop -p 1234 |
-u | 指定用户名 | htop -u username |
-d | 设置刷新间隔 | htop -d 5 |
-C | 隐藏 CPU 使用率 | htop -C |
-t | 显示树状结构 | htop -t |
htop 界面说明:
| 按键 | 功能 |
|---|---|
F1 | 显示帮助 |
F2 | 设置 |
F3 | 搜索进程 |
F4 | 过滤进程 |
F5 | 树状显示 |
F6 | 排序 |
F9 | 杀死进程 |
F10 | 退出 |
指令输出示例:
$ htop
# 显示交互式进程管理器界面
# 显示CPU、内存使用情况,可交互操作
iotop - 监控磁盘 I/O
命令参数解释表格:
| 参数 | 说明 | 示例 |
|---|---|---|
-o | 只显示有 I/O 的进程 | iotop -o |
-b | 批处理模式 | iotop -b |
-n | 指定刷新次数 | iotop -n 10 |
-d | 设置刷新间隔 | iotop -d 2 |
-p | 指定进程 ID | iotop -p 1234 |
指令输出示例:
$ iotop
Total DISK READ: 0.00 B/s | Total DISK WRITE: 0.00 B/s
Current DISK READ: 0.00 B/s | Current DISK WRITE: 0.00 B/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND
1234 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % systemd
1235 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % kthreadd
nethogs - 监控网络使用
命令参数解释表格:
| 参数 | 说明 | 示例 |
|---|---|---|
-t | 跟踪模式 | nethogs -t |
-d | 设置刷新间隔 | nethogs -d 5 |
-v | 详细模式 | nethogs -v |
-c | 指定刷新次数 | nethogs -c 10 |
指令输出示例:
$ nethogs
# 显示网络使用情况
# 按进程显示网络流量
