Appearance
一、Docker 常用
镜像操作
shell
# 1. 清理构建缓存
docker builder prune -a
# 2. 清理未使用的镜像(省 ~5 GB)
docker image prune -a
# 3. 清理停止的容器、未使用的网络等
docker system pruneStack 操作
shell
# 查看运行的Stack
docker stack ls容器操作
根据service name 查询容器 id
shell
docker ps \
--filter "label=com.docker.swarm.service.name=family-ledger_postgres" \
--format "{{.ID}}" |
head -n1备份容器中的Postgres SQL数据库
备份:
已假设已获取了容器id为 48e62ef1dcc5
shell
docker exec 48e62ef1dcc5 pg_dump -U postgres -d family_ledger -Fc > /tmp/back_20260828_1206.dump
# 要密码
docker exec 48e62ef1dcc5 env PGPASSWORD=密码 pg_dump -U postgres -d family_ledger -Fc > /tmp/back_20260828_1206.dump- Fc 是 pg_dump 的一个输出格式参数,拆开来看:
F = Format(输出格式)
c = custom(自定义格式)
导出为纯本
shell
docker exec 48e62ef1dcc5 \
pg_dump \
-U postgres \
-d family_ledger \
> /tmp/back_20260828_1206.sql恢复:
shell
# 1. 从宿主机拷进容器
docker cp /tmp/back_20260828_1206.dump 48e62ef1dcc5:/tmp/
# 2. 在容器里恢复
docker exec 48e62ef1dcc5 pg_restore -U postgres -d family_ledger /tmp/back_20260828_1206.dump如果容器内忆隐射端口出来的话,则可以不用先复制dump文件,可使用管道
shell
pg_restore -U postgres -h localhost -p 5432 -d family_ledger < /tmp/back_20260828_1206.dump从文本恢复
bash
psql -U postgres -d family_ledger -f /tmp/back_20260828_1206.sql标准恢复流程(最不容易出错):
bash
# 1. 确认容器在跑
docker ps
# 2. 确认数据库存在,不存在就建
docker exec -it 48e62ef1dcc5 psql -U postgres -c "CREATE DATABASE family_ledger"
# 3. 拷文件进容器
docker cp /tmp/back_20260828_1206.dump 48e62ef1dcc5:/tmp/
# 4. 恢复
docker exec -it 48e62ef1dcc5 pg_restore -U postgres -d family_ledger /tmp/back_20260828_1206.dump二、Linux 常用命令
给某文件授权
bash
bashchmod u+x ./exe1. 检查磁盘
shell
# 查看所有磁盘使用情况
df -h
# 查看指定目录所在磁盘
df -h /path/to/dir
# 以人类可读格式显示 inode 使用情况
df -i查看目录大小
shell
# 查看当前目录下各子目录大小
du -h --max-depth=1
# 查看指定目录大小
du -sh /path/to/dir
# 排序找出最大的目录
du -h --max-depth=1 /path/to/dir | sort -rh
# 查看当前目录前10个最大的文件/目录
du -ah /path/to/dir | sort -rh | head -10查找大文件
shell
# 查找当前目录下大于100M的文件
find . -type f -size +100M -exec ls -lh {} \;
# 查找指定目录下大于1G的文件
find / -type f -size +1G -exec ls -lh {} \; 2>/dev/null
# 查找并排序大文件
find . -type f -size +10M -exec du -h {} + | sort -rh | head -20清理磁盘空间
shell
# 查看已删除但未释放的文件
lsof +L1
# 清理系统日志
journalctl --vacuum-size=100M
# 清理 apt 缓存(Debian/Ubuntu)
sudo apt clean
# 清理旧内核(Debian/Ubuntu)
sudo apt autoremove --purge
# 清理 yum 缓存(CentOS/RHEL)
sudo yum clean all磁盘 IO 监控
shell
# 实时查看磁盘 IO
iostat -x 1
# 查看磁盘使用率最高的进程
iotop
# 查看磁盘挂载信息
mount | column -t2. nano 使用手册
基本操作
shell
# 打开文件
nano filename
# 打开文件并定位到指定行
nano +行号 filename
# 只读模式打开
nano -R filename常用快捷键
文件操作
| 快捷键 | 说明 |
|---|---|
Ctrl+O | 保存文件 |
Ctrl+X | 退出编辑器 |
Ctrl+R | 插入其他文件内容 |
Ctrl+T | 检查拼写 |
光标移动
| 快捷键 | 说明 |
|---|---|
Ctrl+A | 移动到行首 |
Ctrl+E | 移动到行尾 |
Ctrl+P | 移动到上一行 |
Ctrl+N | 移动到下一行 |
Ctrl+← | 向左移动一个单词 |
Ctrl+→ | 向右移动一个单词 |
Ctrl+Y | 向上翻页 |
Ctrl+V | 向下翻页 |
编辑操作
| 快捷键 | 说明 |
|---|---|
Ctrl+K | 剪切当前行 |
Ctrl+U | 粘贴 |
Ctrl+J | 对齐当前段落 |
Ctrl+D | 删除当前行 |
Ctrl+\\ | 替换文本 |
Ctrl+C | 显示当前行号 |
搜索操作
| 快捷键 | 说明 |
|---|---|
Ctrl+W | 向下搜索 |
Alt+W | 向上搜索 |
Ctrl+\ | 查找并替换 |
Alt+G | 跳转到指定行 |
其他操作
| 快捷键 | 说明 |
|---|---|
Ctrl+L | 重绘屏幕 |
Ctrl+Z | 挂起 nano |
Ctrl+G | 查看帮助 |
Alt+U | 撤销 |
Alt+E | 重做 |
Alt+Shift+U | 大小写切换 |
Alt+I | 自动缩进 |
常见使用场景
shell
# 快速编辑配置文件
sudo nano /etc/nginx/nginx.conf
# 搜索并替换
# 1. 按 Ctrl+\ 打开替换对话框
# 2. 输入要查找的文本
# 3. 输入替换的文本
# 4. 选择替换方式:
# - A: 全部替换
# - Y: 替换当前并确认下一个
# - N: 跳过当前
# - ^C: 取消
# 复制粘贴多行
# 1. 按 Ctrl+K 剪切多行(连续按)
# 2. 移动到目标位置
# 3. 按 Ctrl+U 粘贴三、检查端口占用
查看端口占用
shell
# 查看所有监听端口
netstat -tuln
# 查看指定端口占用
netstat -tuln | grep :80
# 查看指定端口占用(ss 命令,更快)
ss -tuln | grep :80
# 查看指定端口占用的进程
lsof -i :80
# 查看指定端口占用的进程(更详细)
netstat -tulnp | grep :80查找进程占用端口
shell
# 查找指定进程占用的端口
netstat -tulnp | grep nginx
# 查找指定进程的 PID
pgrep nginx
# 根据 PID 查看进程详情
ps -p <PID> -f
# 查看进程的网络连接
netstat -anp | grep <PID>杀死占用端口的进程
shell
# 方法1:根据端口号杀死进程
kill $(lsof -t -i :80)
# 方法2:强制杀死进程
kill -9 $(lsof -t -i :80)
# 方法3:根据进程名杀死
pkill nginx
# 方法4:强制杀死指定进程
killall -9 nginx端口扫描与测试
shell
# 测试端口是否开放
telnet localhost 80
# 测试端口是否开放(nc 命令)
nc -zv localhost 80
# 测试端口是否开放(curl)
curl -v telnet://localhost:80
# 扫描开放的端口
nmap localhost
# 扫描指定端口范围
nmap -p 80-443 localhost常见端口说明
| 端口 | 服务 | 说明 |
|---|---|---|
| 22 | SSH | 安全外壳协议 |
| 80 | HTTP | 超文本传输协议 |
| 443 | HTTPS | 安全超文本传输协议 |
| 3306 | MySQL | MySQL 数据库 |
| 5432 | PostgreSQL | PostgreSQL 数据库 |
| 6379 | Redis | Redis 缓存数据库 |
| 8080 | HTTP | 常用 Web 服务端口 |
| 8443 | HTTPS | 常用 Web 安全端口 |
端口监听状态说明
| 状态 | 说明 |
|---|---|
| LISTEN | 监听中,等待连接 |
| ESTABLISHED | 已建立的连接 |
| TIME_WAIT | 等待关闭的连接 |
| CLOSE_WAIT | 等待关闭的连接 |
| SYN_SENT | 等待对方确认连接 |
| SYN_RECEIVED | 收到对方连接请求 |
四、检查网络
网络诊断命令安装说明
| 命令 | 系统自带 | 安装方式 |
|---|---|---|
ping | ✅ 是 | 无需安装 |
traceroute / tracert | ✅ 是 | 无需安装 |
nslookup | ✅ 是 | 无需安装 |
dig | ❌ 否 | apt install dnsutils 或 yum install bind-utils |
telnet | ❌ 否 | apt install telnet 或 yum install telnet |
nc (netcat) | ❌ 否 | apt install netcat 或 yum install nmap-ncat |
curl | ✅ 大多数自带 | apt install curl 或 yum install curl |
wget | ✅ 大多数自带 | apt install wget 或 yum install wget |
nmap | ❌ 否 | apt install nmap 或 yum install nmap |
检查网络连通性(ping)
shell
# 测试与目标主机的连通性
ping google.com
# 指定ping的次数(Linux)
ping -c 4 google.com
# 指定ping的次数(Windows)
ping -n 4 google.com
# 指定超时时间(秒)
ping -W 3 google.com
# 指定包大小
ping -s 1024 google.com
# 持续ping(Ctrl+C 停止)
ping google.com路由追踪(traceroute)
shell
# 追踪到目标主机的路由路径(Linux)
traceroute google.com
# 追踪到目标主机的路由路径(Windows)
tracert google.com
# 不进行DNS解析(更快)
traceroute -n google.com
# 指定跳数限制
traceroute -m 20 google.com
# 使用ICMP协议(Linux)
traceroute -I google.com
# 使用TCP协议(绕过防火墙)
traceroute -T google.comDNS 查询
shell
# 基本DNS查询(nslookup)
nslookup google.com
# 查询指定类型的记录
nslookup -type=MX google.com # 邮件服务器
nslookup -type=CNAME google.com # 别名
nslookup -type=NS google.com # 域名服务器
# 使用指定DNS服务器查询
nslookup google.com 8.8.8.8
# 详细DNS查询(dig)
dig google.com
# 查询特定记录类型
dig google.com A # IPv4地址
dig google.com AAAA # IPv6地址
dig google.com MX # 邮件服务器
dig google.com NS # 域名服务器
dig google.com TXT # 文本记录
# 使用指定DNS服务器查询
dig @8.8.8.8 google.com
# 反向DNS查询
dig -x 8.8.8.8
# 简洁输出
dig +short google.com检查端口连通性
使用 telnet
shell
# 安装telnet(如果未安装)
# Debian/Ubuntu: sudo apt install telnet
# CentOS/RHEL: sudo yum install telnet
# 测试端口连通性
telnet 192.168.1.100 80
# 测试端口连通性(超时5秒)
timeout 5 telnet 192.168.1.100 80使用 nc (netcat)
shell
# 安装nc(如果未安装)
# Debian/Ubuntu: sudo apt install netcat
# CentOS/RHEL: sudo yum install nmap-ncat
# 测试端口连通性
nc -zv 192.168.1.100 80
# 测试端口范围
nc -zv 192.168.1.100 80-443
# 指定超时时间(秒)
nc -zv -w 3 192.168.1.100 80
# 扫描多个端口
nc -zv 192.168.1.100 80 443 8080使用 curl
shell
# 测试HTTP端口
curl -v telnet://192.168.1.100:80
# 测试HTTPS端口
curl -v telnet://192.168.1.100:443
# 测试HTTP连接
curl -I http://google.com
# 测试HTTPS连接
curl -I https://google.com
# 指定超时时间
curl -m 5 -I http://google.com使用 bash 内置 /dev/tcp
shell
# 不需要安装任何工具
timeout 3 bash -c 'echo > /dev/tcp/192.168.1.100/80' && echo "端口开放" || echo "端口关闭"
# 封装成函数
check_port() {
timeout 3 bash -c "echo > /dev/tcp/$1/$2" 2>/dev/null && echo "$1:$2 开放" || echo "$1:$2 关闭"
}
check_port 192.168.1.100 80HTTP 请求测试
shell
# 获取HTTP响应头
curl -I http://google.com
# 获取详细请求信息
curl -v http://google.com
# 跟随重定向
curl -L http://google.com
# 指定User-Agent
curl -A "Mozilla/5.0" http://google.com
# 指定超时时间
curl -m 10 http://google.com
# 下载文件
wget http://example.com/file.zip
# 断点续传
wget -c http://example.com/file.zip网络接口信息
shell
# 查看网络接口信息(Linux)
ip addr show
# 查看网络接口信息(旧命令)
ifconfig
# 查看路由表(Linux)
ip route show
# 查看路由表(旧命令)
route -n
# 查看DNS配置
cat /etc/resolv.conf
# 查看主机名
hostname
# 查看网络连接状态
ss -s综合诊断脚本
shell
#!/bin/bash
# 网络诊断脚本
echo "=== 网络诊断 ==="
# 检查本地网络
echo "1. 检查本地网络接口..."
ip addr show | grep -E "inet.*scope global"
# 检查默认网关
echo "2. 检查默认网关..."
ip route | grep default
# 检查DNS
echo "3. 检查DNS配置..."
cat /etc/resolv.conf | grep nameserver
# 测试网关连通性
GATEWAY=$(ip route | grep default | awk '{print $3}')
echo "4. 测试网关连通性 ($GATEWAY)..."
ping -c 2 $GATEWAY
# 测试外网连通性
echo "5. 测试外网连通性..."
ping -c 2 8.8.8.8
# 测试DNS解析
echo "6. 测试DNS解析..."
nslookup google.com
# 测试HTTP连通性
echo "7. 测试HTTP连通性..."
curl -m 5 -I http://google.com
echo "=== 诊断完成 ==="五、Docker 常规操作
Docker 安装
shell
# 安装 Docker(Ubuntu)
curl -fsSL https://get.docker.com | sh
# 启动 Docker 服务
sudo systemctl start docker
sudo systemctl enable docker
# 将当前用户添加到 docker 组(免 sudo)
sudo usermod -aG docker $USER
# 验证安装
docker --version
docker run hello-world镜像操作
shell
# 搜索镜像
docker search nginx
# 拉取镜像
docker pull nginx
# 拉取指定版本
docker pull nginx:1.24
# 查看本地镜像
docker images
# 删除镜像
docker rmi nginx
# 删除所有未使用的镜像
docker image prune -a
# 导出镜像
docker save -o nginx.tar nginx:latest
# 导入镜像
docker load -i nginx.tar容器生命周期
shell
# 创建并运行容器
docker run -d --name my-nginx -p 80:80 nginx
# 查看运行中的容器
docker ps
# 查看所有容器(包括已停止)
docker ps -a
# 停止容器
docker stop my-nginx
# 启动已停止的容器
docker start my-nginx
# 重启容器
docker restart my-nginx
# 删除容器
docker rm my-nginx
# 强制删除运行中的容器
docker rm -f my-nginx
# 删除所有已停止的容器
docker container prune容器管理
shell
# 进入运行中的容器
docker exec -it my-nginx bash
# 查看容器日志
docker logs my-nginx
# 实时查看日志
docker logs -f my-nginx
# 查看容器详细信息
docker inspect my-nginx
# 查看容器资源使用
docker stats
# 查看容器端口映射
docker port my-nginx
# 复制文件到容器
docker cp ./file.txt my-nginx:/tmp/
# 从容器复制文件
docker cp my-nginx:/tmp/file.txt ./Service
shell
docker service update --image registry.example.com/nginx:latest stackname_servicename # 更新某个stack里的指定服务的镜像
docker service update --env DB_HOST=xxx stackname_servicename # 改环境变量
docker service update --replicas 2 stackname_servicename # 扩副本数
docker service rollback stackname_servicename # 回滚到上一版
docker service ps stackname_servicename # 查看当前状态Docker Compose
shell
# 启动所有服务
docker-compose up -d
# 停止所有服务
docker-compose down
# 查看服务状态
docker-compose ps
# 查看日志
docker-compose logs -f
# 重建并启动
docker-compose up -d --build
# 扩展服务
docker-compose up -d --scale web=3
# 进入容器
docker-compose exec web bash网络操作
shell
# 查看网络列表
docker network ls
# 创建网络
docker network create my-network
# 删除网络
docker network rm my-network
# 将容器连接到网络
docker network connect my-network my-nginx
# 断开网络连接
docker network disconnect my-network my-nginx
# 查看网络详细信息
docker network inspect my-network数据卷操作
shell
# 创建数据卷
docker volume create my-volume
# 查看数据卷
docker volume ls
# 删除数据卷
docker volume rm my-volume
# 删除未使用的数据卷
docker volume prune
# 使用数据卷运行容器
docker run -d --name my-nginx -v my-volume:/usr/share/nginx/html nginx
# 挂载宿主机目录
docker run -d --name my-nginx -v /host/path:/container/path nginx
# 只读挂载
docker run -d --name my-nginx -v /host/path:/container/path:ro nginx清理资源
shell
# 清理所有未使用的资源
docker system prune -a
# 清理未使用的容器
docker container prune -a
# 清理未使用的镜像
docker image prune -a
# 清理未使用的网络
docker network prune
# 清理未使用的数据卷
docker volume prune
# 查看磁盘使用
docker system df常用 Docker 运行参数
| 参数 | 说明 |
|---|---|
-d | 后台运行 |
-it | 交互模式 |
--name | 容器名称 |
-p 宿主:容器 | 端口映射 |
-v 宿主:容器 | 挂载卷 |
--rm | 停止后自动删除 |
--restart=always | 自动重启 |
-e KEY=VALUE | 设置环境变量 |
--network | 指定网络 |
--memory | 内存限制 |
--cpus | CPU 限制 |
常见问题解决
shell
# 权限问题:Cannot connect to the Docker daemon
sudo systemctl start docker
# 端口被占用
docker: Error response from daemon: Ports are not available
# 解决:修改端口映射或停止占用端口的服务
# 容器无法启动
docker logs <容器名> # 查看错误日志
# 镜像拉取超时
# 配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://mirror.ccs.tencentyun.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker六、使用 openssl 生成随机密码
openssl 安装说明
| 系统 | 安装方式 |
|---|---|
| Debian/Ubuntu | sudo apt install openssl |
| CentOS/RHEL | sudo yum install openssl |
| macOS | 系统自带 |
| Windows | 需单独安装或使用 Git Bash |
生成随机密码
shell
# 生成16位随机密码(基础)
openssl rand -base64 16
# 生成32位随机密码
openssl rand -base64 32
# 生成64位随机密码
openssl rand -base64 64
# 生成指定长度的随机密码(字节数)
openssl rand -base64 <字节数>生成可打印字符密码
shell
# 生成只包含字母和数字的密码
openssl rand -base64 <字节数> | tr -d '/+=' | head -c <长度>
# 生成包含特殊字符的密码
openssl rand -base64 <字节数>
# 生成16位纯字母数字密码
openssl rand -base64 16 | tr -d '/+=' | head -c 16
# 生成20位字母数字密码
openssl rand -base64 20 | tr -d '/+=' | head -c 20生成十六进制密码
shell
# 生成16位十六进制密码
openssl rand -hex 16
# 生成32位十六进制密码
openssl rand -hex 32
# 生成指定字节数的十六进制
openssl rand -hex <字节数>生成密码并保存到文件
shell
# 生成密码并保存
openssl rand -base64 16 > password.txt
# 生成密码并直接显示
echo "密码: $(openssl rand -base64 16)"
# 生成多个密码
for i in {1..5}; do openssl rand -base64 16; done
# 生成密码并保存到文件(每行一个密码)
for i in {1..10}; do openssl rand -base64 16 >> passwords.txt; done密码强度对照表
| 长度 | 字节数 | 随机性 | 安全等级 |
|---|---|---|---|
| 8位 | 6字节 | 2^48 | 低(不推荐) |
| 12位 | 9字节 | 2^72 | 中等 |
| 16位 | 12字节 | 2^96 | 较高 |
| 20位 | 15字节 | 2^120 | 高 |
| 32位 | 24字节 | 2^192 | 非常高 |
实用密码生成示例
shell
# 生成强密码(推荐)
openssl rand -base64 24 | tr -d '/+=' | head -c 20
# 生成带特殊字符的强密码
openssl rand -base64 24 | head -c 20
# 生成简单密码(仅字母数字)
openssl rand -base64 12 | tr -d '/+=' | head -c 12
# 生成密码哈希(用于数据库存储)
openssl rand -base64 32 | tr -d '/+=' | head -c 32
# 生成随机盐值(用于密码加密)
openssl rand -hex 16
# 生成随机密钥(用于加密)
openssl rand -base64 32其他随机数生成方式
shell
# 使用 /dev/urandom(Linux)
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 16
# 使用 Python
python3 -c "import secrets; print(secrets.token_urlsafe(16))"
# 使用 Node.js
node -e "console.log(require('crypto').randomBytes(16).toString('base64'))"
# 使用 date 生成简单随机数(不推荐,安全性低)
date +%s%N | sha256sum | head -c 16七、网络请求
Invoke-WebRequest
Invoke-WebRequest 适用于在 Windows Powershell中使用。
1. GET 请求
shell
(Invoke-WebRequest -Uri "https://example.com" -Method GET -Headers @{ "Authorization" = "Bearer Token"; "Content-Type" = "application/json" }).Content2. POST 请求
shell
(Invoke-WebRequest -Uri "https://example.com" -Method POST -Headers @{ "Authorization" = "Bearer Token"; "Content-Type" = "application/json" } -InFile ".\data.json" ).Content八、SSH 隧道
SSH 隧道(SSH Tunneling),也叫 SSH 端口转发,是通过 SSH 加密连接来中转网络流量的技术。常用于安全访问内网服务、绕过防火墙、保护明文传输等场景。
1. 密钥认证(SSH Key Authentication)
原理:
用一对密钥(公钥 + 私钥)代替密码:
私钥 留在本地,相当于"身份证"
公钥 放到服务器上,相当于"锁"
登录时 SSH 用私钥签名,服务器用公钥验证,匹配就放行
操作步骤
1. 本地生成密钥对
bash
ssh-keygen -t ed25519 -C "your_email@example.com" # -C 是备注说明方便记忆ed25519 是一种加密算法 固定值 ;如果系统较老不支持 ed25519,用
ssh-keygen -t rsa -b 4096
一路回车就行。会生成两个文件:
shell
~/.ssh/id_ed25519 → 私钥(自己留着,别给别人)
~/.ssh/id_ed25519.pub → 公钥(放到服务器上)可以设置一个密钥密码(passphrase),这样即使私钥泄露也有一层保护。不输直接回车就是无密码。
2. 把公钥传到服务器
bash
ssh-copy-id user@server-ip输入一次密码,公钥就自动追加到服务器的 ~/.ssh/authorized_keys 里了。
手动方式(如果 ssh-copy-id 不可用):
bash
# 本地
cat ~/.ssh/id_ed25519.pub
# 然后登录服务器
mkdir -p ~/.ssh
echo "刚才复制的公钥内容" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh3. 测试登录
bash
ssh user@server-ip不用输密码就进去了,说明成功 ✅