curl:HTTP 请求瑞士军刀
基本用法
# GET 请求(默认)
curl https://api.example.com/users
curl -s https://api.example.com/data # -s 静默模式(不显示进度)
curl -v https://example.com # -v 详细输出(请求头/响应头)
curl -I https://example.com # -I 只获取响应头(HEAD 请求)
# POST 请求
curl -X POST https://api.example.com/login \
-H "Content-Type: application/json" \
-d '{"username":"alice","password":"secret"}'
# 发送表单数据
curl -X POST https://example.com/form \
-d "name=Alice&email=alice@example.com"
# 发送文件
curl -X POST https://api.example.com/upload \
-F "file=@/path/to/file.pdf" \
-F "description=My file"
# 设置请求头
curl -H "Authorization: Bearer your-token" \
-H "Accept: application/json" \
https://api.example.com/protected
# 下载文件
curl -O https://example.com/file.tar.gz # 保存为原文件名
curl -o myfile.tar.gz https://example.com/file.tar.gz # 自定义文件名
curl -L https://example.com/redirect -o output.html # -L 跟随重定向
curl -C - -O https://example.com/bigfile.iso # -C - 断点续传
# 查看 HTTP 状态码
curl -o /dev/null -w "%{http_code}" https://example.com
curl -s -o /dev/null -w "%{http_code}\n%{time_total}s\n" https://example.com
# 使用代理
curl -x http://proxy:8080 https://example.com
curl --proxy-user user:pass -x http://proxy:8080 https://example.com
wget:专业下载工具
# 基本下载
wget https://example.com/file.tar.gz
wget -O output.tar.gz https://example.com/file.tar.gz # 自定义文件名
wget -q https://example.com/file.tar.gz # 静默模式
# 断点续传(大文件必用)
wget -c https://example.com/bigfile.iso
# 限速下载
wget --limit-rate=1m https://example.com/file.tar.gz # 限制为 1MB/s
# 递归下载网站
wget -r -np -l 2 https://docs.example.com/ # 递归2层,不跟父目录
wget --mirror -p --convert-links https://example.com # 完整镜像
# 从文件列表下载
cat urls.txt | wget -i - # -i 从文件读取 URL
wget -i urls.txt
# 设置用户代理和请求头
wget --user-agent="Mozilla/5.0" https://example.com
wget --header="Authorization: Bearer token" https://api.example.com/data
SSH:安全远程登录
基本连接
# 连接远程服务器
ssh user@hostname
ssh alice@192.168.1.100
ssh -p 2222 alice@example.com # -p 指定非标准端口
ssh -i ~/.ssh/mykey alice@example.com # -i 指定私钥文件
ssh -v alice@example.com # -v 调试连接问题
# 在远程执行命令(不进入交互式 shell)
ssh alice@server "df -h"
ssh alice@server "ls /var/log/*.log | head -10"
ssh alice@server "sudo systemctl restart nginx"
# ~/.ssh/config 配置(简化连接)
# ~/.ssh/config
Host myserver
HostName 192.168.1.100
User alice
Port 2222
IdentityFile ~/.ssh/mykey
ServerAliveInterval 60
ServerAliveCountMax 3
Host bastion
HostName jump.example.com
User admin
IdentityFile ~/.ssh/bastion_key
Host internal
HostName 10.0.0.50
User app
ProxyJump bastion # 通过 bastion 跳板机连接
# 配置后可以直接用别名连接
ssh myserver
ssh internal
SSH 密钥认证
# 生成 SSH 密钥对
ssh-keygen -t ed25519 -C "alice@example.com" # 推荐 ed25519 算法
ssh-keygen -t rsa -b 4096 -C "alice@example.com" # 或 RSA 4096
# 生成两个文件:~/.ssh/id_ed25519(私钥)和 ~/.ssh/id_ed25519.pub(公钥)
# 将公钥添加到远程服务器
ssh-copy-id alice@192.168.1.100 # 自动添加到 authorized_keys
ssh-copy-id -i ~/.ssh/mykey.pub alice@server # 指定公钥文件
# 手动添加(若没有 ssh-copy-id)
cat ~/.ssh/id_ed25519.pub | ssh alice@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
# 正确设置权限(否则 SSH 拒绝使用密钥)
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_ed25519 # 私钥
chmod 644 ~/.ssh/id_ed25519.pub # 公钥
SSH 端口转发(隧道)
# 本地转发:将本地端口转发到远程目标
# 访问 localhost:8080 → 通过 server → 连接 remote-db:5432
ssh -L 8080:remote-db:5432 alice@server
ssh -L 8080:localhost:80 alice@server # 访问 server 上的 localhost:80
ssh -fNL 8080:db:5432 alice@server # -f 后台,-N 不执行命令
# 远程转发:让远程服务器访问本地端口
# 在 server 上访问 server:9090 → 连接到本地的 localhost:3000
ssh -R 9090:localhost:3000 alice@server # 将本地开发服务器暴露给远程
# SOCKS5 代理(动态转发)
ssh -D 1080 alice@server -fN # 建立 SOCKS5 代理
# 然后配置浏览器使用 SOCKS5 代理 127.0.0.1:1080
scp 与 rsync:文件传输
# scp:基于 SSH 的安全复制
scp file.txt alice@server:/tmp/ # 上传到服务器
scp alice@server:/var/log/app.log ./ # 从服务器下载
scp -r ./mydir alice@server:/opt/ # -r 递归复制目录
scp -P 2222 file.txt alice@server:/tmp/ # -P 指定端口(大写P)
scp alice@server1:/file.txt bob@server2:/backup/ # 服务器间传输
# rsync:增量同步(推荐用于大量文件)
rsync -avz ./src/ alice@server:/opt/app/ # -a 归档 -v 详细 -z 压缩
rsync -avz --progress ./src/ alice@server:/opt/app/ # 显示进度
rsync -avz --delete ./src/ alice@server:/opt/app/ # --delete 删除目标中多余文件
rsync -avz --exclude='.git' --exclude='node_modules' ./src/ alice@server:/opt/ # 排除
# 本地同步(备份)
rsync -avz /home/alice/ /backup/alice/
rsync -avz --link-dest=/backup/alice-prev/ /home/alice/ /backup/alice/ # 增量备份
网络诊断工具
# ping:测试连通性
ping google.com # 持续 ping
ping -c 4 google.com # -c 指定次数
ping -i 0.5 -c 10 server # -i 间隔 0.5 秒
ping6 ipv6.google.com # IPv6
# traceroute/tracepath:追踪网络路径
traceroute google.com # 显示每一跳的路由器
tracepath google.com # 不需要 root,功能类似
mtr google.com # 实时动态路由追踪(综合了 ping 和 traceroute)
# dig/nslookup:DNS 查询
dig google.com # 查询 A 记录
dig google.com MX # 查询 MX 记录(邮件服务器)
dig google.com +short # 简短输出
dig @8.8.8.8 google.com # 使用指定 DNS 服务器查询
nslookup google.com # 简单 DNS 查询
host google.com # 简洁的 DNS 查询
# ss:查看网络连接(替代 netstat)
ss -tlnp # -t TCP -l 监听 -n 数字 -p 进程
ss -ulnp # UDP 监听端口
ss -tnp # 所有 TCP 连接
ss -tnp | grep :80 # 过滤 80 端口
ss -s # 汇总统计
# netstat(较旧,很多系统仍可用)
netstat -tlnp # 与 ss -tlnp 类似
# 查看网络接口
ip addr show # 显示所有接口和 IP 地址
ip addr show eth0 # 特定接口
ip link show # 接口状态(up/down)
ip route show # 路由表
ip route get 8.8.8.8 # 到达特定 IP 的路由
# 旧版工具(已废弃但常见)
ifconfig # 被 ip addr 替代
route -n # 被 ip route 替代
nc(netcat):网络调试利器
# 端口扫描
nc -zv server.com 80 # 测试 TCP 80 端口是否开放
nc -zv server.com 22 # SSH 端口
nc -zv server.com 80-443 # 扫描端口范围
nc -zvu server.com 53 # UDP 端口
# 监听端口(服务器模式)
nc -l 8888 # 在 8888 端口监听
# 在另一终端连接
nc server.com 8888 # 连接到服务器 8888 端口
# 文件传输(简单场景)
# 接收方
nc -l 9999 > received_file.tar.gz
# 发送方
nc receiver.ip 9999 < file.tar.gz
# 测试 HTTP 请求
echo -e "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n" | nc example.com 80
# 检查服务响应
echo "QUIT" | nc -w 3 mail.server.com 25 # 测试 SMTP
openssl s_client -connect example.com:443 # 测试 HTTPS/TLS
iptables:防火墙配置
iptables 操作需要谨慎
错误的 iptables 规则可能导致 SSH 连接断开,使服务器无法访问。建议操作前先设置一个定时任务来恢复规则:echo "iptables -F" | at now + 5 minutes
# 查看规则
sudo iptables -L -n -v # -L 列出 -n 数字显示 -v 详细
sudo iptables -L INPUT -n -v # 只查看 INPUT 链
sudo iptables -S # 以规则语法显示
# 基本规则
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT # 允许 SSH
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT # 允许 HTTP
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 允许 HTTPS
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT # 允许已建立连接
sudo iptables -A INPUT -i lo -j ACCEPT # 允许本地回环
sudo iptables -A INPUT -j DROP # 默认拒绝(慎用!确保 SSH 规则在此之前)
# 删除规则
sudo iptables -D INPUT -p tcp --dport 80 -j ACCEPT # 按规则内容删除
sudo iptables -D INPUT 3 # 按行号删除
sudo iptables -F # 清空所有规则(危险!)
# 保存规则(Ubuntu)
sudo apt install iptables-persistent
sudo netfilter-persistent save
# 现代替代方案:ufw(Uncomplicated Firewall)
sudo ufw status
sudo ufw allow ssh
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status numbered
sudo ufw delete 2 # 按编号删除规则
云服务器网络安全最小化原则
- 只开放必要端口(SSH 22、HTTP 80、HTTPS 443)
- 将 SSH 端口改为非标准端口(如 2222),减少扫描攻击
- 禁用 SSH 密码认证(
/etc/ssh/sshd_config设置PasswordAuthentication no),只允许密钥认证 - 使用 fail2ban 防止暴力破解
- 考虑使用云服务商的安全组(比 iptables 更易管理)
本章小结
Linux 网络工具的核心要点:① curl -v 查看完整请求/响应头,curl -w "%{http_code}" 提取状态码,是 API 调试的利器;② SSH 密钥认证比密码认证更安全,私钥权限必须设为 600,否则 SSH 拒绝使用;③ ~/.ssh/config 的 ProxyJump 配置跳板机,避免多次 SSH 跳转的繁琐;④ SSH 本地端口转发(-L)让本地安全访问远程内网服务,是访问数据库的常用技巧;⑤ ss -tlnp(替代 netstat)查看监听端口和对应进程;⑥ ip addr/route 是网络接口管理的现代命令(替代 ifconfig/route);⑦ nc -zv host port 快速测试端口连通性;⑧ 先用 ufw 管理防火墙规则,比 iptables 不易出错,生产环境修改防火墙规则前先设置定时恢复。