脚本

一些常用的服务器运维脚本,复制一行命令就能跑。执行前建议先看一眼源码。

按标签筛选

显示 23 / 23 个

系统信息速查

普通用户即可只读

一条命令看清发行版、内核、CPU、内存、磁盘、网卡与常用服务状态。只读,不改动任何配置。

curl -fsSL https://www.moiu.cn/scripts/sysinfo.sh | bash
  • system
  • readonly

安装 Docker

需要 root

用官方脚本装 Docker 与 Compose,并写入国内镜像加速。已经装过会先问一句,不会直接覆盖。

curl -fsSL https://www.moiu.cn/scripts/docker-install.sh | bash
  • docker
  • install

开启 BBR

需要 root

把拥塞控制换成 BBR、队列规则换成 fq,写进 sysctl.d 立即生效。内核低于 4.9 会直接退出。

curl -fsSL https://www.moiu.cn/scripts/bbr-enable.sh | bash
  • network
  • tuning

创建 Swap

需要 root

在 /swapfile 建一个指定大小的交换文件,写进 fstab 开机自动挂载,并把 swappiness 调到 10。

curl -fsSL https://www.moiu.cn/scripts/swap-setup.sh | bash
  • memory
  • tuning

SSH 基础加固

需要 root

禁 root 直登、关密码登录、收紧重试与认证窗口。没有密钥就先拒绝执行,改完先 sshd -t 校验,不过就回滚。

curl -fsSL https://www.moiu.cn/scripts/ssh-harden.sh | bash
  • security
  • ssh

目录打包备份

普通用户即可

把目录打成带时间戳的 tar.gz,自动排除 node_modules、.git、缓存与 dist。不需要 root。

curl -fsSL https://www.moiu.cn/scripts/dir-backup.sh | bash
  • backup
  • ops

安装 Komari

需要 root

用南大镜像源部署 Komari 监控,数据持久化在 /opt/komari/data。需要先装好 Docker。

curl -fsSL https://www.moiu.cn/scripts/komari-install.sh | bash
  • monitor
  • docker

卸载哪吒 Agent

需要 root

停服务、删 systemd 单元、清目录与残留进程,最后自己验证一遍。支持带随机后缀的命名。

curl -fsSL https://www.moiu.cn/scripts/nezha-uninstall.sh | bash
  • ops
  • cleanup

系统更新

需要 root

刷新软件源、升级已装包、清掉无用依赖。需要重启时只提示,不会替你重启。

curl -fsSL https://www.moiu.cn/scripts/system-update.sh | bash
  • update
  • system

设置时区

需要 root

默认设为 Asia/Shanghai,并打开自动校时。传一个时区名就能改,比如 UTC。

curl -fsSL https://www.moiu.cn/scripts/timezone-set.sh | bash
  • system
  • time

配置防火墙

需要 root

默认拒绝入站、放行出站,只开 SSH 与 80/443。先放行 SSH 再启用,不会把自己关在外面。

curl -fsSL https://www.moiu.cn/scripts/ufw-firewall.sh | bash
  • security
  • network

安装 Nginx

需要 root

装好即启动并设为开机自启。已经装过就直接退出,不覆盖你现有的配置。

curl -fsSL https://www.moiu.cn/scripts/nginx-install.sh | bash
  • web
  • install

申请 HTTPS 证书

需要 root

用 certbot 的 nginx 插件自动改配置、配续期定时器、把 http 跳去 https。域名要先解析到本机。

curl -fsSL https://www.moiu.cn/scripts/certbot-ssl.sh | bash
  • web
  • tls

磁盘清理

需要 root

清 apt 缓存、无用依赖、journal 日志、/tmp 陈旧文件、Docker 悬空镜像。不碰 /home 与业务目录。

curl -fsSL https://www.moiu.cn/scripts/disk-cleanup.sh | bash
  • ops
  • disk

端口占用速查

普通用户即可只读

列出正在监听的端口与对应进程,给个端口号就只看那一个。只读,不需要 root。

curl -fsSL https://www.moiu.cn/scripts/port-usage.sh | bash
  • network
  • readonly

安装 Node.js

需要 root

用 NodeSource 官方源装指定主版本(默认 22 LTS)。已经装过会先问一句,不会直接覆盖。

curl -fsSL https://www.moiu.cn/scripts/nodejs-install.sh | bash
  • runtime
  • install

安装 fail2ban

需要 root

装好后开一个 sshd 的 jail:10 分钟内失败 5 次就封 1 小时。配合防火墙脚本效果更好。

curl -fsSL https://www.moiu.cn/scripts/fail2ban-install.sh | bash
  • security
  • install

配置密钥登录

需要 root

把一个公钥写进指定用户的 authorized_keys,顺手把权限修好。公钥从参数传,不从标准输入读。

curl -fsSL https://www.moiu.cn/scripts/ssh-key-setup.sh | bash
  • security
  • ssh

新建 sudo 用户

需要 root

建一个带 sudo 权限的用户,可选把公钥一并写进去。会自动认 sudo 组还是 wheel 组。

curl -fsSL https://www.moiu.cn/scripts/user-add.sh | bash
  • security
  • ops

提高文件描述符上限

需要 root

把 nofile 提到 65535,同时调大 file-max 与连接队列。高并发服务常在这三处撞墙。

curl -fsSL https://www.moiu.cn/scripts/limits-tune.sh | bash
  • tuning
  • system

证书到期检查

普通用户即可只读

扫本机常见证书目录,也可以传个域名查线上证书,直接告诉你还剩多少天。只读。

curl -fsSL https://www.moiu.cn/scripts/cert-check.sh | bash
  • tls
  • readonly

DNS 与连通性检查

普通用户即可只读

看用的哪个解析器、域名解不解得出来、公共 DNS 通不通、出口 IP 是什么。只读。

curl -fsSL https://www.moiu.cn/scripts/dns-check.sh | bash
  • network
  • readonly

定时任务速查

需要 root只读

列出所有用户的 crontab、/etc/cron.d 与 systemd timer。排查「半夜谁在跑东西」用。只读但要 root。

curl -fsSL https://www.moiu.cn/scripts/crontab-list.sh | bash
  • ops
  • readonly