# 把这段写进 ~/.ssh/config, 之后 `ssh myserver` 就行
Host myserver
HostName 1.2.3.4
User ubuntu
Port 22
IdentityFile ~/.ssh/your-key.pem
ServerAliveInterval 60
# 备用端口
Host myserver-alt
HostName 1.2.3.4
Port 2222
User ubuntu
IdentityFile ~/.ssh/your-key.pem
# 本地端口转发 (本地 8888 → 远程 80)
ssh -L 8888:localhost:80 myserver
# 远程端口转发 (远程 9000 → 本地 3000, 给同事看本机服务)
ssh -R 9000:localhost:3000 myserver
# SOCKS5 代理 (debug 跨区服务时常用)
ssh -D 1080 myserver
# 跳板机 ProxyJump (a 在内网, 通过 b 进)
ssh -J bastion-host internal-host
# 生成 ed25519 密钥 (比 RSA 短 + 更安全)
ssh-keygen -t ed25519 -C "[email protected]"
# 推 public key 到远程 ~/.ssh/authorized_keys
ssh-copy-id -i ~/.ssh/id_ed25519.pub myserver
tmux new -s work # 新建命名会话
tmux ls # 列出会话
tmux a -t work # attach (重接)
tmux kill-session -t work
Ctrl+b d # detach 脱离 (会话保活)
Ctrl+b c # 新窗口
Ctrl+b n / p # 下/上 窗口
Ctrl+b 0..9 # 跳到第 N 窗口
Ctrl+b % # 垂直分屏
Ctrl+b " # 水平分屏
Ctrl+b 方向键 # 切换 pane
Ctrl+b z # 当前 pane 全屏 / 还原
Ctrl+b [ # 进入滚动模式 (q 退出)
sudo systemctl status nginx # 状态
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl reload nginx # 重载配置, 不断连接
sudo systemctl restart nginx # 完全重启
sudo systemctl enable nginx # 开机自启
sudo systemctl disable nginx
# 日志
sudo journalctl -u nginx # 全部日志
sudo journalctl -u nginx -f # 实时跟踪
sudo journalctl -u nginx --since "1 hour ago"
sudo journalctl -u nginx -p err # 只看 error
# 列出失败的 service
sudo systemctl --failed
sudo ss -tlnp # 监听 TCP 端口 + 进程
sudo ss -ulnp # 监听 UDP 端口
sudo ss -tnp # 已建立的 TCP 连接
sudo lsof -i :8080 # 谁在用 8080 端口
sudo netstat -tlnp # 老一辈写法
ping -c 4 example.com
nc -vz HOST PORT # 测 TCP 连通 (verbose, zero-IO)
nc -uvz HOST PORT # 测 UDP
mtr HOST # 路由跟踪 + 实时丢包率
traceroute HOST # 传统路由跟踪
dig example.com +short
dig example.com MX
dig @8.8.8.8 example.com # 指定 DNS 服务器
nslookup example.com
host example.com
curl -sI URL # 只看 headers
curl -v URL # verbose 连接细节
curl -L URL # 跟随重定向
curl -w '%{time_total}\n' -o /dev/null -s URL # 测响应时间
# IMDSv2 必须先拿 token (IMDSv1 已弃用)
TOKEN=$(curl -s -X PUT 'http://169.254.169.254/latest/api/token' \
-H 'X-aws-ec2-metadata-token-ttl-seconds: 60')
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
http://169.254.169.254/latest/meta-data/instance-type
# 常用 endpoint
# /instance-id 实例 ID
# /instance-type 实例类型
# /local-ipv4 内网 IP
# /public-ipv4 公网 IP
# /iam/security-credentials/<role> 临时凭证
# /placement/region 所在 region
# 加载模块
echo 'tcp_bbr' | sudo tee /etc/modules-load.d/bbr.conf
# 写入 sysctl
echo 'net.core.default_qdisc=fq' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv4.tcp_congestion_control=bbr' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
# 验证
sysctl net.ipv4.tcp_congestion_control
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# 验证
free -h
sudo swapon --show
# 在 EC2 launch 时填入 user-data, 第一次启动自动跑
# 配合 #!/bin/bash 头
# 写完查 /var/log/cloud-init-output.log 看输出
sudo tail -100 /var/log/cloud-init-output.log
aws configure --profile work
# 之后用 --profile work 切换, 或
export AWS_PROFILE=work
aws sts get-caller-identity # 验证当前身份
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId,State.Name,Tags[?Key==`Name`].Value|[0]]' --output table
# 启停
aws ec2 start-instances --instance-ids i-xxx
aws ec2 stop-instances --instance-ids i-xxx
# EBS 快照
aws ec2 create-snapshot --volume-id vol-xxx --description "manual"
aws s3 ls
aws s3 cp file.txt s3://mybucket/
aws s3 sync ./local s3://mybucket/path # 增量同步
aws s3 cp s3://mybucket/file.txt - | head # 流式读
git log --oneline -10
git log --graph --oneline --all --decorate
# 找谁写的
git blame <file>
git blame -L 10,20 <file> # 只看 10-20 行
# 暂存改动 (临时切分支)
git stash # 存
git stash pop # 取出最近一份
git stash list
# 撤销
git restore <file> # 丢弃工作区改动
git restore --staged <file> # 取消 add
git reset --soft HEAD~1 # 撤销 commit 保留改动
git reset --hard HEAD~1 # 彻底丢 (慎用)
# 远程
git remote -v
git fetch origin
git rebase origin/main # 不要在共享分支上 rebase
# 磁盘占用 (从大到小排)
du -sh ~/Desktop/* 2>/dev/null | sort -hr | head -10
df -h # 文件系统占用
# 找大文件
find / -size +500M -type f 2>/dev/null
# 按时间找
find . -mtime -1 # 最近 1 天改过的
find . -mmin -30 # 最近 30 分钟改过的
# 批量改名 (用 fd + xargs)
fd -e txt | xargs -I{} mv {} {}.bak
# 跨文件搜 (ripgrep, 比 grep 快 10x)
rg "TODO" .
rg "error" --type python
# 压缩
tar -czf backup.tar.gz dir/ # 压
tar -xzf backup.tar.gz # 解
tar -tzf backup.tar.gz | head # 看 content 不解压
这是 living document — 用到了就回来加. 原版分散在主页 #cheatsheet 和 #aws-snippets 两个 tab, 合并成 blog post 更好查更好更新.