[{"content":"一、服务器环境初始化 1、检查 1 2 3 4 5 6 7 8 9 cat /etc/os-release \u0026amp;\u0026amp; uname -r lscpu | grep -E \u0026#39;Model name|CPU\\\\(s\\\\)|Core|Thread\u0026#39; \u0026amp;\u0026amp; free -h df -hT \u0026amp;\u0026amp; lsblk systemctl list-units --type=service --state=running systemctl list-unit-files --type=service --state=enabled sudo ss -tulnp sudo sshd -T | grep -E \u0026#39;^(port|permitrootlogin|passwordauthentication|pubkeyauthentication|maxauthtries|clientaliveinterval|clientalivecountmax)\u0026#39; ulimit -n \u0026amp;\u0026amp; ulimit -u \u0026amp;\u0026amp; cat /proc/sys/fs/file-max \u0026amp;\u0026amp; cat /proc/sys/fs/file-nr swapon --show \u0026amp;\u0026amp; sysctl vm.swappiness net.core.somaxconn 2、创建swap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 创建swap文件 fallocate -l 2G /swapfile chmod 600 /swapfile mkswap /swapfile swapon /swapfile echo \u0026#39;/swapfile none swap sw 0 0\u0026#39; \u0026gt;\u0026gt; tee -a /etc/fstab tee /etc/sysctl.d/99-swap.conf \u0026lt;\u0026lt;\u0026#39;EOF\u0026#39; # Swap usage preference vm.swappiness = 10 EOF # vm.swappiness=10 表示降低内核主动使用 Swap 的倾向，但不代表只有内存耗尽时才使用 Swap。 sysctl --system # 加载配置 free -h 3、apt优化，软件安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # 备份原 sources.list cp /etc/apt/sources.list /etc/apt/sources.list.bak # 替换为阿里云镜像（Ubuntu） sed -i \u0026#39;s@http://.*.ubuntu.com@http://mirrors.aliyun.com@g\u0026#39; /etc/apt/sources.list # 更新并升级 apt update \u0026amp;\u0026amp; sudo apt upgrade -y # 清理无用依赖和缓存 apt autoremove --purge -y apt clean # 安装所需服务 apt -y install lrzsz 4、ssh配置 4.1、本机创建key 1 2 3 win+r cmd ssh-keygen -t ed25519 4.2、上传服务器 1 2 3 4 cd /root/.ssh rz chmod 600 ./* cat id_ed25519.pub \u0026gt;\u0026gt; authorized_keys 4.3、ssh优化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 vim /etc/ssh/sshd_config # 关闭 DNS 反向解析（避免客户端 IP 解析域名时因 DNS 响应慢导致卡顿） UseDNS no UsePAM yes # PAM配置需要 # 关闭 GSSAPI 认证（非域环境无用，关闭可减少 PAM 开销和 Kerberos 尝试） GSSAPIAuthentication no Port 54322 X11Forwarding no PrintMotd no # 由PAM管理，ssh关闭 # AllowUsers ops admin # 限制认证尝试次数与超时时间（防暴力破解） MaxAuthTries 3 LoginGraceTime 20 # 启用公钥认证，彻底禁用密码认证 PubkeyAuthentication yes PasswordAuthentication no ChallengeResponseAuthentication no KbdInteractiveAuthentication no # 单个连接最大通道数（根据业务需求调整） MaxSessions 10 # 未认证连接限制：50个开始随机拒绝，达到200个全拒绝（防慢速 DoS） MaxStartups 50:30:200 # 心跳保活：每 60 秒发送一次，连续 5 次无响应则断开（清理僵尸连接） ClientAliveInterval 60 ClientAliveCountMax 5 # 测试配置是否正确 sshd -t systemctl restart ssh 5、精简服务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 # 一个容器包服务，跟apt一样还吃资源 systemctl disable --now snapd.service snapd.socket apt purge -y snapd rm -rf /snap /var/snap /var/lib/snapd # 硬件相关关掉 # multipathd（多路径设备管理） systemctl disable --now multipathd.service multipathd.socket # open-iscsi（iSCSI 远程存储） systemctl disable --now open-iscsi.service iscsid.service iscsid.socket systemctl mask open-iscsi.service iscsid.service iscsid.socket # udisks2（磁盘热插拔/自动挂载守护） systemctl disable --now udisks2.service # fwupd（固件更新守护） systemctl disable --now fwupd.service fwupd-refresh.timer # tuned：性能调优守护，对 2G 小机器意义不大 systemctl disable --now tuned.service # packagekit 是桌面环境的包管理抽象层，服务器上用 apt 直接管理即可。atd 是一次性定时任务，如果你不需要 at 命令，可以关掉。 systemctl disable --now packagekit.service systemctl mask packagekit.service # mask 会阻止任何服务通过依赖关系把它重新拉起。 systemctl disable --now atd.service # getty@tty1 和 serial-getty@ttyS0 是本地登录终端和串口控制台。 systemctl disable --now getty@tty1.service systemctl disable --now serial-getty@ttyS0.service # 更新关闭 systemctl disable --now unattended-upgrades.service # 如果进程还在，直接杀掉 pkill -f unattended-upgrade-sh # 移除错误上报和崩溃报告 systemctl disable --now whoopsie apport apt purge -y whoopsie apport popularity-contest systemctl list-units --type=service --state=running 6、配置motd 1 2 3 sed -i \u0026#39;s/^ENABLED=.*/ENABLED=0/\u0026#39; /etc/default/motd-news # 新闻 chmod -x /etc/update-motd.d/* vim /etc/motd 7、中文支持（如果vim或系统乱码就执行） 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 # 安装中文语言包（提供 zh_CN.UTF-8 的生成素材） apt update \u0026amp;\u0026amp; apt install -y language-pack-zh-hans language-pack-zh-hans-base # 同时生成中文和英文 UTF-8 locale（英文用于保证 TTY 兼容性） locale-gen en_US.UTF-8 locale-gen zh_CN.UTF-8 update-locale --reset LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh grep -nE \u0026#39;UsePAM|AcceptEnv\u0026#39; /etc/ssh/sshd_config #看ssh打开这两个配置没PAM yes cat /etc/default/locale locale # 看配置应用没 locale -a locale charmap # 如果vim还有问题就执行 cat \u0026gt; ~/.vimrc \u0026lt;\u0026lt;\u0026#39;EOF\u0026#39; \u0026#34; UTF-8 encoding set encoding=utf-8 set fileencoding=utf-8 set fileencodings=utf-8,gb18030,gbk,gb2312 \u0026#34; Display set number set cursorline set wrap \u0026#34; Search set hlsearch set incsearch \u0026#34; Indentation set tabstop=4 set shiftwidth=4 set expandtab \u0026#34; Terminal compatibility set termguicolors EOF ","date":"2026-07-11T00:00:00+08:00","permalink":"/p/test1/","title":"Test3"},{"content":"一、服务器环境初始化 1、检查 1 2 3 4 5 6 7 8 9 cat /etc/os-release \u0026amp;\u0026amp; uname -r lscpu | grep -E \u0026#39;Model name|CPU\\\\(s\\\\)|Core|Thread\u0026#39; \u0026amp;\u0026amp; free -h df -hT \u0026amp;\u0026amp; lsblk systemctl list-units --type=service --state=running systemctl list-unit-files --type=service --state=enabled sudo ss -tulnp sudo sshd -T | grep -E \u0026#39;^(port|permitrootlogin|passwordauthentication|pubkeyauthentication|maxauthtries|clientaliveinterval|clientalivecountmax)\u0026#39; ulimit -n \u0026amp;\u0026amp; ulimit -u \u0026amp;\u0026amp; cat /proc/sys/fs/file-max \u0026amp;\u0026amp; cat /proc/sys/fs/file-nr swapon --show \u0026amp;\u0026amp; sysctl vm.swappiness net.core.somaxconn 2、创建swap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 创建swap文件 fallocate -l 2G /swapfile chmod 600 /swapfile mkswap /swapfile swapon /swapfile echo \u0026#39;/swapfile none swap sw 0 0\u0026#39; \u0026gt;\u0026gt; tee -a /etc/fstab tee /etc/sysctl.d/99-swap.conf \u0026lt;\u0026lt;\u0026#39;EOF\u0026#39; # Swap usage preference vm.swappiness = 10 EOF # vm.swappiness=10 表示降低内核主动使用 Swap 的倾向，但不代表只有内存耗尽时才使用 Swap。 sysctl --system # 加载配置 free -h 3、apt优化，软件安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # 备份原 sources.list cp /etc/apt/sources.list /etc/apt/sources.list.bak # 替换为阿里云镜像（Ubuntu） sed -i \u0026#39;s@http://.*.ubuntu.com@http://mirrors.aliyun.com@g\u0026#39; /etc/apt/sources.list # 更新并升级 apt update \u0026amp;\u0026amp; sudo apt upgrade -y # 清理无用依赖和缓存 apt autoremove --purge -y apt clean # 安装所需服务 apt -y install lrzsz 4、ssh配置 4.1、本机创建key 1 2 3 win+r cmd ssh-keygen -t ed25519 4.2、上传服务器 1 2 3 4 cd /root/.ssh rz chmod 600 ./* cat id_ed25519.pub \u0026gt;\u0026gt; authorized_keys 4.3、ssh优化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 vim /etc/ssh/sshd_config # 关闭 DNS 反向解析（避免客户端 IP 解析域名时因 DNS 响应慢导致卡顿） UseDNS no UsePAM yes # PAM配置需要 # 关闭 GSSAPI 认证（非域环境无用，关闭可减少 PAM 开销和 Kerberos 尝试） GSSAPIAuthentication no Port 54322 X11Forwarding no PrintMotd no # 由PAM管理，ssh关闭 # AllowUsers ops admin # 限制认证尝试次数与超时时间（防暴力破解） MaxAuthTries 3 LoginGraceTime 20 # 启用公钥认证，彻底禁用密码认证 PubkeyAuthentication yes PasswordAuthentication no ChallengeResponseAuthentication no KbdInteractiveAuthentication no # 单个连接最大通道数（根据业务需求调整） MaxSessions 10 # 未认证连接限制：50个开始随机拒绝，达到200个全拒绝（防慢速 DoS） MaxStartups 50:30:200 # 心跳保活：每 60 秒发送一次，连续 5 次无响应则断开（清理僵尸连接） ClientAliveInterval 60 ClientAliveCountMax 5 # 测试配置是否正确 sshd -t systemctl restart ssh 5、精简服务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 # 一个容器包服务，跟apt一样还吃资源 systemctl disable --now snapd.service snapd.socket apt purge -y snapd rm -rf /snap /var/snap /var/lib/snapd # 硬件相关关掉 # multipathd（多路径设备管理） systemctl disable --now multipathd.service multipathd.socket # open-iscsi（iSCSI 远程存储） systemctl disable --now open-iscsi.service iscsid.service iscsid.socket systemctl mask open-iscsi.service iscsid.service iscsid.socket # udisks2（磁盘热插拔/自动挂载守护） systemctl disable --now udisks2.service # fwupd（固件更新守护） systemctl disable --now fwupd.service fwupd-refresh.timer # tuned：性能调优守护，对 2G 小机器意义不大 systemctl disable --now tuned.service # packagekit 是桌面环境的包管理抽象层，服务器上用 apt 直接管理即可。atd 是一次性定时任务，如果你不需要 at 命令，可以关掉。 systemctl disable --now packagekit.service systemctl mask packagekit.service # mask 会阻止任何服务通过依赖关系把它重新拉起。 systemctl disable --now atd.service # getty@tty1 和 serial-getty@ttyS0 是本地登录终端和串口控制台。 systemctl disable --now getty@tty1.service systemctl disable --now serial-getty@ttyS0.service # 更新关闭 systemctl disable --now unattended-upgrades.service # 如果进程还在，直接杀掉 pkill -f unattended-upgrade-sh # 移除错误上报和崩溃报告 systemctl disable --now whoopsie apport apt purge -y whoopsie apport popularity-contest systemctl list-units --type=service --state=running 6、配置motd 1 2 3 sed -i \u0026#39;s/^ENABLED=.*/ENABLED=0/\u0026#39; /etc/default/motd-news # 新闻 chmod -x /etc/update-motd.d/* vim /etc/motd 7、中文支持（如果vim或系统乱码就执行） 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 # 安装中文语言包（提供 zh_CN.UTF-8 的生成素材） apt update \u0026amp;\u0026amp; apt install -y language-pack-zh-hans language-pack-zh-hans-base # 同时生成中文和英文 UTF-8 locale（英文用于保证 TTY 兼容性） locale-gen en_US.UTF-8 locale-gen zh_CN.UTF-8 update-locale --reset LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh grep -nE \u0026#39;UsePAM|AcceptEnv\u0026#39; /etc/ssh/sshd_config #看ssh打开这两个配置没PAM yes cat /etc/default/locale locale # 看配置应用没 locale -a locale charmap # 如果vim还有问题就执行 cat \u0026gt; ~/.vimrc \u0026lt;\u0026lt;\u0026#39;EOF\u0026#39; \u0026#34; UTF-8 encoding set encoding=utf-8 set fileencoding=utf-8 set fileencodings=utf-8,gb18030,gbk,gb2312 \u0026#34; Display set number set cursorline set wrap \u0026#34; Search set hlsearch set incsearch \u0026#34; Indentation set tabstop=4 set shiftwidth=4 set expandtab \u0026#34; Terminal compatibility set termguicolors EOF ","date":"2024-07-11T00:00:00+08:00","permalink":"/p/test1/","title":"Test0"},{"content":"一、服务器环境初始化 1、检查 1 2 3 4 5 6 7 8 9 cat /etc/os-release \u0026amp;\u0026amp; uname -r lscpu | grep -E \u0026#39;Model name|CPU\\\\(s\\\\)|Core|Thread\u0026#39; \u0026amp;\u0026amp; free -h df -hT \u0026amp;\u0026amp; lsblk systemctl list-units --type=service --state=running systemctl list-unit-files --type=service --state=enabled sudo ss -tulnp sudo sshd -T | grep -E \u0026#39;^(port|permitrootlogin|passwordauthentication|pubkeyauthentication|maxauthtries|clientaliveinterval|clientalivecountmax)\u0026#39; ulimit -n \u0026amp;\u0026amp; ulimit -u \u0026amp;\u0026amp; cat /proc/sys/fs/file-max \u0026amp;\u0026amp; cat /proc/sys/fs/file-nr swapon --show \u0026amp;\u0026amp; sysctl vm.swappiness net.core.somaxconn 2、创建swap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 创建swap文件 fallocate -l 2G /swapfile chmod 600 /swapfile mkswap /swapfile swapon /swapfile echo \u0026#39;/swapfile none swap sw 0 0\u0026#39; \u0026gt;\u0026gt; tee -a /etc/fstab tee /etc/sysctl.d/99-swap.conf \u0026lt;\u0026lt;\u0026#39;EOF\u0026#39; # Swap usage preference vm.swappiness = 10 EOF # vm.swappiness=10 表示降低内核主动使用 Swap 的倾向，但不代表只有内存耗尽时才使用 Swap。 sysctl --system # 加载配置 free -h 3、apt优化，软件安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # 备份原 sources.list cp /etc/apt/sources.list /etc/apt/sources.list.bak # 替换为阿里云镜像（Ubuntu） sed -i \u0026#39;s@http://.*.ubuntu.com@http://mirrors.aliyun.com@g\u0026#39; /etc/apt/sources.list # 更新并升级 apt update \u0026amp;\u0026amp; sudo apt upgrade -y # 清理无用依赖和缓存 apt autoremove --purge -y apt clean # 安装所需服务 apt -y install lrzsz 4、ssh配置 4.1、本机创建key 1 2 3 win+r cmd ssh-keygen -t ed25519 4.2、上传服务器 1 2 3 4 cd /root/.ssh rz chmod 600 ./* cat id_ed25519.pub \u0026gt;\u0026gt; authorized_keys 4.3、ssh优化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 vim /etc/ssh/sshd_config # 关闭 DNS 反向解析（避免客户端 IP 解析域名时因 DNS 响应慢导致卡顿） UseDNS no UsePAM yes # PAM配置需要 # 关闭 GSSAPI 认证（非域环境无用，关闭可减少 PAM 开销和 Kerberos 尝试） GSSAPIAuthentication no Port 54322 X11Forwarding no PrintMotd no # 由PAM管理，ssh关闭 # AllowUsers ops admin # 限制认证尝试次数与超时时间（防暴力破解） MaxAuthTries 3 LoginGraceTime 20 # 启用公钥认证，彻底禁用密码认证 PubkeyAuthentication yes PasswordAuthentication no ChallengeResponseAuthentication no KbdInteractiveAuthentication no # 单个连接最大通道数（根据业务需求调整） MaxSessions 10 # 未认证连接限制：50个开始随机拒绝，达到200个全拒绝（防慢速 DoS） MaxStartups 50:30:200 # 心跳保活：每 60 秒发送一次，连续 5 次无响应则断开（清理僵尸连接） ClientAliveInterval 60 ClientAliveCountMax 5 # 测试配置是否正确 sshd -t systemctl restart ssh 5、精简服务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 # 一个容器包服务，跟apt一样还吃资源 systemctl disable --now snapd.service snapd.socket apt purge -y snapd rm -rf /snap /var/snap /var/lib/snapd # 硬件相关关掉 # multipathd（多路径设备管理） systemctl disable --now multipathd.service multipathd.socket # open-iscsi（iSCSI 远程存储） systemctl disable --now open-iscsi.service iscsid.service iscsid.socket systemctl mask open-iscsi.service iscsid.service iscsid.socket # udisks2（磁盘热插拔/自动挂载守护） systemctl disable --now udisks2.service # fwupd（固件更新守护） systemctl disable --now fwupd.service fwupd-refresh.timer # tuned：性能调优守护，对 2G 小机器意义不大 systemctl disable --now tuned.service # packagekit 是桌面环境的包管理抽象层，服务器上用 apt 直接管理即可。atd 是一次性定时任务，如果你不需要 at 命令，可以关掉。 systemctl disable --now packagekit.service systemctl mask packagekit.service # mask 会阻止任何服务通过依赖关系把它重新拉起。 systemctl disable --now atd.service # getty@tty1 和 serial-getty@ttyS0 是本地登录终端和串口控制台。 systemctl disable --now getty@tty1.service systemctl disable --now serial-getty@ttyS0.service # 更新关闭 systemctl disable --now unattended-upgrades.service # 如果进程还在，直接杀掉 pkill -f unattended-upgrade-sh # 移除错误上报和崩溃报告 systemctl disable --now whoopsie apport apt purge -y whoopsie apport popularity-contest systemctl list-units --type=service --state=running 6、配置motd 1 2 3 sed -i \u0026#39;s/^ENABLED=.*/ENABLED=0/\u0026#39; /etc/default/motd-news # 新闻 chmod -x /etc/update-motd.d/* vim /etc/motd 7、中文支持（如果vim或系统乱码就执行） 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 # 安装中文语言包（提供 zh_CN.UTF-8 的生成素材） apt update \u0026amp;\u0026amp; apt install -y language-pack-zh-hans language-pack-zh-hans-base # 同时生成中文和英文 UTF-8 locale（英文用于保证 TTY 兼容性） locale-gen en_US.UTF-8 locale-gen zh_CN.UTF-8 update-locale --reset LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh grep -nE \u0026#39;UsePAM|AcceptEnv\u0026#39; /etc/ssh/sshd_config #看ssh打开这两个配置没PAM yes cat /etc/default/locale locale # 看配置应用没 locale -a locale charmap # 如果vim还有问题就执行 cat \u0026gt; ~/.vimrc \u0026lt;\u0026lt;\u0026#39;EOF\u0026#39; \u0026#34; UTF-8 encoding set encoding=utf-8 set fileencoding=utf-8 set fileencodings=utf-8,gb18030,gbk,gb2312 \u0026#34; Display set number set cursorline set wrap \u0026#34; Search set hlsearch set incsearch \u0026#34; Indentation set tabstop=4 set shiftwidth=4 set expandtab \u0026#34; Terminal compatibility set termguicolors EOF ","date":"2023-07-11T00:00:00+08:00","permalink":"/p/test1/","title":"Test0"},{"content":"一、服务器环境初始化 1、检查 1 2 3 4 5 6 7 8 9 cat /etc/os-release \u0026amp;\u0026amp; uname -r lscpu | grep -E \u0026#39;Model name|CPU\\\\(s\\\\)|Core|Thread\u0026#39; \u0026amp;\u0026amp; free -h df -hT \u0026amp;\u0026amp; lsblk systemctl list-units --type=service --state=running systemctl list-unit-files --type=service --state=enabled sudo ss -tulnp sudo sshd -T | grep -E \u0026#39;^(port|permitrootlogin|passwordauthentication|pubkeyauthentication|maxauthtries|clientaliveinterval|clientalivecountmax)\u0026#39; ulimit -n \u0026amp;\u0026amp; ulimit -u \u0026amp;\u0026amp; cat /proc/sys/fs/file-max \u0026amp;\u0026amp; cat /proc/sys/fs/file-nr swapon --show \u0026amp;\u0026amp; sysctl vm.swappiness net.core.somaxconn 2、创建swap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 创建swap文件 fallocate -l 2G /swapfile chmod 600 /swapfile mkswap /swapfile swapon /swapfile echo \u0026#39;/swapfile none swap sw 0 0\u0026#39; \u0026gt;\u0026gt; tee -a /etc/fstab tee /etc/sysctl.d/99-swap.conf \u0026lt;\u0026lt;\u0026#39;EOF\u0026#39; # Swap usage preference vm.swappiness = 10 EOF # vm.swappiness=10 表示降低内核主动使用 Swap 的倾向，但不代表只有内存耗尽时才使用 Swap。 sysctl --system # 加载配置 free -h 3、apt优化，软件安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # 备份原 sources.list cp /etc/apt/sources.list /etc/apt/sources.list.bak # 替换为阿里云镜像（Ubuntu） sed -i \u0026#39;s@http://.*.ubuntu.com@http://mirrors.aliyun.com@g\u0026#39; /etc/apt/sources.list # 更新并升级 apt update \u0026amp;\u0026amp; sudo apt upgrade -y # 清理无用依赖和缓存 apt autoremove --purge -y apt clean # 安装所需服务 apt -y install lrzsz 4、ssh配置 4.1、本机创建key 1 2 3 win+r cmd ssh-keygen -t ed25519 4.2、上传服务器 1 2 3 4 cd /root/.ssh rz chmod 600 ./* cat id_ed25519.pub \u0026gt;\u0026gt; authorized_keys 4.3、ssh优化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 vim /etc/ssh/sshd_config # 关闭 DNS 反向解析（避免客户端 IP 解析域名时因 DNS 响应慢导致卡顿） UseDNS no UsePAM yes # PAM配置需要 # 关闭 GSSAPI 认证（非域环境无用，关闭可减少 PAM 开销和 Kerberos 尝试） GSSAPIAuthentication no Port 54322 X11Forwarding no PrintMotd no # 由PAM管理，ssh关闭 # AllowUsers ops admin # 限制认证尝试次数与超时时间（防暴力破解） MaxAuthTries 3 LoginGraceTime 20 # 启用公钥认证，彻底禁用密码认证 PubkeyAuthentication yes PasswordAuthentication no ChallengeResponseAuthentication no KbdInteractiveAuthentication no # 单个连接最大通道数（根据业务需求调整） MaxSessions 10 # 未认证连接限制：50个开始随机拒绝，达到200个全拒绝（防慢速 DoS） MaxStartups 50:30:200 # 心跳保活：每 60 秒发送一次，连续 5 次无响应则断开（清理僵尸连接） ClientAliveInterval 60 ClientAliveCountMax 5 # 测试配置是否正确 sshd -t systemctl restart ssh 5、精简服务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 # 一个容器包服务，跟apt一样还吃资源 systemctl disable --now snapd.service snapd.socket apt purge -y snapd rm -rf /snap /var/snap /var/lib/snapd # 硬件相关关掉 # multipathd（多路径设备管理） systemctl disable --now multipathd.service multipathd.socket # open-iscsi（iSCSI 远程存储） systemctl disable --now open-iscsi.service iscsid.service iscsid.socket systemctl mask open-iscsi.service iscsid.service iscsid.socket # udisks2（磁盘热插拔/自动挂载守护） systemctl disable --now udisks2.service # fwupd（固件更新守护） systemctl disable --now fwupd.service fwupd-refresh.timer # tuned：性能调优守护，对 2G 小机器意义不大 systemctl disable --now tuned.service # packagekit 是桌面环境的包管理抽象层，服务器上用 apt 直接管理即可。atd 是一次性定时任务，如果你不需要 at 命令，可以关掉。 systemctl disable --now packagekit.service systemctl mask packagekit.service # mask 会阻止任何服务通过依赖关系把它重新拉起。 systemctl disable --now atd.service # getty@tty1 和 serial-getty@ttyS0 是本地登录终端和串口控制台。 systemctl disable --now getty@tty1.service systemctl disable --now serial-getty@ttyS0.service # 更新关闭 systemctl disable --now unattended-upgrades.service # 如果进程还在，直接杀掉 pkill -f unattended-upgrade-sh # 移除错误上报和崩溃报告 systemctl disable --now whoopsie apport apt purge -y whoopsie apport popularity-contest systemctl list-units --type=service --state=running 6、配置motd 1 2 3 sed -i \u0026#39;s/^ENABLED=.*/ENABLED=0/\u0026#39; /etc/default/motd-news # 新闻 chmod -x /etc/update-motd.d/* vim /etc/motd 7、中文支持（如果vim或系统乱码就执行） 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 # 安装中文语言包（提供 zh_CN.UTF-8 的生成素材） apt update \u0026amp;\u0026amp; apt install -y language-pack-zh-hans language-pack-zh-hans-base # 同时生成中文和英文 UTF-8 locale（英文用于保证 TTY 兼容性） locale-gen en_US.UTF-8 locale-gen zh_CN.UTF-8 update-locale --reset LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh grep -nE \u0026#39;UsePAM|AcceptEnv\u0026#39; /etc/ssh/sshd_config #看ssh打开这两个配置没PAM yes cat /etc/default/locale locale # 看配置应用没 locale -a locale charmap # 如果vim还有问题就执行 cat \u0026gt; ~/.vimrc \u0026lt;\u0026lt;\u0026#39;EOF\u0026#39; \u0026#34; UTF-8 encoding set encoding=utf-8 set fileencoding=utf-8 set fileencodings=utf-8,gb18030,gbk,gb2312 \u0026#34; Display set number set cursorline set wrap \u0026#34; Search set hlsearch set incsearch \u0026#34; Indentation set tabstop=4 set shiftwidth=4 set expandtab \u0026#34; Terminal compatibility set termguicolors EOF ","date":"2022-07-11T00:00:00+08:00","permalink":"/p/test1/","title":"Test0"},{"content":"一、服务器环境初始化 1、检查 1 2 3 4 5 6 7 8 9 cat /etc/os-release \u0026amp;\u0026amp; uname -r lscpu | grep -E \u0026#39;Model name|CPU\\\\(s\\\\)|Core|Thread\u0026#39; \u0026amp;\u0026amp; free -h df -hT \u0026amp;\u0026amp; lsblk systemctl list-units --type=service --state=running systemctl list-unit-files --type=service --state=enabled sudo ss -tulnp sudo sshd -T | grep -E \u0026#39;^(port|permitrootlogin|passwordauthentication|pubkeyauthentication|maxauthtries|clientaliveinterval|clientalivecountmax)\u0026#39; ulimit -n \u0026amp;\u0026amp; ulimit -u \u0026amp;\u0026amp; cat /proc/sys/fs/file-max \u0026amp;\u0026amp; cat /proc/sys/fs/file-nr swapon --show \u0026amp;\u0026amp; sysctl vm.swappiness net.core.somaxconn 2、创建swap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 创建swap文件 fallocate -l 2G /swapfile chmod 600 /swapfile mkswap /swapfile swapon /swapfile echo \u0026#39;/swapfile none swap sw 0 0\u0026#39; \u0026gt;\u0026gt; tee -a /etc/fstab tee /etc/sysctl.d/99-swap.conf \u0026lt;\u0026lt;\u0026#39;EOF\u0026#39; # Swap usage preference vm.swappiness = 10 EOF # vm.swappiness=10 表示降低内核主动使用 Swap 的倾向，但不代表只有内存耗尽时才使用 Swap。 sysctl --system # 加载配置 free -h 3、apt优化，软件安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # 备份原 sources.list cp /etc/apt/sources.list /etc/apt/sources.list.bak # 替换为阿里云镜像（Ubuntu） sed -i \u0026#39;s@http://.*.ubuntu.com@http://mirrors.aliyun.com@g\u0026#39; /etc/apt/sources.list # 更新并升级 apt update \u0026amp;\u0026amp; sudo apt upgrade -y # 清理无用依赖和缓存 apt autoremove --purge -y apt clean # 安装所需服务 apt -y install lrzsz 4、ssh配置 4.1、本机创建key 1 2 3 win+r cmd ssh-keygen -t ed25519 4.2、上传服务器 1 2 3 4 cd /root/.ssh rz chmod 600 ./* cat id_ed25519.pub \u0026gt;\u0026gt; authorized_keys 4.3、ssh优化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 vim /etc/ssh/sshd_config # 关闭 DNS 反向解析（避免客户端 IP 解析域名时因 DNS 响应慢导致卡顿） UseDNS no UsePAM yes # PAM配置需要 # 关闭 GSSAPI 认证（非域环境无用，关闭可减少 PAM 开销和 Kerberos 尝试） GSSAPIAuthentication no Port 54322 X11Forwarding no PrintMotd no # 由PAM管理，ssh关闭 # AllowUsers ops admin # 限制认证尝试次数与超时时间（防暴力破解） MaxAuthTries 3 LoginGraceTime 20 # 启用公钥认证，彻底禁用密码认证 PubkeyAuthentication yes PasswordAuthentication no ChallengeResponseAuthentication no KbdInteractiveAuthentication no # 单个连接最大通道数（根据业务需求调整） MaxSessions 10 # 未认证连接限制：50个开始随机拒绝，达到200个全拒绝（防慢速 DoS） MaxStartups 50:30:200 # 心跳保活：每 60 秒发送一次，连续 5 次无响应则断开（清理僵尸连接） ClientAliveInterval 60 ClientAliveCountMax 5 # 测试配置是否正确 sshd -t systemctl restart ssh 5、精简服务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 # 一个容器包服务，跟apt一样还吃资源 systemctl disable --now snapd.service snapd.socket apt purge -y snapd rm -rf /snap /var/snap /var/lib/snapd # 硬件相关关掉 # multipathd（多路径设备管理） systemctl disable --now multipathd.service multipathd.socket # open-iscsi（iSCSI 远程存储） systemctl disable --now open-iscsi.service iscsid.service iscsid.socket systemctl mask open-iscsi.service iscsid.service iscsid.socket # udisks2（磁盘热插拔/自动挂载守护） systemctl disable --now udisks2.service # fwupd（固件更新守护） systemctl disable --now fwupd.service fwupd-refresh.timer # tuned：性能调优守护，对 2G 小机器意义不大 systemctl disable --now tuned.service # packagekit 是桌面环境的包管理抽象层，服务器上用 apt 直接管理即可。atd 是一次性定时任务，如果你不需要 at 命令，可以关掉。 systemctl disable --now packagekit.service systemctl mask packagekit.service # mask 会阻止任何服务通过依赖关系把它重新拉起。 systemctl disable --now atd.service # getty@tty1 和 serial-getty@ttyS0 是本地登录终端和串口控制台。 systemctl disable --now getty@tty1.service systemctl disable --now serial-getty@ttyS0.service # 更新关闭 systemctl disable --now unattended-upgrades.service # 如果进程还在，直接杀掉 pkill -f unattended-upgrade-sh # 移除错误上报和崩溃报告 systemctl disable --now whoopsie apport apt purge -y whoopsie apport popularity-contest systemctl list-units --type=service --state=running 6、配置motd 1 2 3 sed -i \u0026#39;s/^ENABLED=.*/ENABLED=0/\u0026#39; /etc/default/motd-news # 新闻 chmod -x /etc/update-motd.d/* vim /etc/motd 7、中文支持（如果vim或系统乱码就执行） 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 # 安装中文语言包（提供 zh_CN.UTF-8 的生成素材） apt update \u0026amp;\u0026amp; apt install -y language-pack-zh-hans language-pack-zh-hans-base # 同时生成中文和英文 UTF-8 locale（英文用于保证 TTY 兼容性） locale-gen en_US.UTF-8 locale-gen zh_CN.UTF-8 update-locale --reset LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh grep -nE \u0026#39;UsePAM|AcceptEnv\u0026#39; /etc/ssh/sshd_config #看ssh打开这两个配置没PAM yes cat /etc/default/locale locale # 看配置应用没 locale -a locale charmap # 如果vim还有问题就执行 cat \u0026gt; ~/.vimrc \u0026lt;\u0026lt;\u0026#39;EOF\u0026#39; \u0026#34; UTF-8 encoding set encoding=utf-8 set fileencoding=utf-8 set fileencodings=utf-8,gb18030,gbk,gb2312 \u0026#34; Display set number set cursorline set wrap \u0026#34; Search set hlsearch set incsearch \u0026#34; Indentation set tabstop=4 set shiftwidth=4 set expandtab \u0026#34; Terminal compatibility set termguicolors EOF ","date":"2021-07-11T00:00:00+08:00","permalink":"/p/test1/","title":"Test0"},{"content":"新的开始 今天，「星 ＆ 晨 | Stars」正式开启建设。\n搭建这个博客，是希望拥有一个属于自己的空间，记录技术学习过程，也收藏日常生活中的一些美好瞬间。\n1 2 3 4 5 6 7 8 cdjcjaklcjkasjcascsak;lcjas;kcjsakc cd ssh ls cat sadaskdjaskldjkasjdas sjdkasdklasdjasdsa 未来计划 记录技术学习笔记 分享项目实践经验 完善博客功能与视觉设计 记录生活中的点滴 愿星光与晨曦，见证每一次成长。\n","date":"2018-07-10T00:00:00+08:00","permalink":"/p/hello-stars/","title":"你好，星辰"},{"content":"一、服务器环境初始化 1、检查 1 2 3 4 5 6 7 8 9 cat /etc/os-release \u0026amp;\u0026amp; uname -r lscpu | grep -E \u0026#39;Model name|CPU\\\\(s\\\\)|Core|Thread\u0026#39; \u0026amp;\u0026amp; free -h df -hT \u0026amp;\u0026amp; lsblk systemctl list-units --type=service --state=running systemctl list-unit-files --type=service --state=enabled sudo ss -tulnp sudo sshd -T | grep -E \u0026#39;^(port|permitrootlogin|passwordauthentication|pubkeyauthentication|maxauthtries|clientaliveinterval|clientalivecountmax)\u0026#39; ulimit -n \u0026amp;\u0026amp; ulimit -u \u0026amp;\u0026amp; cat /proc/sys/fs/file-max \u0026amp;\u0026amp; cat /proc/sys/fs/file-nr swapon --show \u0026amp;\u0026amp; sysctl vm.swappiness net.core.somaxconn 2、创建swap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 创建swap文件 fallocate -l 2G /swapfile chmod 600 /swapfile mkswap /swapfile swapon /swapfile echo \u0026#39;/swapfile none swap sw 0 0\u0026#39; \u0026gt;\u0026gt; tee -a /etc/fstab tee /etc/sysctl.d/99-swap.conf \u0026lt;\u0026lt;\u0026#39;EOF\u0026#39; # Swap usage preference vm.swappiness = 10 EOF # vm.swappiness=10 表示降低内核主动使用 Swap 的倾向，但不代表只有内存耗尽时才使用 Swap。 sysctl --system # 加载配置 free -h 3、apt优化，软件安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # 备份原 sources.list cp /etc/apt/sources.list /etc/apt/sources.list.bak # 替换为阿里云镜像（Ubuntu） sed -i \u0026#39;s@http://.*.ubuntu.com@http://mirrors.aliyun.com@g\u0026#39; /etc/apt/sources.list # 更新并升级 apt update \u0026amp;\u0026amp; sudo apt upgrade -y # 清理无用依赖和缓存 apt autoremove --purge -y apt clean # 安装所需服务 apt -y install lrzsz 4、ssh配置 4.1、本机创建key 1 2 3 win+r cmd ssh-keygen -t ed25519 4.2、上传服务器 1 2 3 4 cd /root/.ssh rz chmod 600 ./* cat id_ed25519.pub \u0026gt;\u0026gt; authorized_keys 4.3、ssh优化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 vim /etc/ssh/sshd_config # 关闭 DNS 反向解析（避免客户端 IP 解析域名时因 DNS 响应慢导致卡顿） UseDNS no UsePAM yes # PAM配置需要 # 关闭 GSSAPI 认证（非域环境无用，关闭可减少 PAM 开销和 Kerberos 尝试） GSSAPIAuthentication no Port 54322 X11Forwarding no PrintMotd no # 由PAM管理，ssh关闭 # AllowUsers ops admin # 限制认证尝试次数与超时时间（防暴力破解） MaxAuthTries 3 LoginGraceTime 20 # 启用公钥认证，彻底禁用密码认证 PubkeyAuthentication yes PasswordAuthentication no ChallengeResponseAuthentication no KbdInteractiveAuthentication no # 单个连接最大通道数（根据业务需求调整） MaxSessions 10 # 未认证连接限制：50个开始随机拒绝，达到200个全拒绝（防慢速 DoS） MaxStartups 50:30:200 # 心跳保活：每 60 秒发送一次，连续 5 次无响应则断开（清理僵尸连接） ClientAliveInterval 60 ClientAliveCountMax 5 # 测试配置是否正确 sshd -t systemctl restart ssh 5、精简服务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 # 一个容器包服务，跟apt一样还吃资源 systemctl disable --now snapd.service snapd.socket apt purge -y snapd rm -rf /snap /var/snap /var/lib/snapd # 硬件相关关掉 # multipathd（多路径设备管理） systemctl disable --now multipathd.service multipathd.socket # open-iscsi（iSCSI 远程存储） systemctl disable --now open-iscsi.service iscsid.service iscsid.socket systemctl mask open-iscsi.service iscsid.service iscsid.socket # udisks2（磁盘热插拔/自动挂载守护） systemctl disable --now udisks2.service # fwupd（固件更新守护） systemctl disable --now fwupd.service fwupd-refresh.timer # tuned：性能调优守护，对 2G 小机器意义不大 systemctl disable --now tuned.service # packagekit 是桌面环境的包管理抽象层，服务器上用 apt 直接管理即可。atd 是一次性定时任务，如果你不需要 at 命令，可以关掉。 systemctl disable --now packagekit.service systemctl mask packagekit.service # mask 会阻止任何服务通过依赖关系把它重新拉起。 systemctl disable --now atd.service # getty@tty1 和 serial-getty@ttyS0 是本地登录终端和串口控制台。 systemctl disable --now getty@tty1.service systemctl disable --now serial-getty@ttyS0.service # 更新关闭 systemctl disable --now unattended-upgrades.service # 如果进程还在，直接杀掉 pkill -f unattended-upgrade-sh # 移除错误上报和崩溃报告 systemctl disable --now whoopsie apport apt purge -y whoopsie apport popularity-contest systemctl list-units --type=service --state=running 6、配置motd 1 2 3 sed -i \u0026#39;s/^ENABLED=.*/ENABLED=0/\u0026#39; /etc/default/motd-news # 新闻 chmod -x /etc/update-motd.d/* vim /etc/motd 7、中文支持（如果vim或系统乱码就执行） 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 # 安装中文语言包（提供 zh_CN.UTF-8 的生成素材） apt update \u0026amp;\u0026amp; apt install -y language-pack-zh-hans language-pack-zh-hans-base # 同时生成中文和英文 UTF-8 locale（英文用于保证 TTY 兼容性） locale-gen en_US.UTF-8 locale-gen zh_CN.UTF-8 update-locale --reset LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh grep -nE \u0026#39;UsePAM|AcceptEnv\u0026#39; /etc/ssh/sshd_config #看ssh打开这两个配置没PAM yes cat /etc/default/locale locale # 看配置应用没 locale -a locale charmap # 如果vim还有问题就执行 cat \u0026gt; ~/.vimrc \u0026lt;\u0026lt;\u0026#39;EOF\u0026#39; \u0026#34; UTF-8 encoding set encoding=utf-8 set fileencoding=utf-8 set fileencodings=utf-8,gb18030,gbk,gb2312 \u0026#34; Display set number set cursorline set wrap \u0026#34; Search set hlsearch set incsearch \u0026#34; Indentation set tabstop=4 set shiftwidth=4 set expandtab \u0026#34; Terminal compatibility set termguicolors EOF ","date":"2010-07-11T00:00:00+08:00","permalink":"/p/test1/","title":"Test0"}]