别再让VPS裸奔!保姆级Linux安全加固教程,彻底告别SSH暴力破解
<div class="ce-block" data-id="fIMM3eNnHH" ><div class="ce-block__content" style="">
<h3 class="ce-header ce-header--left">再让VPS裸奔!保姆级Linux安全加固教程,彻底告别SSH暴力破解</h3>
</div>
</div><div class="ce-block" data-id="41ByBOIUFY" >
<div class="ce-block__content" style="">
<h4 class="ce-header ce-header--left">▎ 前言:为什么要折腾这个?</h4>
</div>
</div><div class="ce-block ce-block--focused" data-id="pf-DVJ6Snf" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left">我们先做一个测试:</div>
</div>
</div><div class="ce-block ce-block--focused" data-id="XyqpyGI-H_" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left">在你的Linux控制台执行:<code>grep "Failed password" /var/log/auth.log</code></div>
</div>
</div><div class="ce-block ce-block--focused" data-id="hjpM0mYaZh" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left">你应该能看到一大片的<span> </span><code>Failed password</code>的错误</div>
</div>
</div><div class="ce-block ce-block--focused" data-id="2QFqJ4Evyr" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left">不同发行版可能命令不一样,这里通过<code>Debian12</code><span> </span>做演示,这条命令可能刷屏,谨慎执行。</div>
</div>
</div><div class="ce-block ce-block--focused" data-id="L5oyjO4VG3" >
<div class="ce-block__content">
<div class="cdx-block image-tool image-tool--filled ">
<div class="image-tool__image">
<div class="image-tool__image-preloader" style=""></div>
<img id="aimg_L5oyjO4VG3" class="image-tool__image-picture _zoom" src="http://idceve.com/data/attachment/forum/202609/06/180336vk97s78k9tkp55gg.png" title="" alt="" data-aid="153"/>
</div>
<div class="cdx-input image-tool__caption" data-placeholder=""></div>
</div>
</div>
</div><div class="ce-block ce-block--focused" data-id="L24EIAKMkq" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left"><b>问题描述:</b>刚买的 VPS 日志里全是 Failed password?这是全网脚本在扫描默认 22 端口。因为你的服务器默认是有公网IP的,在公网的环境下无时无刻不能其他人访问或者恶意攻击,所以你的SSH端口无时无刻都有人尝试去登入扫描。</div>
</div>
</div><div class="ce-block ce-block--focused" data-id="vm5hAds0LY" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left"><font style="color: rgb(255, 19, 0);">风险提示:如果你的SSH是弱密码请尽快更改,或者往下看跟着教程走。不然被不明人员攻破你的服务器,,服务器变“肉鸡”(挖矿、DDoS),商家删机/关机都是小事,如果做非法业务受牵连就得不偿失了。</font></div>
</div>
</div><div class="ce-block" data-id="rMluTRnj8p" >
<div class="ce-block__content" style="">
<h4 class="ce-header ce-header--left">▎ 前期准备工作</h4>
</div>
</div><div class="ce-block ce-block--focused" data-id="2hYi8awXXu" >
<div class="ce-block__content">
<ul class="cdx-list-unordered"><li class="cdx-list__item"><b>环境:</b>一台刚开通的 VPS(Debian/Ubuntu/CentOS),我这里演示的操作系统以<code>Debian12</code>,虽然Debian13也出来了,但是还是习惯使用Debian12,作为我的主力使用的操作系统。</li><li class="cdx-list__item"><b>链接SSH的工具</b>:SSH 客户端(Xshell, XTerminal, FinalShell),三选其一就可以,我这里放上了<a href="https://www.xterminal.cn/" rel="noopener nofollow ugc">XTerminal</a>的下载链接。</li></ul>
</div>
</div><div class="ce-block ce-block--focused" data-id="FMLPkmJBrg" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left"><font style="color: rgb(255, 19, 0);">注意!:在所有设置确认成功前,绝对不要关闭当前的 SSH 连接窗口!,否则使用商家的VNC链接或者只能重装系统了。</font></div>
</div>
</div><div class="ce-block" data-id="mqBPgoiTUO" >
<div class="ce-block__content" style="">
<h4 class="ce-header ce-header--left">▎ 第一步 更新软件源和同步时间</h4>
</div>
</div><div class="ce-block ce-block--focused" data-id="0wRp43u2PX" >
<div class="ce-block__content">
<ol class="cdx-list-ordered" style="list-style-type:none; margin-left: -15px;"><li class="cdx-list__item">1. 为什么需要更新软件源呢?只是为了下载最新的软件,不然软件是旧版本可能存在一直漏洞,这样就有一点本末倒置了,所以第一步先更新软件源和系统软件,升级到最新版本。</li></ol>
</div>
</div><div class="ce-block ce-block--focused" data-id="PFVE6UAJy0" >
<div class="ce-block__content">
<ul class="cdx-list-unordered"><li class="cdx-list__item">Debian/Ubuntu:<span> </span><code>apt update && apt upgrade -y</code></li></ul>
</div>
</div><div class="ce-block ce-block--focused" data-id="lRrniEsI_h" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left"><font style="color: rgb(0, 112, 255);">有的时候会无法安装软件,也可以先更新软件源。</font></div>
</div>
</div><div class="ce-block ce-block--focused" data-id="0LMEqBU4Or" >
<div class="ce-block__content">
<ol class="cdx-list-ordered" style="list-style-type:none; margin-left: -15px;"><li class="cdx-list__item">1. 同步服务器时间(因为是国内时间,我习惯同步到上海时间,这一步不是必须的)</li></ol>
</div>
</div><div class="ce-block ce-block--focused" data-id="MgLK45Gp-y" >
<div class="ce-block__content">
<ul class="cdx-list-unordered"><li class="cdx-list__item"><b>校准时间:</b>将服务器时间设置为上海时间,方便后续看日志排查问题。<ul class="cdx-list__item-children" class="cdx-list-unordered"><li class="cdx-list__item">命令:<code>timedatectl set-timezone Asia/Shanghai</code></li></ul></li></ul>
</div>
</div><div class="ce-block" data-id="oNZ19K71A2" >
<div class="ce-block__content" style="">
<h4 class="ce-header ce-header--left">▎ 第二步:创建普通管理员账号(避免 Root 裸奔,这里是强烈建议的,如果觉得麻烦可以不做)</h4>
</div>
</div><div class="ce-block ce-block--focused" data-id="vdzty732-R" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left">Root用户权限最高,也是最方便的,所以很多人喜欢直接使用Root用户执行命令和运行程序。但是风险也是最高的,如果弱密码被破解或者秘钥被泄露,导致的结果也是最原则的,因为人家已经拥有了Root权限(进你的服务器如无人之境)。</div>
</div>
</div><div class="ce-block ce-block--focused" data-id="NWYJ3QzDl7" >
<div class="ce-block__content">
<ol class="cdx-list-ordered" style="list-style-type:none; margin-left: -15px;"><li class="cdx-list__item">1. <b>新建用户</b>:<code>adduser myadmin</code><span> </span>(名字自定义)。</li><li class="cdx-list__item">2. <b>赋予权限</b>:<code>usermod -aG sudo myadmin</code>。</li></ol>
</div>
</div><div class="ce-block ce-block--focused" data-id="ZlZfOw-t73" >
<div class="ce-block__content">
<ul class="cdx-list-unordered"><li class="cdx-list__item"><b>验证</b>:退出 Root,用新用户登录并测试<span> </span><code>sudo apt update</code>,确保权限生效。</li></ul>
</div>
</div><div class="ce-block ce-block--focused" data-id="N_rBiPPXLP" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left">sudo命令就是把普通用户的权限提权成root权限执行,在平时权限不够的时候可以使用。</div>
</div>
</div><div class="ce-block" data-id="Ybl-erbhs3" >
<div class="ce-block__content" style="">
<h4 class="ce-header ce-header--left">▎ 第三步:配置 SSH 密钥登录(给大门换把锁)</h4>
</div>
</div><div class="ce-block ce-block--focused" data-id="eFZI1aJtEg" >
<div class="ce-block__content">
<ul class="cdx-list-unordered"><li class="cdx-list__item"><b>生成密钥对:</b><ul class="cdx-list__item-children" class="cdx-list-unordered"><li class="cdx-list__item">推荐算法:使用 Ed25519 算法(比 RSA 更安全高效)。</li><li class="cdx-list__item">命令:<code>ssh-keygen -t ed25519 -C "your_email"</code>。</li></ul></li><li class="cdx-list__item"><b>上传公钥到服务器:</b><ul class="cdx-list__item-children" class="cdx-list-unordered"><li class="cdx-list__item">方法A(推荐):使用<span> </span><code>ssh-copy-id myadmin@ip_address</code><span> </span>自动部署。</li><li class="cdx-list__item">方法B(手动):<code>mkdir -p ~/.ssh && echo "公钥内容" >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys</code>。</li></ul></li><li class="cdx-list__item"><b>测试连接:</b>新开窗口,使用私钥登录成功后再继续。</li></ul>
</div>
</div><div class="ce-block ce-block--focused" data-id="NwQd6mZSlY" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left">这里聊个题外话,在秘钥安全这方面,默认使用秘钥,禁止Root账户使用密码登入的商家还是比较少的,无接触最多的也就是<code>DMIT</code>,它家默认创建的服务器是使用秘钥登入的。</div>
</div>
</div><div class="ce-block" data-id="2RPW37Pm6L" >
<div class="ce-block__content" style="">
<h4 class="ce-header ce-header--left">▎ 第四步:修改 SSH 端口与防火墙设置</h4>
</div>
</div><div class="ce-block ce-block--focused" data-id="5Zd_ELkz3A" >
<div class="ce-block__content">
<ul class="cdx-list-unordered"><li class="cdx-list__item"><b>修改配置文件,开放其他端口</b>:<code>nano /etc/ssh/sshd_config</code><ul class="cdx-list__item-children" class="cdx-list-unordered"><li class="cdx-list__item">策略:保留<span> </span><code>Port 22</code>,新增一行<span> </span><code>Port 10086</code>(双端口并行,防止配置错误失联)。</li></ul></li></ul>
</div>
</div><div class="ce-block ce-block--focused" data-id="bAEQ14LUqE" >
<div class="ce-block__content">
<blockquote class="cdx-block cdx-quote">
<div class="cdx-input cdx-quote__text">
很多厂商其实在重装系统的时候,就可以指定其他端口作为SSH连接端口,这样可以防止很多默认扫描行为,因为SSH在22端口是默认的,导致很多恶意扫描只针对SSH,如果其他高位端口,它还需要扫描你服务器的所有端口才能确定。
</div>
<div class="cdx-input cdx-quote__caption cdx-quote__caption-left">
</div>
<div class="quote-background-icon">
<svg t="1747277238703" class="icon" viewBox="0 0 1126 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6647" width="60" height="60"><path d="M809.07988966 473.917355c33.85124 8.46280999 50.77686 25.38843 50.77685999 50.77686 0 42.31404999-42.31405 126.942149-135.404959 262.347107-33.85124 50.77686-50.77686 93.090909-50.77686 135.404959 0 67.70247899 25.38843 101.553719 84.6281 101.553719 33.85124001 0 67.702479-25.38843 110.016529-76.165289 169.256198-211.570248 253.884298-406.214876 253.88429699-600.859504 0-93.090909-25.38843-177.719008-76.16528899-236.95867799-50.77686-67.702479-118.479339-93.090909-194.64462799-93.09090901-59.23966901 0-118.479339 25.38843-160.79338901 67.702479S622.89807165 186.18181799 622.89807164 253.884298c8.46281 110.016529 67.702479 186.18181801 186.18181802 220.033057m-609.32231401 0c33.85124 8.46280999 50.77686 25.38843 50.77686 50.77686 0 42.31404999-42.31405 126.942149-135.404959 262.347107-33.85124 50.77686-50.77686 93.090909-50.77686 135.404959 0 67.70247899 25.38843 101.553719 84.6281 101.553719 33.85124001 0 67.702479-25.38843 110.016529-76.165289 169.256198-211.570248 253.884298-414.677686 253.88429701-600.859504 0-93.090909-25.38843-177.719008-76.16528901-236.958678-50.77686-67.702479-118.479339-93.090909-194.644628-93.090909-59.23966901 0-118.479339 25.38843-160.793389 67.702479s-67.70247899 101.553719-67.702479 169.256199c16.92562 110.016529 76.165289 186.181818 186.181818 220.033057" fill="#bfbfbf" p-id="6648"></path></svg>
</div>
</blockquote>
</div>
</div><div class="ce-block ce-block--focused" data-id="PQpZ_J0bAp" >
<div class="ce-block__content">
<div class="cdx-block image-tool image-tool--filled ">
<div class="image-tool__image">
<div class="image-tool__image-preloader" style=""></div>
<img id="aimg_PQpZ_J0bAp" class="image-tool__image-picture _zoom" src="http://idceve.com/data/attachment/forum/202609/06/180413ykjyka19a9jdqqrd.png" title="" alt="" data-aid="154"/>
</div>
<div class="cdx-input image-tool__caption" data-placeholder=""></div>
</div>
</div>
</div><div class="ce-block ce-block--focused" data-id="dUTST8sfj_" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left"><b>
配置防火墙(UFW/Firewalld)<span style="color: rgb(34, 34, 34); font-family: Inter, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;">:</span></b></div>
</div>
</div><div class="ce-block ce-block--focused" data-id="PJNQ1ljICS" >
<div class="ce-block__content">
<ul class="cdx-list-unordered"><li class="cdx-list__item"><b>重点:</b>必须先放行新端口,再启用防火墙!,这一点至关重要,如果没搞定就退出SSH,容易整个服务器都失联,那就有意思了。</li><li class="cdx-list__item">Debian/Ubuntu (UFW):</li></ul>
</div>
</div><div class="ce-block ce-block--focused" data-id="Ir49IpabFl" >
<div class="ce-block__content">
<div class="editorjs-codeFlask_Wrapper">
<div class="editorjs-codeFlask_Header">
<div class="editorjs-codeFlask_LangDisplay">shell</div>
<button class="editorjs-codeFlask_CopyButton" title="复制代码" id="codeflask-copy-Ir49IpabFl">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
</svg>
</button>
<button class="editorjs-codeFlask_Toggle" id="codeflask-Toggle-Ir49IpabFl">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<polyline points="6 9 12 15 18 9"></polyline>
</svg>
</button>
</div>
<div class="editorjs-codeFlask_ContentContainer">
<div class="editorjs-codeFlask_Editor" id="codeflask-Ir49IpabFl" >
</div>
</div>
<div class="editorjs-codeFlask_BottomButtonContainer" id="codeflask-bottomBtn-Ir49IpabFl">
<button class="editorjs-codeFlask_BottomToggle expand-mode" title="展开代码" data-empty="false"><span class="toggle-icon">▼</span> 展开</button>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="static/js/editorjs/tools/codeflask/codeflask150.min.js?xvG"></script>
<script type="application/javascript">
const editorElem1790134478SmT5b = document.getElementById('codeflask-Ir49IpabFl');
var isCollapsed1790134478SmT5b = true;
const flask1790134478SmT5b = new CodeFlask(editorElem1790134478SmT5b, {
language: 'shell',
lineNumbers: true,
styleParent: this.shadowRoot,
rtl: false,
readonly: true
});
var code1790134478SmT5b = `ufw allow 10086/tcp# 放行新SSH端口
ufw allow 80/tcp # 预留Web端口
ufw allow 443/tcp
ufw enable # 启用防火墙`;
code1790134478SmT5b = code1790134478SmT5b.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'");
flask1790134478SmT5b.addLanguage('shell', Prism.languages['shell']);
flask1790134478SmT5b.onUpdate((code) => {
// do something with code here.
// this will trigger whenever the code
// in the editor changes.
// console.log(code)
});
// flask.updateCode('');
// This will also trigger .onUpdate()
flask1790134478SmT5b.updateCode(code1790134478SmT5b);
const currentCode1790134478SmT5b = flask1790134478SmT5b.getCode();
var coderow1790134478SmT5b = parseInt('');
if (coderow1790134478SmT5b === undefined || coderow1790134478SmT5b !== coderow1790134478SmT5b || coderow1790134478SmT5b === 0) {
coderow1790134478SmT5b = flask1790134478SmT5b.lineNumber;
}
if (coderow1790134478SmT5b < 20) {
editorElem1790134478SmT5b.parentElement.style.height = '300px';
editorElem1790134478SmT5b.style.height = '300px';
} else if (coderow1790134478SmT5b >= 20) {
editorElem1790134478SmT5b.parentElement.style.height = '500px';
editorElem1790134478SmT5b.style.height = '500px';
}
//console.log({currentCode1790134478SmT5b})
const copyBtn1790134478SmT5b = document.getElementById('codeflask-copy-Ir49IpabFl');
copyBtn1790134478SmT5b.addEventListener('click', function() {
try {
// 先检查 Clipboard API 是否可用
if (navigator.clipboard && navigator.clipboard.writeText) {
// 使用 Clipboard API 复制代码
navigator.clipboard.writeText(code1790134478SmT5b).then(function() {
// 复制成功提示
const originalText = copyBtn1790134478SmT5b.innerHTML;
copyBtn1790134478SmT5b.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg> 已复制';
// 一段时间后恢复原文本
setTimeout(function() {
copyBtn1790134478SmT5b.innerHTML = originalText;
}, 2000);
}).catch(function(err) {
// 复制失败处理
console.error('复制失败:', err);
// 降级方案:使用传统的复制方法
fallbackCopyTextToClipboard(code1790134478SmT5b);
});
} else {
// Clipboard API 不可用时直接使用降级方案
fallbackCopyTextToClipboard(code1790134478SmT5b);
}
// 提取降级方案为独立函数
function fallbackCopyTextToClipboard(text) {
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed';
textArea.style.opacity = '0';
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
const originalText = copyBtn1790134478SmT5b.innerHTML;
copyBtn1790134478SmT5b.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg> 已复制';
setTimeout(function() {
copyBtn1790134478SmT5b.innerHTML = originalText;
}, 2000);
} catch (copyErr) {
console.error('传统复制方法也失败了:', copyErr);
} finally {
document.body.removeChild(textArea);
}
}
} catch (err) {
console.error('复制功能不可用:', err);
}
});
const bottomBtn1790134478SmT5b = document.getElementById('codeflask-bottomBtn-Ir49IpabFl');
const Toggle1790134478SmT5b = document.getElementById('codeflask-Toggle-Ir49IpabFl');
bottomBtn1790134478SmT5b.addEventListener('click', function() {
if (isCollapsed1790134478SmT5b) {
// 展开代码
isCollapsed1790134478SmT5b = false;
const codeElement1790134478SmT5b = editorElem1790134478SmT5b.querySelector('.codeflask__code');
if (codeElement1790134478SmT5b) {
const actualHeight1790134478SmT5b = codeElement1790134478SmT5b.scrollHeight + 20;
editorElem1790134478SmT5b.parentElement.style.height = actualHeight1790134478SmT5b + 'px';
editorElem1790134478SmT5b.style.height = actualHeight1790134478SmT5b + 'px';
}
bottomBtn1790134478SmT5b.innerHTML = '<button class="editorjs-codeFlask_BottomToggle" title="折叠代码"><span class="toggle-icon">▲</span> 折叠</button>';
Toggle1790134478SmT5b.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"></polyline></svg>';
} else {
// 折叠代码
isCollapsed1790134478SmT5b = true;
if (coderow1790134478SmT5b < 20) {
editorElem1790134478SmT5b.parentElement.style.height = '300px';
editorElem1790134478SmT5b.style.height = '300px';
} else if (coderow1790134478SmT5b >= 20) {
editorElem1790134478SmT5b.parentElement.style.height = '500px';
editorElem1790134478SmT5b.style.height = '500px';
}
bottomBtn1790134478SmT5b.innerHTML = '<button class="editorjs-codeFlask_BottomToggle expand-mode" title="展开代码" data-empty="false"><span class="toggle-icon">▼</span> 展开</button>';
Toggle1790134478SmT5b.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>';
}
});
Toggle1790134478SmT5b.addEventListener('click', function() {
bottomBtn1790134478SmT5b.click();
});
</script><div class="ce-block ce-block--focused" data-id="onkCiN9PrZ" >
<div class="ce-block__content">
<ul class="cdx-list-unordered"><li class="cdx-list__item"><b>验证:</b>尝试通过新端口 10086 连接。成功后,回到配置文件删除<span> </span><code>Port 22</code><span> </span>并重启 SSH 服务。</li></ul>
</div>
</div><div class="ce-block ce-block--focused" data-id="cIjPt3OTMn" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left">80和443是网站搭建必备的端口,如果你是有其他的端口需要使用,修改上面的命令后面的端口号就好了,这一步是为了安全不暴露端口,如果懒得弄也可以不做,不是必须的。</div>
</div>
</div><div class="ce-block" data-id="bWBVrYUybJ" >
<div class="ce-block__content" style="">
<h4 class="ce-header ce-header--left">▎ 第五步:禁止密码与 Root 登录 完成漏洞封堵</h4>
</div>
</div><div class="ce-block ce-block--focused" data-id="_8IMkBAnVo" >
<div class="ce-block__content">
<ul class="cdx-list-unordered"><li class="cdx-list__item"><b>修改配置:</b><code>nano /etc/ssh/sshd_config</code><ul class="cdx-list__item-children" class="cdx-list-unordered"><li class="cdx-list__item"><code>PermitRootLogin no</code>:彻底禁止 Root 登录(因为我们已经有了 sudo 用户)所以不必要担心。</li><li class="cdx-list__item"><code>PasswordAuthentication no</code>:彻底关闭密码验证,这样只能通过秘钥登入了。</li></ul></li><li class="cdx-list__item"><b>重启服务:</b><code>systemctl restart sshd</code><span> </span>完成新配置的加载。</li></ul>
</div>
</div><div class="ce-block ce-block--focused" data-id="8brcUShINg" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left"><font style="color: rgb(76, 175, 80);">完成这一步之后只能通过秘钥登入,root账户无法使用普通密码登入,密码登入方式作废。如果秘钥要使用穷举法暴力破解,那基本上是痴心妄想了。你只要不主动泄露基本上密码无忧。</font></div>
</div>
</div><div class="ce-block ce-block--focused" data-id="1664CjHIwN" >
<div class="ce-block__content">
<div class="cdx-block image-tool image-tool--filled ">
<div class="image-tool__image">
<div class="image-tool__image-preloader" style=""></div>
<img id="aimg_1664CjHIwN" class="image-tool__image-picture _zoom" src="http://idceve.com/data/attachment/forum/202609/06/180747zwbc3llwyb8w33f3.png" title="" alt="" data-aid="155"/>
</div>
<div class="cdx-input image-tool__caption" data-placeholder=""></div>
</div>
</div>
</div><div class="ce-block" data-id="zRXK_KRIer" >
<div class="ce-block__content" style="">
<h4 class="ce-header ce-header--left">第五步:安装 Fail2Ban 这里是加强工具(非必要)</h4>
</div>
</div><div class="ce-block ce-block--focused" data-id="aDpDEACRX-" >
<div class="ce-block__content">
<ul class="cdx-list-unordered"><li class="cdx-list__item"><b>痛点:</b>即使改了端口,可能还有人扫;Fail2Ban 可以自动拉黑多次试错的 IP,就跟一开始一样很烦,日志里面都是尝试登入的日志,有时候会你的日志会变得很大,很大原因就是因为尝试SSH登入惹得锅。</li><li class="cdx-list__item"><b>安装:</b><code>apt install fail2ban -y</code>。</li><li class="cdx-list__item"><b>配置:</b>复制配置文件<span> </span><code>cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local</code>,设置<span> </span><code>bantime</code><span> </span>(封禁时间) 和<span> </span><code>maxretry</code><span> </span>(最大尝试次数)。</li></ul>
</div>
</div><div class="ce-block ce-block--focused" data-id="rtsl2SleZ-" >
<div class="ce-block__content">
<blockquote class="cdx-block cdx-quote">
<div class="cdx-input cdx-quote__text">
fail2ban的配置文件位于<code class="inline-code">/etc/fail2ban</code>目录中。主要的配置文件是<code class="inline-code">jail.conf</code>,你可以在这个文件中定义全局设置,或者为不同的服务创建特定的规则。
</div>
<div class="cdx-input cdx-quote__caption cdx-quote__caption-left">
</div>
<div class="quote-background-icon">
<svg t="1747277238703" class="icon" viewBox="0 0 1126 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6647" width="60" height="60"><path d="M809.07988966 473.917355c33.85124 8.46280999 50.77686 25.38843 50.77685999 50.77686 0 42.31404999-42.31405 126.942149-135.404959 262.347107-33.85124 50.77686-50.77686 93.090909-50.77686 135.404959 0 67.70247899 25.38843 101.553719 84.6281 101.553719 33.85124001 0 67.702479-25.38843 110.016529-76.165289 169.256198-211.570248 253.884298-406.214876 253.88429699-600.859504 0-93.090909-25.38843-177.719008-76.16528899-236.95867799-50.77686-67.702479-118.479339-93.090909-194.64462799-93.09090901-59.23966901 0-118.479339 25.38843-160.79338901 67.702479S622.89807165 186.18181799 622.89807164 253.884298c8.46281 110.016529 67.702479 186.18181801 186.18181802 220.033057m-609.32231401 0c33.85124 8.46280999 50.77686 25.38843 50.77686 50.77686 0 42.31404999-42.31405 126.942149-135.404959 262.347107-33.85124 50.77686-50.77686 93.090909-50.77686 135.404959 0 67.70247899 25.38843 101.553719 84.6281 101.553719 33.85124001 0 67.702479-25.38843 110.016529-76.165289 169.256198-211.570248 253.884298-414.677686 253.88429701-600.859504 0-93.090909-25.38843-177.719008-76.16528901-236.958678-50.77686-67.702479-118.479339-93.090909-194.644628-93.090909-59.23966901 0-118.479339 25.38843-160.793389 67.702479s-67.70247899 101.553719-67.702479 169.256199c16.92562 110.016529 76.165289 186.181818 186.181818 220.033057" fill="#bfbfbf" p-id="6648"></path></svg>
</div>
</blockquote>
</div>
</div><div class="ce-block ce-block--focused" data-id="6FlizLvI60" >
<div class="ce-block__content">
<ul class="cdx-list-unordered"><li class="cdx-list__item"><b>配置文件介绍:</b></li><li class="cdx-list__item">bantime:在检测到攻击后,攻击者被禁止的秒数。</li><li class="cdx-list__item">unbantime:定期检查和解除禁止的时间,以秒为单位。</li><li class="cdx-list__item">maxretry:在执行ban之前允许的最大失败尝试次数。</li><li class="cdx-list__item">findtime:在maxretry次数内允许的时间窗口,以秒为单位。</li></ul>
</div>
</div><div class="ce-block" data-id="WpUF_3J98r" >
<div class="ce-block__content" style="">
<h4 class="ce-header ce-header--left">▎ 最后救援大法</h4>
</div>
</div><div class="ce-block ce-block--focused" data-id="wCqnbe6QCW" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left">如果真的在执行过程中出现意外,或者防火墙忘了开放端口,或者秘钥丢失找不到了,也不要着急下面提供一个解决思路。</div>
</div>
</div><div class="ce-block ce-block--focused" data-id="JhasBFSk5V" >
<div class="ce-block__content">
<ul class="cdx-list-unordered"><li class="cdx-list__item"><b>VNC 直连大法:</b><ul class="cdx-list__item-children" class="cdx-list-unordered"><li class="cdx-list__item">场景:防火墙没开就重启了?密钥丢了?</li><li class="cdx-list__item">操作:登录 VPS 商家后台(基本上所有的商家都有),打开 VNC 控制台。VNC 相当于直连显示器,不受 SSH 限制,可直接登录修改配置,配置好新的秘钥或者开启SSH密码登入更换新的密码,在重新开始。</li></ul></li></ul>
</div>
</div><div class="ce-block ce-block--focused" data-id="y7ZFHV4Lch" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left">实在没救了,那就只能挂壁了,如果是新的服务器也就无所谓去商家后台重装系统就好了。</div>
</div>
</div><div class="ce-block" data-id="1ZCcaR7hgg" >
<div class="ce-block__content" style="">
<h4 class="ce-header ce-header--left">▎ 总结</h4>
</div>
</div><div class="ce-block ce-block--focused" data-id="S-bsq-e27b" >
<div class="ce-block__content">
<div class="ce-paragraph cdx-block ce-paragraph--left">总结下来就这几个步骤,基本上能让你的VPS高枕无忧,不用担心恶意的扫描和暴力破解。防火墙也能有效的防护端口,把不必要暴露外部的应用端口给好好保护起来。比如MYSQL的<code>3306</code>、Redis的<code>6379</code>很多人都是使用的纯数字简单密码或者根本就没密码,这也是十分危险的行为。</div>
</div>
</div><div class="ce-block ce-block--focused" data-id="E4rtBNlI2I" >
<div class="ce-block__content">
<ol class="cdx-list-ordered" style="list-style-type:none; margin-left: -15px;"><li class="cdx-list__item">1. 系统已更新且时间已校准</li><li class="cdx-list__item">2.新建 Sudo 用户,且 Root 远程登录已禁用</li><li class="cdx-list__item">3.SSH 密钥登录测试通过,密码登录已关闭</li><li class="cdx-list__item">4.SSH 端口已修改,且防火墙 (UFW) 已正确放行</li><li class="cdx-list__item">5.Fail2Ban 已开启监控</li></ol>
</div>
</div><style type="text/css">
.ce-block {
margin-top: 20px;
margin-bottom: 20px;
}
.ce-block__content,.ce-toolbar__content {
/* max-width:calc(100% - 50px) */
margin-left: auto;
margin-right: auto;
}
/**
* Plugin styles
*/
.ce-header {
position: relative;
padding: 1px 0px 1px 15px;
margin: 0;
line-height: 1.25em;
outline: none;
margin-bottom: 10px;
}
.ce-header p,
.ce-header div {
padding: 0 !important;
margin: 0 !important;
}
.ce-header::before {
content: "";
background-color: #3e8fe3;
width: 6px;
height: 100%;
position: absolute;
left: 0;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
/**
* Styles for Plugin icon in Toolbar
*/
.ce-header__icon {
}
.ce-header::before {
position: absolute;
content: attr(data-placeholder);
color: #707684;
font-weight: normal;
display: none;
cursor: text;
}
.ce-header:empty::before {
display: block;
}
.ce-header:empty:focus::before {
display: none;
}
/* FontSize */
h1.ce-header {
font-size: 2.0em;
}
h2.ce-header {
font-size: 1.7em;
}
h3.ce-header {
font-size: 1.4em;
}
h4.ce-header {
font-size: 1.15em;
}
h5.ce-header {
font-size: 0.95em;
}
h6.ce-header {
font-size: 0.8em;
}
/* Alignment*/
.ce-header--right {
text-align: right;
}
.ce-header--center {
text-align: center;
}
.ce-header--left {
text-align: left;
}
.ce-header--justify {
text-align: justify;
}
</style><style type="text/css">
.ce-block {
margin-bottom: 20px;
}
.ce-block__content,.ce-toolbar__content {
/* max-width:calc(100% - 50px) */
margin-left: auto;
margin-right: auto;
}
.ce-paragraph {
line-height: 1.6em;
outline: none;
text-indent: 2em;
font-size: 16px;
}
.ce-paragraph--right {
text-align: right;
}
.ce-paragraph--center {
text-align: center;
}
.ce-paragraph--left {
text-align: left;
}
.ce-paragraph--justify {
text-align: justify;
}
.ce-paragraph-text-indent {
text-align: justify;
}
.ce-paragraph:empty::before{
content: attr(data-placeholder);
color: #707684;
font-weight: normal;
opacity: 0;
}
/** Show placeholder at the first paragraph if Editor is empty */
.codex-editor--empty .ce-block:first-child .ce-paragraph:empty::before {
opacity: 1;
}
.codex-editor--toolbox-opened .ce-block:first-child .ce-paragraph:empty::before,
.codex-editor--empty .ce-block:first-child .ce-paragraph:empty:focus::before {
opacity: 0;
}
.ce-paragraph p:first-of-type{
margin-top: 0;
}
.ce-paragraph p:last-of-type{
margin-bottom: 0;
}
.svg-icon {
width: 1em;
height: 1em;
}
.svg-icon path,
.svg-icon polygon,
.svg-icon rect {
fill: #4691f6;
}
.svg-icon circle {
stroke: #4691f6;
stroke-width: 1;
}
</style><style type="text/css">
.ce-block {
margin-bottom: 20px;
}
.ce-block__content,.ce-toolbar__content {
/* max-width:calc(100% - 50px) */
margin-left: auto;
margin-right: auto;
}
.image-tool {
--bg-color: #cdd1e0;
--front-color: #388ae5;
--border-color: #e8e8eb;
}
.image-tool__image {
border-radius: 3px;
overflow: hidden;
margin-bottom: 10px;
}
.image-tool__image-picture {
max-width: 100%;
vertical-align: bottom;
display: block;
margin-left: auto;
margin-right: auto;
}
.image-tool__image-preloader {
width: 50px;
height: 50px;
border-radius: 50%;
background-size: cover;
margin: auto;
position: relative;
background-color: #cdd1e0;
background-position: center center;
}
.image-tool__image-preloader::after {
content: "";
position: absolute;
z-index: 3;
width: 60px;
height: 60px;
border-radius: 50%;
border: 2px solid #cdd1e0;
border-top-color: #388ae5;
left: 50%;
top: 50%;
margin-top: -30px;
margin-left: -30px;
animation: image-preloader-spin 2s infinite linear;
box-sizing: border-box;
}
.image-tool__caption::before {
position: absolute !important;
content: attr(data-placeholder);
color: #707684;
font-weight: normal;
display: none;
}
.image-tool__caption:empty::before {
display: block;
}
.image-tool__caption:empty:focus::before {
display: none;
}
.image-tool--empty .image-tool__image {
display: none;
}
.image-tool--empty .image-tool__caption, .image-tool--loading .image-tool__caption {
display: none;
}
.image-tool .cdx-button {
display: flex;
align-items: center;
justify-content: center;
}
.image-tool .cdx-button svg {
height: auto;
margin: 0 6px 0 0;
}
.image-tool--filled .cdx-button {
display: none;
}
.image-tool--filled .image-tool__image-preloader {
display: none;
}
.image-tool--loading .image-tool__image {
min-height: 200px;
display: flex;
border: 1px solid #e8e8eb;
background-color: #fff;
}
.image-tool--loading .image-tool__image-picture {
display: none;
}
.image-tool--loading .cdx-button {
display: none;
}
/**
* Tunes
* ----------------
*/
.image-tool--withBorder .image-tool__image {
border: 1px solid #e8e8eb;
}
.image-tool--withBackground .image-tool__image {
padding: 15px;
background: #cdd1e0;
}
.image-tool--withBackground .image-tool__image-picture {
max-width: 60%;
margin: 0 auto;
}
.image-tool--stretched .image-tool__image-picture {
width: 100%;
}
.image-tool__caption {
text-align: center;
font-size: 14px;
color: #a3a3a3;
}
@keyframes image-preloader-spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style><style type="text/css">
.ce-block {
margin-bottom: 20px;
}
.ce-block__content,.ce-toolbar__content {
/* max-width:calc(100% - 50px) */
margin-left: auto;
margin-right: auto;
}
.cdx-list {
margin:0;
outline:none;
display:block;
counter-reset:item;
padding:6px;
}
.cdx-list__item {
line-height:1.45em;
display:block;
padding-top:8px;
margin-left: 2em;
}
.cdx-list__item-children {
display:block;
}
.cdx-list__item {
outline:none
}
.cdx-list__item-content {
word-break:break-word;
white-space:pre-wrap;
grid-area:content;
padding-left:8px
}
.cdx-list__item:before {
counter-increment:item;
white-space:nowrap
}
.cdx-list-li-container {
display: flex;
}
.cdx-list-ordered .cdx-list__item:before {
/* content:counters(item,".",numeric) "." */
}
.cdx-list-ordered {
list-style-type: none;
margin-left: -15px;
counter-reset:item;
font-size: 16px;
}
.cdx-list-unordered {
font-size: 16px;
}
.cdx-list-unordered .cdx-list__item:before {
content:"•"
}
.cdx-list-checklist .cdx-list__item:before {
content:""
}
.cdx-list__settings .cdx-settings-button {
width:50%
}
.cdx-list__checkbox {
padding-top:calc((1.45em - 1.2em) / 2);
grid-area:checkbox;
width:1.2em;
height:1.2em;
display:flex;
cursor:pointer;
font-size: 16px;
}
.cdx-list__checkbox svg {
opacity:0;
height:1.2em;
width:1.2em;
left:-1px;
top:-1px;
position:absolute
}
@media (hover:hover) {
.cdx-list__checkbox:not(.cdx-list__checkbox--no-hover):hover .cdx-list__checkbox-check svg {
opacity:1
}
}.cdx-list__checkbox--checked-1 {
line-height:1.45em
}
@media (hover:hover) {
.cdx-list__checkbox--checked-1:not(.cdx-list__checkbox--checked-1--no-hover):hover .cdx-checklist__checkbox-check {
background:#0059AB;
border-color:#0059AB
}
}.cdx-list__checkbox--checked-1 .cdx-list__checkbox-check {
background:#369FFF;
border-color:#369FFF
}
.cdx-list__checkbox--checked-1 .cdx-list__checkbox-check svg {
opacity:1
}
.cdx-list__checkbox--checked-1 .cdx-list__checkbox-check svg path {
stroke:#fff
}
.cdx-list__checkbox--checked-1 .cdx-list__checkbox-check:before {
opacity:0;
visibility:visible;
transform:scale(2.5)
}
.cdx-list__checkbox-check {
cursor:pointer;
display:inline-block;
position:relative;
margin:0 auto;
width:1.2em;
height:1.2em;
box-sizing:border-box;
border-radius:5px;
border:1px solid #C9C9C9;
background:#fff
}
.cdx-list__checkbox-check:before {
content:"";
position:absolute;
top:0;
right:0;
bottom:0;
left:0;
border-radius:100%;
background-color:#369FFF;
visibility:hidden;
pointer-events:none;
transform:scale(1);
transition:transform .4s ease-out,opacity .4s
}
.cdx-list__checkbox-check--disabled {
pointer-events:none
}
.cdx-list-start-with-field {
background:#F8F8F8;
border:1px solid rgba(226,226,229,.2);
border-radius:6px;
padding:2px;
display:grid;
grid-template-columns:auto auto 1fr;
grid-template-rows:auto
}
.cdx-list-start-with-field--invalid {
background:#FFECED;
border:1px solid #E13F3F
}
.cdx-list-start-with-field--invalid .cdx-list-start-with-field__input {
color:#e13f3f
}
.cdx-list-start-with-field__input {
font-size:16px;
outline:none;
font-weight:500;
font-family:inherit;
border:0;
background:transparent;
margin:0;
padding:0;
line-height:22px;
min-width:calc(100% - 10px)
}
.cdx-list-start-with-field__input::placeholder {
color:#797979;
font-weight:500
}
</style><style type="text/css">
.ce-block {
margin-bottom: 20px;
}
.ce-block__content, .ce-toolbar__content {
margin-left: auto;
margin-right: auto;
}
.cdx-quote {
position: relative;
background-color: #f8f8f8;
padding: 24px!important;
margin: 0px;
border-radius: 12px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
.quote-background-icon {
position: absolute;
top: 10px;
right: 10px;
opacity: 0.2;
z-index: 1;
}
.cdx-quote__text {
min-height: 30px;
margin-bottom: 10px;
font-size: 16px;
line-height: 1.5;
color: #333;
}
.cdx-quote__caption {
padding: 10px 20px 0px 20px;
font-size: 14px;
color: #777;
}
.cdx-quote__caption-left {
text-align: left;
}
.cdx-quote__caption-center {
text-align: center;
}
.cdx-quote__caption-right {
text-align: right;
}
.cdx-quote ::before {
position: absolute;
content: attr(data-placeholder);
color: #bbb;
font-weight: normal;
opacity: 0;
}
.cdx-quote :empty::before {
opacity: 1;
}
.cdx-quote :empty:focus::before {
opacity: 0;
}
.cdx-quote-settings {
display: flex;
}
.cdx-quote-settings .cdx-settings-button {
width: 50%;
}
</style><style type="text/css">
.ce-block {
margin-bottom: 20px;
}
.ce-block__content,.ce-toolbar__content {
/* max-width:calc(100% - 50px) */
margin-left: auto;
margin-right: auto;
position: relative;
}
/* 主容器 */
.editorjs-codeFlask_Wrapper {
border: 1px solid #dcdfe6;
border-radius: 5px;
background-color: #f6f8fa;
margin-bottom: 10px;
position: relative;
transition: all 0.3s ease;
width: 100%;
min-height: 100px;
overflow: hidden;
}
/* 标题栏 */
.editorjs-codeFlask_Header {
display: flex;
justify-content: flex-end;
align-items: center;
padding: 8px 12px;
background-color: #e9ecef;
border-bottom: 1px solid #dcdfe6;
position: relative;
z-index: 0; /* 从10降低到2 */
}
/* 语言显示 */
.editorjs-codeFlask_LangDisplay {
padding: 2px 8px;
background-color: #409eff;
color: white;
border-radius: 3px;
font-size: 12px;
font-weight: 500;
position: absolute;
left: 12px;
top: 50%;
transform: translateY(-50%);
}
/* 内容容器 - 禁止纵向滚动 */
.editorjs-codeFlask_ContentContainer {
position: relative;
min-height: 100px;
overflow-x: hidden; /* 禁止横向滚动 */
overflow-y: hidden;
transition: height 0.3s ease;
}
/* 编辑器容器 - 禁止纵向滚动 */
.editorjs-codeFlask_Editor {
position: relative;
min-height: 100px;
overflow-x: hidden; /* 禁止横向滚动 */
overflow-y: hidden;
transition: height 0.3s ease;
}
/* 底部按钮容器 - 优化为更简洁的样式 */
.editorjs-codeFlask_BottomButtonContainer {
display: flex;
justify-content: center;
align-items: center;
padding: 4px;
background-color: #f8f9fa;
border-top: 1px solid #e9ecef;
position: relative;
z-index: 0;
}
/* 底部横向折叠/展开按钮 - 优化样式,使其更融入设计 */
.editorjs-codeFlask_BottomToggle {
width: 100%;
background-color: transparent;
color: #606266;
border: none;
border-radius: 4px;
padding: 6px 12px;
font-size: 12px;
font-weight: 400;
cursor: pointer;
transition: all 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
gap: 4px;
text-align: center;
position: relative;
}
.editorjs-codeFlask_BottomToggle:hover {
background-color: #e9ecef;
color: #409eff;
}
.editorjs-codeFlask_BottomToggle:active {
background-color: #dee2e6;
}
.editorjs-codeFlask_BottomToggle .toggle-icon {
font-size: 11px;
transition: transform 0.3s ease;
}
/* 展开按钮(显示"展开"文本)上边缘添加渐变虚化效果 */
.editorjs-codeFlask_BottomToggle:has(span.toggle-icon:contains("▲"))::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
height: 6px;
background: linear-gradient(to bottom, rgba(248, 249, 250, 0) 0%, rgba(248, 249, 250, 1) 100%);
pointer-events: none;
}
/* 展开按钮(显示"展开"文本)上边缘添加渐变虚化效果 */
/* 删除不被广泛支持的:contains()选择器 */
.editorjs-codeFlask_BottomToggle.expand-mode::before {
content: '';
position: absolute;
top: -60px;
left: 0;
right: 0;
height: 60px;
background: linear-gradient(to bottom, rgb(255 255 255 / 0%) 0%, rgba(248, 249, 250, 1) 100%);
pointer-events: none;
}
/* CodeFlask 主容器样式 */
.editorjs-codeFlask_Editor .codeflask {
position: relative;
background: #fafafa;
border-radius: 0 0 4px 4px;
min-height: 100px;
overflow-x: hidden; /* 禁止横向滚动 */
overflow-y: hidden;
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
}
/* 行号容器 */
.editorjs-codeFlask_Editor .codeflask.codeflask--has-line-numbers:before {
content: '';
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: 40px;
background-color: #f5f5f5;
border-right: 1px solid #e0e0e0;
z-index: 0;
}
/* 行号 */
.editorjs-codeFlask_Editor .codeflask__lines {
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: 40px;
padding: 10px 0;
background-color: #f5f5f5;
border-right: 1px solid #e0e0e0;
z-index: 0;
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
font-size: 14px;
line-height: 21px;
color: #666;
text-align: right;
user-select: none;
overflow: hidden;
}
.editorjs-codeFlask_Editor .codeflask__lines__line {
padding-right: 8px;
}
/* 文本区域 - 输入框 */
.editorjs-codeFlask_Editor .codeflask__textarea {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
padding: 10px;
border: none;
background: transparent;
color: transparent;
caret-color: #333;
resize: none;
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
font-size: 14px;
line-height: 21px;
z-index: 1;
overflow-x: hidden; /* 禁止横向滚动 */
overflow-y: hidden;
white-space: pre-wrap; /* 自动换行 */
tab-size: 4;
outline: none;
}
/* 添加选中文本样式 */
.editorjs-codeFlask_Editor .codeflask__textarea::selection {
background-color: #b3d4fc;
color: #333;
}
.editorjs-codeFlask_Editor .codeflask__textarea::-moz-selection {
background-color: #b3d4fc;
color: #333;
}
/* 代码预览区域 */
.editorjs-codeFlask_Editor .codeflask__pre {
position: absolute;
top: 0;
left: 0;
width: 100%; /* 确保宽度不超出容器 */
height: 100%;
padding: 10px;
margin: 0;
border: none;
background: transparent;
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
font-size: 14px;
line-height: 21px;
z-index: 0;
overflow-x: hidden; /* 禁止横向滚动 */
overflow-y: hidden;
white-space: pre-wrap; /* 自动换行 */
pointer-events: none;
}
/* 代码高亮区域 */
.editorjs-codeFlask_Editor .codeflask__code {
display: block;
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
font-size: 14px;
line-height: 21px;
color: #333;
white-space: pre-wrap; /* 自动换行 */
tab-size: 4;
overflow: visible;
}
/* 语法高亮样式 - 确保Prism.js样式正确应用 */
.editorjs-codeFlask_Editor .codeflask__code {
background: transparent !important;
}
/* 通用语法高亮token样式 */
.editorjs-codeFlask_Editor .token.comment,
.editorjs-codeFlask_Editor .token.prolog,
.editorjs-codeFlask_Editor .token.doctype,
.editorjs-codeFlask_Editor .token.cdata {
color: #708090;
}
.editorjs-codeFlask_Editor .token.punctuation {
color: #999;
}
.editorjs-codeFlask_Editor .token.namespace {
opacity: 0.7;
}
.editorjs-codeFlask_Editor .token.property,
.editorjs-codeFlask_Editor .token.tag,
.editorjs-codeFlask_Editor .token.boolean,
.editorjs-codeFlask_Editor .token.number,
.editorjs-codeFlask_Editor .token.constant,
.editorjs-codeFlask_Editor .token.symbol,
.editorjs-codeFlask_Editor .token.deleted {
color: #905;
}
.editorjs-codeFlask_Editor .token.selector,
.editorjs-codeFlask_Editor .token.attr-name,
.editorjs-codeFlask_Editor .token.string,
.editorjs-codeFlask_Editor .token.char,
.editorjs-codeFlask_Editor .token.builtin,
.editorjs-codeFlask_Editor .token.inserted {
color: #690;
}
.editorjs-codeFlask_Editor .token.operator,
.editorjs-codeFlask_Editor .token.entity,
.editorjs-codeFlask_Editor .token.url,
.editorjs-codeFlask_Editor .language-css .token.string,
.editorjs-codeFlask_Editor .style .token.string {
color: #9a6e3a;
background: hsla(0, 0%, 100%, 0.5);
}
.editorjs-codeFlask_Editor .token.atrule,
.editorjs-codeFlask_Editor .token.attr-value,
.editorjs-codeFlask_Editor .token.keyword {
color: #07a;
}
.editorjs-codeFlask_Editor .token.function,
.editorjs-codeFlask_Editor .token.class-name {
color: #dd4a68;
}
.editorjs-codeFlask_Editor .token.regex,
.editorjs-codeFlask_Editor .token.important,
.editorjs-codeFlask_Editor .token.variable {
color: #e90;
}
/* 复制按钮样式 */
.editorjs-codeFlask_CopyButton {
background-color: #409eff;
color: white;
border: none;
border-radius: 3px;
padding: 6px 12px;
margin-right: 8px;
font-size: 12px;
cursor: pointer;
transition: background-color 0.2s ease;
display: flex;
align-items: center;
justify-content: center;
min-width: 28px;
height: 28px;
}
.editorjs-codeFlask_CopyButton:hover {
background-color: #66b1ff;
}
.editorjs-codeFlask_CopyButton.copied {
background-color: #67c23a;
}
/* 折叠/展开按钮样式 */
.editorjs-codeFlask_Toggle {
background-color: #909399;
color: white;
border: none;
border-radius: 3px;
padding: 0 8px;
font-size: 12px;
cursor: pointer;
transition: background-color 0.2s ease;
display: flex;
align-items: center;
justify-content: center;
min-width: 28px;
height: 28px;
font-weight: bold;
}
.editorjs-codeFlask_Toggle:hover {
background-color: #a6a9ad;
}
/* 响应式调整 */
@media (max-width: 768px) {
.editorjs-codeFlask_Header {
padding: 6px 8px;
}
.editorjs-codeFlask_Editor .codeflask__textarea,
.editorjs-codeFlask_Editor .codeflask__pre,
.editorjs-codeFlask_Editor .codeflask__code {
font-size: 13px;
line-height: 19px;
padding: 8px;
}
.editorjs-codeFlask_Editor .codeflask.codeflask--has-line-numbers:before {
width: 35px;
}
.editorjs-codeFlask_Editor .codeflask__lines {
width: 35px;
font-size: 12px;
}
}
/* 滚动条样式 - 完全隐藏纵向滚动条 */
.editorjs-codeFlask_Wrapper ::-webkit-scrollbar {
width: 0;/* 纵向滚动条宽度为0 */
height: 6px;/* 横向滚动条保持6px宽度 */
}
.editorjs-codeFlask_Wrapper ::-webkit-scrollbar-track {
background: transparent;
border-radius: 0;
}
.editorjs-codeFlask_Wrapper ::-webkit-scrollbar-thumb {
background: transparent;
border-radius: 0;
}
/* Firefox 滚动条隐藏 */
.editorjs-codeFlask_Wrapper {
scrollbar-width: none;/* Firefox 隐藏纵向滚动条 */
}
.editorjs-codeFlask_Wrapper ::-moz-scrollbar {
width: 0;
height: 6px;
}
/* 焦点状态 */
.editorjs-codeFlask_Wrapper:focus-within {
border-color: #409eff;
box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}
/* 只读模式样式 */
.editorjs-codeFlask_Wrapper.readonly .editorjs-codeFlask_Header {
background-color: #f5f7fa;
}
.editorjs-codeFlask_Wrapper.readonly .editorjs-codeFlask_Editor .codeflask__textarea {
cursor: default;
}
/* 确保所有元素正确显示 */
.editorjs-codeFlask_Wrapper * {
box-sizing: border-box;
}
/* 修复行号对齐 */
.editorjs-codeFlask_Editor .codeflask--has-line-numbers .codeflask__textarea,
.editorjs-codeFlask_Editor .codeflask--has-line-numbers .codeflask__pre {
padding-left: 50px;
}
.editorjs-codeFlask_Editor .codeflask--has-line-numbers .codeflask__lines {
padding-top: 10px;
}
/* 语言显示元素 - 添加点击样式 */
.editorjs-codeFlask_LangDisplay {
padding: 2px 8px;
background-color: #409eff;
color: white;
border-radius: 3px;
font-size: 12px;
font-weight: 500;
position: absolute;
left: 12px;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
transition: all 0.2s ease;
}
.editorjs-codeFlask_LangDisplay:hover {
background-color: #66b1ff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
/* 语言选择弹窗 */
.editorjs-codeFlask_LanguagePopup {
background-color: white;
border: 1px solid #dcdfe6;
border-radius: 4px;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
width: 250px;
max-height: 300px;
z-index: 1000;
overflow: hidden;
}
/* 搜索框 */
.editorjs-codeFlask_LanguageSearch {
width: 100%;
padding: 8px 12px;
border: none;
border-bottom: 1px solid #ebeef5;
font-size: 12px;
outline: none;
box-sizing: border-box;
}
/* 语言列表 */
.editorjs-codeFlask_LanguagesList {
max-height: 250px;
overflow-y: auto;
}
/* 语言选项 */
.editorjs-codeFlask_LanguageItem {
padding: 8px 12px;
font-size: 12px;
cursor: pointer;
transition: background-color 0.2s ease;
}
.editorjs-codeFlask_LanguageItem:hover {
background-color: #f5f7fa;
}
.editorjs-codeFlask_LanguageItem.selected {
background-color: #ecf5ff;
color: #409eff;
font-weight: 500;
}
/* 滚动条样式 */
.editorjs-codeFlask_LanguagesList::-webkit-scrollbar {
width: 6px;
}
.editorjs-codeFlask_LanguagesList::-webkit-scrollbar-track {
background-color: #f5f7fa;
}
.editorjs-codeFlask_LanguagesList::-webkit-scrollbar-thumb {
background-color: #c0c4cc;
border-radius: 3px;
}
.editorjs-codeFlask_LanguagesList::-webkit-scrollbar-thumb:hover {
background-color: #909399;
}
</style><style type="text/css">
.inline-code {
background: rgba(250, 239, 240, 0.78);
color: #b44437;
padding: 3px 4px;
border-radius: 5px;
margin: 0 1px;
font-family: inherit;
font-size: 0.86em;
font-weight: 500;
letter-spacing: 0.3px;
}
</style>
页:
[1]