<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Debian on Colinx Blog</title><link>/tags/debian/</link><description>Recent content in Debian on Colinx Blog</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Tue, 14 Mar 2023 00:00:00 +0000</lastBuildDate><atom:link href="/tags/debian/index.xml" rel="self" type="application/rss+xml"/><item><title>个人向 Linux 新服务器初始化清单</title><link>/posts/%E4%B8%AA%E4%BA%BA%E5%90%91linux%E6%96%B0%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%88%9D%E5%A7%8B%E5%8C%96%E6%B8%85%E5%8D%95/</link><pubDate>Tue, 14 Mar 2023 00:00:00 +0000</pubDate><guid>/posts/%E4%B8%AA%E4%BA%BA%E5%90%91linux%E6%96%B0%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%88%9D%E5%A7%8B%E5%8C%96%E6%B8%85%E5%8D%95/</guid><description>
&lt;p>一份 Linux 初始化清单，避免每次拿到新的服务器都要一个个去各种地方搜集指令，以做备忘 &amp;amp; 供有需要的朋友参考。&lt;/p>
&lt;p>服务器发行版我个人推荐 Debian 系列，CentOS 系现在已经开始分裂而且说实话对新手其实并不友好。Debian 是在兼容性，易用性和稳定性之间都取得不错平衡的发行版。新手推荐 Ubuntu, 不过最近商业化有点过度，夹带了越来越多的私活，我个人所有新安装的 Linux 已经全线转向 Debian. 下文以目前最新的 Debian 11 Bullseye 为例。Debian 12 bookworm 也测试通过。&lt;/p>
&lt;blockquote>
&lt;p>azure 干净的 debian 11 镜像，资源使用情况供参考：&lt;/p>
&lt;p>~1G Disk, ~100M RAM, ~300 packages&lt;/p>
&lt;/blockquote>
&lt;p>本文仅列举主要事项和操作，新手可先行阅读这篇文章熟悉概念。&lt;a href="https://blog.colinx.one/posts/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/">云服务器入门指南&lt;/a>&lt;/p>
&lt;p>正文开始&lt;/p>
&lt;h2 id="1---基础环境搭建">
1 - 基础环境搭建
&lt;a href="#1---%e5%9f%ba%e7%a1%80%e7%8e%af%e5%a2%83%e6%90%ad%e5%bb%ba" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h2>
&lt;h3 id="11-防火墙">
1.1 防火墙
&lt;a href="#11-%e9%98%b2%e7%81%ab%e5%a2%99" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>检查服务商防火墙和系统自带防火墙 (debian 系一般为 &lt;code>ufw&lt;/code>, cent os 系一般为 &lt;code>firewall-cmd&lt;/code>).放行 SSH(22), 新的 SSH(自定义), HTTP, HTTPS, 以及其他常用开发端口&lt;/p>
&lt;h3 id="12-新建用户与检查-sudoer">
1.2 新建用户与检查 sudoer
&lt;a href="#12-%e6%96%b0%e5%bb%ba%e7%94%a8%e6%88%b7%e4%b8%8e%e6%a3%80%e6%9f%a5-sudoer" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>登录到 root 用户，创建个人账户并设定密码&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">// -m 创建对应home文件夹
// 添加到 sudo 用户组自动获得sudo权限, 部分发行版为wheel组
useradd -m --groups sudo colin
// 设定密码
passwd colin
&lt;/code>&lt;/pre>&lt;/div>&lt;p>也可直接编辑&lt;code>/etc/sudoers&lt;/code> 文件为新用户添加 sudo 权限，使用 &lt;code>visudo&lt;/code> 指令可以自动帮你校验，避免配置写错把系统搞崩&lt;/p>
&lt;h3 id="13-镜像源与基础软件">
1.3 镜像源与基础软件
&lt;a href="#13-%e9%95%9c%e5%83%8f%e6%ba%90%e4%b8%8e%e5%9f%ba%e7%a1%80%e8%bd%af%e4%bb%b6" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>如为境内服务器需要更换镜像源&lt;/p>
&lt;h4 id="更换国内镜像源">
更换国内镜像源
&lt;a href="#%e6%9b%b4%e6%8d%a2%e5%9b%bd%e5%86%85%e9%95%9c%e5%83%8f%e6%ba%90" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h4>
&lt;p>一般情况下，将  &lt;code>/etc/apt/sources.list&lt;/code>  文件中 Debian 默认的源地址  &lt;code>http://deb.debian.org/&lt;/code>  替换为  &lt;code>http://mirrors.ustc.edu.cn&lt;/code>  即可。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">//for debian
sudo sed -i &lt;span style="color:#e6db74">&amp;#39;s/deb.debian.org/mirrors.ustc.edu.cn/g&amp;#39;&lt;/span> /etc/apt/sources.list
//for ubuntu
sudo sed -i &lt;span style="color:#e6db74">&amp;#39;s@//.*archive.ubuntu.com@//mirrors.ustc.edu.cn@g&amp;#39;&lt;/span> /etc/apt/sources.list
&lt;/code>&lt;/pre>&lt;/div>&lt;p>CentOS 系建议启用 EPEL, PowerTools 等 repo 以更方便地安装常用软件和工具。此处不再赘述&lt;/p>
&lt;p>推荐的几个国内镜像站：&lt;a href="https://mirrors.tuna.tsinghua.edu.cn/">清华大学 TUNA 镜像站&lt;/a>, &lt;a href="https://mirrors.ustc.edu.cn/">中科大 USTC LUG 镜像站&lt;/a>, &lt;a href="https://mirrors.cloud.tencent.com/">腾讯镜像站&lt;/a>&lt;/p>
&lt;h4 id="基础软件">
基础软件
&lt;a href="#%e5%9f%ba%e7%a1%80%e8%bd%af%e4%bb%b6" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h4>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">sudo apt update
sudo apt install wget curl git nano
sudo apt install zsh tmux htop duf htop tldr screenfetch tree
&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="14-ssh-安全">
1.4 SSH 安全
&lt;a href="#14-ssh-%e5%ae%89%e5%85%a8" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>修改端口，配置文件&lt;code>/etc/ssh/sshd_config&lt;/code>. 重启机器或 sshd 服务后生效。
此外最好将&lt;code>PermitRootLogin&lt;/code>修改为&lt;code>no&lt;/code>, 可以禁用 root 登录。&lt;/p>
&lt;p>在本机检查&lt;code>~/.ssh/&lt;/code>有无 id_rsa 等已生成的 key. 如没有再使用 &lt;code>ssh-keygen&lt;/code> 生成私钥
将本机的公钥上传到远端，再写入远端的 &lt;code>authorized_keys&lt;/code> 中&lt;/p>
&lt;p>&lt;code>cat ~/id_rsa.pub &amp;gt;&amp;gt; ~/.ssh/authorized_keys&lt;/code>&lt;/p>
&lt;p>注意修改权限，&lt;code>~/.ssh/authorized_keys&lt;/code> 权限为 600. &lt;code>~/.ssh/&lt;/code>为 700&lt;/p>
&lt;p>可以参考以下设置&lt;/p>
&lt;blockquote>
&lt;p>.ssh/ 0700/rwx&amp;mdash;&amp;mdash;&lt;br>
.ssh/&lt;em>.pub 644/rw-r&amp;ndash;r&amp;ndash;&lt;br>
.ssh/&lt;/em> 0600/rw&amp;mdash;&amp;mdash;-&lt;/p>
&lt;/blockquote>
&lt;p>如失败可参考这篇文章 debug. &lt;a href="https://superuser.com/questions/1137438/ssh-key-authentication-fails">https://superuser.com/questions/1137438/ssh-key-authentication-fails&lt;/a>&lt;/p>
&lt;h3 id="15-设置-hostname">
1.5 设置 hostname
&lt;a href="#15-%e8%ae%be%e7%bd%ae-hostname" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>可选，为了便于识别和后续配置 oh-my-zsh 更美观。这里修改完后还需要更新 hosts 设置，把刚才设置的新的主机名指向 localhost&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">sudo hostnamectl set-hostname my-new-server
sudo hostnamectl status
&lt;/code>&lt;/pre>&lt;/div>&lt;p>此外&lt;code>/etc/hosts&lt;/code> 中默认会有个本机 hostname 映射到&lt;code>127.0.0.1&lt;/code> 的配置，修改 host 之后这里别名的映射也要修改下，不然终端执行命令会一直弹 &lt;code>sudo: unable to resolve host xxx: Name or service not known&lt;/code>&lt;/p>
&lt;p>修改后需重启&lt;/p>
&lt;hr>
&lt;h3 id="16-swap">
1.6 SWAP
&lt;a href="#16-swap" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>可选，建议内存&amp;lt;2G 配置 swap, 大小至少为 2 倍物理内存，可以有效避免意外爆内存的情况。仅推荐 SSD 盘的服务器开启。&lt;/p>
&lt;p>推荐用 &lt;code>fallocate&lt;/code> , 因为这个是最简单、最快速的创建交换空间的方法。 &lt;code>fallocate&lt;/code>  命令用于为文件预分配块 / 大小。&lt;/p>
&lt;p>使用  &lt;code>fallocate&lt;/code>  创建交换空间，首先在  &lt;code>/&lt;/code>  目录下创建一个名为  &lt;code>swap_space&lt;/code>  的文件。然后分配 2GB 到  &lt;code>swap_space&lt;/code>  文件：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">sudo fallocate -l 2G /swap_space
ls -lh /swap_space
// 然后更改文件权限，让 &lt;span style="color:#e6db74">`&lt;/span>/swap_space&lt;span style="color:#e6db74">`&lt;/span> 更安全：
sudo chmod &lt;span style="color:#ae81ff">600&lt;/span> /swap_space
// 创建交换空间
sudo mkswap /swap_space
// 启用
sudo swapon /swap_space
// 使用 s 参数查看列表
sudo swapon -s
&lt;/code>&lt;/pre>&lt;/div>&lt;p>每次重启后都要重新挂载磁盘分区。因此为了使之持久化，就像上面一样，我们编辑  &lt;code>/etc/fstab&lt;/code>  并输入下面行：&lt;/p>
&lt;pre tabindex="0">&lt;code>/swap_space swap  swap  sw  0  0
&lt;/code>&lt;/pre>&lt;p>保存并退出文件。现在我们的交换分区会一直被挂载了。我们重启后可以在终端运行  &lt;code>free -m&lt;/code>  来检查交换分区是否生效。&lt;/p>
&lt;p>还可以按需选择使用 zram，提升内存可用量，不过会略微增加 cpu 使用和内存延时。可以搜索 zramctl, zramswap 等关键字&lt;/p>
&lt;hr>
&lt;h3 id="17-绑定域名">
1.7 绑定域名  
&lt;a href="#17-%e7%bb%91%e5%ae%9a%e5%9f%9f%e5%90%8d" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>可选，绑定一个域名或者改下本地 host 便于后续访问&lt;/p>
&lt;h3 id="18-添加本地-ssh-别名">
1.8 添加本地 SSH 别名
&lt;a href="#18-%e6%b7%bb%e5%8a%a0%e6%9c%ac%e5%9c%b0-ssh-%e5%88%ab%e5%90%8d" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>配置文件为本机的 &lt;code>~/.ssh/config&lt;/code>
格式如下&lt;/p>
&lt;pre tabindex="0">&lt;code class="language-config" data-lang="config">Host serverA
HostName myserver.domain.xyz
User colinx
Port 2333
&lt;/code>&lt;/pre>&lt;h2 id="2---常用工具与配置">
2 - 常用工具与配置
&lt;a href="#2---%e5%b8%b8%e7%94%a8%e5%b7%a5%e5%85%b7%e4%b8%8e%e9%85%8d%e7%bd%ae" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h2>
&lt;h3 id="21-docker-环境">
2.1 Docker 环境
&lt;a href="#21-docker-%e7%8e%af%e5%a2%83" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;h4 id="docker-安装">
Docker 安装
&lt;a href="#docker-%e5%ae%89%e8%a3%85" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h4>
&lt;p>可参考官网文档，注意是 docker engine 的安装。&lt;a href="https://docs.docker.com/engine/install/debian/">Docker 官方安装文档&lt;/a>&lt;/p>
&lt;p>或者我之前写的 RSS MAN 部署文档中 docker 安装的部分
&lt;a href="https://blog.colinx.one/posts/rssmanx%E5%AE%89%E8%A3%85%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97/#1-docker%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87">RSS MAN X 安装部署指南/#1-docker 环境准备&lt;/a>&lt;/p>
&lt;h4 id="docker-镜像源配置及日志配置">
Docker 镜像源配置及日志配置
&lt;a href="#docker-%e9%95%9c%e5%83%8f%e6%ba%90%e9%85%8d%e7%bd%ae%e5%8f%8a%e6%97%a5%e5%bf%97%e9%85%8d%e7%bd%ae" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h4>
&lt;p>docker mirror 配置可以加速 image pull, 国内公开可用的加速站点可以参考这里 &lt;a href="https://gist.github.com/y0ngb1n/7e8f16af3242c7815e7ca2f0833d3ea6">docker 加速站点&lt;/a>&lt;/p>
&lt;p>此外服务器上的 docker 都是长时间持续运行的，不少容器日志打的很随意，log 文件容易占据过多空间，也最好限制一下。&lt;/p>
&lt;p>文件位置&lt;code>/etc/docker/daemon.json&lt;/code>, 下面的配置供参考&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-json" data-lang="json">{
&lt;span style="color:#f92672">&amp;#34;registry-mirrors&amp;#34;&lt;/span>: [
&lt;span style="color:#e6db74">&amp;#34;https://docker.nastool.de&amp;#34;&lt;/span>,
&lt;span style="color:#e6db74">&amp;#34;https://docker.actima.top&amp;#34;&lt;/span>,
&lt;span style="color:#e6db74">&amp;#34;https://docker.unsee.tech&amp;#34;&lt;/span>,
&lt;span style="color:#e6db74">&amp;#34;https://docker.gh-proxy.com&amp;#34;&lt;/span>,
&lt;span style="color:#e6db74">&amp;#34;https://docker.zhai.cm&amp;#34;&lt;/span>,
&lt;span style="color:#e6db74">&amp;#34;https://docker.1panel.live&amp;#34;&lt;/span>,
&lt;span style="color:#e6db74">&amp;#34;https://docker.1ms.run&amp;#34;&lt;/span>,
&lt;span style="color:#e6db74">&amp;#34;https://docker.imgdb.de&amp;#34;&lt;/span>,
&lt;span style="color:#e6db74">&amp;#34;https://dockerproxy.net&amp;#34;&lt;/span>
],
&lt;span style="color:#f92672">&amp;#34;log-opts&amp;#34;&lt;/span>: {
&lt;span style="color:#f92672">&amp;#34;max-file&amp;#34;&lt;/span>: &lt;span style="color:#e6db74">&amp;#34;5&amp;#34;&lt;/span>,
&lt;span style="color:#f92672">&amp;#34;max-size&amp;#34;&lt;/span>: &lt;span style="color:#e6db74">&amp;#34;1m&amp;#34;&lt;/span>
}
}
&lt;/code>&lt;/pre>&lt;/div>&lt;p>修改完保存重启 docker 服务即可生效。可使用 &lt;code>docker info&lt;/code> 命令检查是否生效&lt;/p>
&lt;h3 id="22-oh-my-zsh">
2.2 OH-MY-ZSH
&lt;a href="#22-oh-my-zsh" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>安装 OH-MY-ZSH, 在此之前确保已安装 &lt;code>git&lt;/code> 和 &lt;code>zsh&lt;/code>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">sh -c &lt;span style="color:#e6db74">&amp;#34;&lt;/span>&lt;span style="color:#66d9ef">$(&lt;/span>curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh&lt;span style="color:#66d9ef">)&lt;/span>&lt;span style="color:#e6db74">&amp;#34;&lt;/span>
&lt;span style="color:#75715e"># 如果在墙内 github 速度不加可以考虑使用清华的镜像&lt;/span>
&lt;span style="color:#75715e"># https://mirrors.tuna.tsinghua.edu.cn/help/ohmyzsh.git/&lt;/span>
&lt;span style="color:#75715e"># 先下载到任意位置，然后指定REMOTE 参数执行安装程序&lt;/span>
git clone https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git --depth&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#ae81ff">1&lt;/span>
cd ohmyzsh/tools
REMOTE&lt;span style="color:#f92672">=&lt;/span>https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git sh install.sh
&lt;/code>&lt;/pre>&lt;/div>&lt;p>安装插件&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions --depth&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#ae81ff">1&lt;/span>
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git &lt;span style="color:#e6db74">${&lt;/span>ZSH_CUSTOM&lt;span style="color:#66d9ef">:-&lt;/span>~/.oh-my-zsh/custom&lt;span style="color:#e6db74">}&lt;/span>/plugins/zsh-syntax-highlighting --depth&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#ae81ff">1&lt;/span>
&lt;/code>&lt;/pre>&lt;/div>&lt;p>git clone 速度不佳可以考虑一些开放的加速服务，比如：&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://ghproxy.org/">https://ghproxy.org/&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://mirror.ghproxy.com/">https://mirror.ghproxy.com/&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>按需修改配置。文件位置&lt;code>~/.zshrc&lt;/code>, 下面为个人常用配置供参考。注意去源文件修改对应项，没有再到末尾加&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">&lt;span style="color:#75715e"># custom conf override&lt;/span>
ZSH_THEME&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;af-magic&amp;#34;&lt;/span>
CASE_SENSITIVE&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;false&amp;#34;&lt;/span>
HYPHEN_INSENSITIVE&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;false&amp;#34;&lt;/span>
plugins&lt;span style="color:#f92672">=(&lt;/span>git z zsh-autosuggestions zsh-syntax-highlighting sudo&lt;span style="color:#f92672">)&lt;/span>
&lt;/code>&lt;/pre>&lt;/div>&lt;p>自定义配置，添加到末尾&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">export ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#ae81ff">20&lt;/span>
&lt;/code>&lt;/pre>&lt;/div>&lt;p>保存并应用&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">source ~/.zshrc
&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="23-nano-代码文件规则">
2.3 nano 代码文件规则
&lt;a href="#23-nano-%e4%bb%a3%e7%a0%81%e6%96%87%e4%bb%b6%e8%a7%84%e5%88%99" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>日常常用文本编辑器为 nano, 轻量级编辑需求完全满足。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">curl https://cdn.jsdelivr.net/gh/scopatz/nanorc/install.sh | sh
&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="24-时区调整">
2.4 时区调整
&lt;a href="#24-%e6%97%b6%e5%8c%ba%e8%b0%83%e6%95%b4" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>一般安装完都是 UTC+0, 看日志什么的不方便。服务器初始化的时候配置好后面可以免去很多麻烦&lt;/p>
&lt;p>debian 系可用过 &lt;code>timedatectl&lt;/code> 命令调整时区。东八区可以用这个命令&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">sudo timedatectl set-timezone Asia/Shanghai
&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="25-厂商监控sdk-卸载">
2.5 厂商监控/SDK 卸载
&lt;a href="#25-%e5%8e%82%e5%95%86%e7%9b%91%e6%8e%a7sdk-%e5%8d%b8%e8%bd%bd" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>懂得都懂，自己搜&lt;/p>
&lt;h3 id="26-fail2ban-配置">
2.6 fail2ban 配置
&lt;a href="#26-fail2ban-%e9%85%8d%e7%bd%ae" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>使用 fail2ban 可以很好地保护你的服务器，避免被人恶意爆破 SSH 等服务。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-bash" data-lang="bash">// 安装 fail2ban
sudo apt update &lt;span style="color:#f92672">&amp;amp;&amp;amp;&lt;/span> sudo apt install fail2ban
sudo systemctl enable fail2ban
&lt;/code>&lt;/pre>&lt;/div>&lt;p>之后需要按照实际情况修改一下配置文件。这里记录一下最小配置。注意默认的配置 &lt;code>/etc/fail2ban/jail.conf&lt;/code>不要改，不然每次软件更新会被覆盖。在 jaid.d 这个目录下面新建一个文件&lt;code>/etc/fail2ban/jail.d/local.conf&lt;/code>&lt;/p>
&lt;pre tabindex="0">&lt;code class="language-conf" data-lang="conf">[sshd]
enabled = true
# 这里修改为实际的 sshd 端口
port = 20000
filter = sshd
banaction = iptables-allports
[DEFAULT]
# 1h 时间窗口
findtime = 3600
maxretry = 3
bantime = 6h
&lt;/code>&lt;/pre>&lt;p>之后重启&lt;code>sudo systemctl restart fail2ban&lt;/code>，然后可以看下服务状态是否正常 &lt;code>sudo systemctl status fail2ban&lt;/code>，如果配置文件有问题会报错。如果是显示&lt;code> active (running)&lt;/code> 就说明没有问题了。&lt;/p>
&lt;p>fail2ban 的测试及关闭服务方法：&lt;/p>
&lt;p>查看当前封禁 IP：&lt;code>sudo fail2ban-client status sshd&lt;/code>&lt;br>
解禁某一 IP: &lt;code>sudo fail2ban-client set sshd unbanip IP_ADDRESS&lt;/code>&lt;br>
停止 fail2ban 服务：&lt;code>sudo systemctl stop fail2ban&lt;/code>&lt;br>
关闭 fail2ban 服务：&lt;code>sudo systemctl disable fail2ban&lt;/code>&lt;/p>
&lt;p>刚配置没一会就有 IP 被封禁了，可以看到效果还是很给力，也安心了不少&lt;/p>
&lt;pre tabindex="0">&lt;code>➜ sudo fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 1
| |- Total failed: 6
| `- File list: /var/log/auth.log
`- Actions
|- Currently banned: 1
|- Total banned: 1
`- Banned IP list: 154.216.19.42
&lt;/code>&lt;/pre>&lt;h3 id="27-git-代理">
2.7 Git 代理
&lt;a href="#27-git-%e4%bb%a3%e7%90%86" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>一般在 &lt;code>.bashrc&lt;/code>/&lt;code>.zshrc&lt;/code> 中定义 &lt;code>HTTP_PROXY&lt;/code> / &lt;code>HTTPS_PROXY&lt;/code> 可以应对大部分场景。但是对于使用 ssh 协议的 git 仓库 (通过 ssh 方式，相较于 https 的不用每次输用户名和密码。一般对只有克隆公共仓库的时候才会用 https, 其他时候对于自己的仓库我都是用 ssh), 并不会走 http 代理。&lt;/p>
&lt;p>这里需要在 ssh config 中配置转发走代理才行。使用&lt;code>nc&lt;/code>(macOS 等默认会预装) 搭配 socks 代理最方便&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-ini" data-lang="ini">&lt;span style="color:#a6e22e">Host github.com&lt;/span>
&lt;span style="color:#a6e22e">HostName github.com&lt;/span>
&lt;span style="color:#a6e22e">User git&lt;/span>
&lt;span style="color:#a6e22e">Port 22&lt;/span>
&lt;span style="color:#a6e22e">ProxyCommand nc -x 192.168.100.222:8085 %h %p&lt;/span>
&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="3---进阶内容">
3 - 进阶内容
&lt;a href="#3---%e8%bf%9b%e9%98%b6%e5%86%85%e5%ae%b9" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h2>
&lt;h3 id="31-内核参数调优">
3.1 内核参数调优
&lt;a href="#31-%e5%86%85%e6%a0%b8%e5%8f%82%e6%95%b0%e8%b0%83%e4%bc%98" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>一些内核参数调整，交换内存阈值和 bbr, tcp fast open 等，按需启用。启用前务必确认自己了解对应字段的含义，否则不如保留系统初始值。&lt;/p>
&lt;p>配置文件位置 &lt;code>/etc/sysctl.conf&lt;/code>&lt;/p>
&lt;pre tabindex="0">&lt;code class="language-conf" data-lang="conf"># mem
vm.swappiness = 10
# bbr and network tune 
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
net.ipv4.tcp_fastopen=3
net.ipv4.tcp_slow_start_after_idle=0
# zram 相关配置，配合下文 zram 使用
vm.watermark_boost_factor = 0
vm.watermark_scale_factor = 125
vm.page-cluster = 0
&lt;/code>&lt;/pre>&lt;p>配置完之后保存并应用&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">sudo sysctl -p
&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="32-更多内存空间---zram">
3.2 更多内存空间 - ZRAM
&lt;a href="#32-%e6%9b%b4%e5%a4%9a%e5%86%85%e5%ad%98%e7%a9%ba%e9%97%b4---zram" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>ZRAM 内存压缩，对于个人服务器来说，始终活跃的程序占用的内存只有比较小一部分，启用 zram 可以将这部分内存压缩，从而可以部署更多的服务。&lt;/p>
&lt;p>建议使用&lt;code>zramtool&lt;/code>, 简单方便&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-shell" data-lang="shell">sudo apt install zram-tools
&lt;/code>&lt;/pre>&lt;/div>&lt;p>修改&lt;code>/etc/default/zramswap&lt;/code>, 末尾添加&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-ini" data-lang="ini">&lt;span style="color:#a6e22e">ALGO&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">zstd&lt;/span>
&lt;span style="color:#a6e22e">PERCENT&lt;/span>&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">60&lt;/span>
&lt;/code>&lt;/pre>&lt;/div>&lt;p>之后执行&lt;code>sudo service zramswap reload&lt;/code>重载生效。
可以使用 swapon 命令查看生效情况&lt;/p>
&lt;pre tabindex="0">&lt;code>~ » sudo swapon -show
NAME TYPE SIZE USED PRIO
/swap_space file 2G 32.1M -2
/dev/zram0 partition 489.9M 0B 100
&lt;/code>&lt;/pre>&lt;p>这样一来，会将机器物理内存的 50% 配置 zram, 使用 zstd 压缩算法，一般压缩比能达到 3:1, 也就是说，1G 的机器，可以相当于有&lt;code>0.5G+3*0.5G=2G&lt;/code>的内存。&lt;/p>
&lt;p>注意 Debian 新版本 swapon 等工具所在的目录 /usr/sbin 不在普通用户的 PATH 中，可能需要手动执行前缀使用。如 /usr/sbin/swapon&lt;/p>
&lt;h3 id="33-更多内存空间---关闭-kdump释放预留的内存空间">
3.3 更多内存空间 - 关闭 kdump，释放预留的内存空间
&lt;a href="#33-%e6%9b%b4%e5%a4%9a%e5%86%85%e5%ad%98%e7%a9%ba%e9%97%b4---%e5%85%b3%e9%97%ad-kdump%e9%87%8a%e6%94%be%e9%a2%84%e7%95%99%e7%9a%84%e5%86%85%e5%ad%98%e7%a9%ba%e9%97%b4" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>新服务器开起来之后，你会发现无论是 htop 还是 free 等命令，看到的总内存就是比你买服务器时页面上写的要小。比如我开的两核 1G 的机器，实际机器上看到的总内存只有 816M. 这是因为 linux 系统会默认开启 kdump，预留了一部分内存不让我们使用。&lt;/p>
&lt;p>Kdump 是 Linux 系统的一种内核崩溃转储机制，它允许在系统发生内核崩溃（例如内核 panic）时，捕获内存的转储信息，从而帮助事后分析故障原因。默认的配置&lt;code>2G-8G:256M,8G-16G:512M,16G-:768M&lt;/code>, 如果你买的是 2G 的机器，上来就有 1/8 的空间用不了。&lt;/p>
&lt;p>但是我们个人日常使用的 linux 机器基本用不到这个功能，不用预留这部分空间。&lt;/p>
&lt;p>操作方法有两种：将预留内存改为 0 or 直接关闭 kdump 服务。对于个人玩家小内存机器我建议直接关闭。操作如下：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-bash" data-lang="bash">&lt;span style="color:#75715e"># 备份grub配置文件&lt;/span>
sudo cp /etc/default/grub /etc/default/grub.bak
&lt;span style="color:#75715e"># 编辑grub配置,删除 crashkernel=0M-2G:0M,2G-8G:192M 这样的部分 &lt;/span>
sudo nano /etc/default/grub
&lt;span style="color:#75715e"># 部分发行版镜像可能还有单独的kdump配置文件,直接删除&lt;/span>
sudo rm /etc/default/grub.d/kdump-tools.cfg
&lt;span style="color:#75715e"># 更新grub配置&lt;/span>
sudo update-grub
&lt;span style="color:#75715e"># 关闭并禁用Kdump服务&lt;/span>
sudo systemctl disable kdump-tools
sudo systemctl stop kdump-tools
&lt;span style="color:#75715e"># 重启系统&lt;/span>
sudo reboot
&lt;span style="color:#75715e"># 检查是否生效, 状态为 inactive(dead)，即 kdump 服务已停止运行&lt;/span>
sudo systemctl status kdump
&lt;span style="color:#75715e"># 检查 crash size 是否为0&lt;/span>
cat /sys/kernel/kexec_crash_size
&lt;/code>&lt;/pre>&lt;/div>&lt;p>更多内容可以参见这篇文档：&lt;a href="https://help.aliyun.com/zh/ecs/use-cases/view-and-change-the-size-of-reserved-memory-on-a-linux-instance#7d5ae6803amdp">预留内存相关文档 - 阿里云&lt;/a> 或者 &lt;a href="https://cloud.tencent.com/document/product/213/115734">预留内存相关文档 - 腾讯云&lt;/a>&lt;/p>
&lt;h3 id="33-其他进阶配置">
3.3 其他进阶配置
&lt;a href="#33-%e5%85%b6%e4%bb%96%e8%bf%9b%e9%98%b6%e9%85%8d%e7%bd%ae" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h3>
&lt;p>一些其他的系统维护技巧与策略，如&lt;/p>
&lt;p>&lt;strong>配置文件管理&lt;/strong>
所有应用 docker 化，通过 &lt;code>docker compose&lt;/code> 文件管理
配置共享存储，&lt;code>rclone&lt;/code> 挂载 webdav, 同步 docker compose 等配置文件;
traefik 网关作为统一出口，负责服务发现和自动维护 HTTPS 证书，自定义配置通过 headless CMS 如 directus 管理，traefik 设定为通过 http 方式获取远端配置即可。&lt;/p>
&lt;p>&lt;strong>数据库备份&lt;/strong>
所有数据相关的统一挂载到&lt;code>/data/database/xxx&lt;/code>, 配置定时任务进行备份
以及配置 s3 等，上传到其他存储介质和其他地域。&lt;/p>
&lt;p>这些内容此处不再赘述，有机会再单独写篇文章分享吧&lt;/p>
&lt;p>END&lt;/p>
&lt;hr>
&lt;h2 id="附录">
附录
&lt;a href="#%e9%99%84%e5%bd%95" class="h-anchor" aria-hidden="true">#&lt;/a>
&lt;/h2>
&lt;ul>
&lt;li>Install Docker Engine &lt;a href="https://docs.docker.com/engine/install/">https://docs.docker.com/engine/install/&lt;/a>&lt;/li>
&lt;li>RSSManX 安装部署指南 &lt;a href="https://blog.colinx.one/posts/rssmanx%E5%AE%89%E8%A3%85%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97/">https://blog.colinx.one/posts/rssmanx%E5%AE%89%E8%A3%85%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97/&lt;/a>&lt;/li>
&lt;li>云服务器入门指南 &lt;a href="https://blog.colinx.one/posts/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/">https://blog.colinx.one/posts/%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/&lt;/a>&lt;/li>
&lt;li>Portainer Install Doc &lt;a href="https://docs.portainer.io/start/install-ce/server/docker/linux">https://docs.portainer.io/start/install-ce/server/docker/linux&lt;/a>&lt;/li>
&lt;/ul></description></item></channel></rss>