一、系统配置

更新系统

1
apt update && apt full-upgrade -y && apt autoremove -y && apt autoclean -y

安装常用工具

1
apt install sudo htop git wget curl screen emacs-nox net-tools unattended-upgrades fail2ban -y

配置自动更新策略

编辑文件:

1
sudo vim /etc/apt/apt.conf.d/20auto-upgrades

修改为:

1
2
3
APT::Periodic::Update-Package-Lists "1";      // 每天更新软件包列表
APT::Periodic::Unattended-Upgrade "1"; // 每天自动安装安全更新
APT::Periodic::AutocleanInterval "7"; // 每周自动清理无用包

设置内核更新后自动重启

编辑文件:

1
sudo vim /etc/apt/apt.conf.d/50unattended-upgrades

找到该项取消注释并修改为:

1
Unattended-Upgrade::Automatic-Reboot "true";

可选:指定重启时间(推荐凌晨)

1
Unattended-Upgrade::Automatic-Reboot-Time "02:00";

验证自动更新是否启用

运行:

1
systemctl status unattended-upgrades

如果显示 active (running),说明已启用。

启动 fail2ban

1
2
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

开启BBR

root 账户执行

1
2
3
4
5
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf

echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf

sysctl -p

检查 bbr 状态

1
sysctl net.ipv4.tcp_congestion_control | grep bbr

二、创建非 root 用户并启用 SSH 密钥登录

创建非 root 用户

1
sudo adduser fpc

赋予 sudo 权限

1
sudo usermod -aG sudo fpc

切换到新用户

1
su - fpc

确认当前用户已经变为 fpc


生成 SSH 密钥(在本地电脑执行)

如果你还没有 SSH 密钥,请在本地终端运行:

1
ssh-keygen -t ed25519 -C "fupcode@outlook.com"

执行后会生成:

  • 私钥(保存在本地)
  • 公钥(用于上传到服务器)

默认路径:

1
2
~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub

将公钥添加到服务器

在服务器上(确保当前用户为 fpc)执行:

1
2
mkdir -p ~/.ssh
chmod 700 ~/.ssh

然后将本地公钥内容复制粘贴到:

1
vim ~/.ssh/authorized_keys

粘贴公钥后保存。

设置权限:

1
chmod 600 ~/.ssh/authorized_keys

禁止 root 登录

编辑 SSH 配置文件:

1
sudo vim /etc/ssh/sshd_config

找到并修改以下内容:

1
PermitRootLogin no

重启 SSH 服务

1
sudo systemctl restart ssh

⚠️ 在关闭当前 root 会话前,请确保:

  • 可以使用 fpc 用户登录
  • SSH 密钥认证成功

否则可能会被锁在服务器外。

三、用户配置

git配置

设置 git 账户信息,指定 git 的编辑器。

1
2
3
4
5
git config --global user.name "fpc"

git config --global user.email "fupcode@outlook.com"

git config --global core.editor "vim"

上传 SSH 密钥至 GitHub

创建密钥

1
ssh-keygen -t ed25519 -C "fupcode@outlook.com"

执行

1
cat ~/.ssh/id_ed25519.pub

复制到 GitHub

配置 Zsh 与自定义主题

安装 zsh 与基础工具:

1
2
3
sudo apt update
sudo apt install zsh -y
chsh -s $(which zsh)

重新登录服务器,遇到 zsh 配置界面直接按 q

安装 oh-my-zsh:

1
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

重新登录一次。

下载并安装自定义主题:

1
2
3
mkdir -p ~/.oh-my-zsh/custom/themes
wget https://fupengcheng.top/shared/fpc.zsh-theme.tar.gz
tar xzvf fpc.zsh-theme.tar.gz -C ~/.oh-my-zsh/custom/themes/

编辑配置文件:

1
vim ~/.zshrc

修改主题与插件:

1
2
3
4
5
6
7
8
ZSH_THEME="fpc"

plugins=(
git
zsh-autosuggestions
zsh-syntax-highlighting
z
)

最后增加个人自用的 shell 配置:

1
2
3
4
5
alias la='ls -A'
alias ll='ls -lh'
alias l='ls -lAh'
alias vba='. .venv/bin/activate'
alias da='deactivate'

安装插件:

1
2
3
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

z 插件已内置于 oh-my-zsh,无需额外安装。

加载配置:

1
source ~/.zshrc