# VPS 的默认配置



## 一、系统配置

### 更新系统

```shell
apt update && apt full-upgrade -y && apt autoremove -y && apt autoclean -y
```

### 安装常用工具

```shell
apt install sudo htop git wget curl vim tmux net-tools unattended-upgrades fail2ban -y
```

### 配置自动更新策略

编辑文件：

```shell
sudo vim /etc/apt/apt.conf.d/20auto-upgrades
```

修改为：

```shell
APT::Periodic::Update-Package-Lists "1";      // 每天更新软件包列表
APT::Periodic::Unattended-Upgrade "1";        // 每天自动安装安全更新
APT::Periodic::AutocleanInterval "7";         // 每周自动清理无用包
```

### 设置内核更新后自动重启

编辑文件：

```shell
sudo vim /etc/apt/apt.conf.d/50unattended-upgrades
```

找到该项取消注释并修改为：

```shell
Unattended-Upgrade::Automatic-Reboot "true";
```

可选：指定重启时间（推荐凌晨）

```shell
Unattended-Upgrade::Automatic-Reboot-Time "04:00";
```

------

### 验证自动更新是否启用

运行：

```shell
systemctl status unattended-upgrades
```

如果显示 `active (running)`，说明已启用。

### 启动 fail2ban

```shell
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
```

### 开启BBR (境外服务器)

root 账户执行

```shell
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf

echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf

sysctl -p
```

检查 bbr 状态

```shell
sysctl net.ipv4.tcp_congestion_control | grep bbr
```

## 二、创建非 root 用户并启用 SSH 密钥登录

### 创建非 root 用户

```shell
sudo adduser fpc
```

### 赋予 sudo 权限

```shell
sudo usermod -aG sudo fpc
```

### 切换到新用户

```shell
su - fpc
```

确认当前用户已经变为 `fpc`。

------

### 生成 SSH 密钥（在本地电脑执行）

如果你还没有 SSH 密钥，请在本地终端运行：

```shell
ssh-keygen -t ed25519 -C "fupcode@outlook.com"
```

执行后会生成：

- 私钥（保存在本地）
- 公钥（用于上传到服务器）

默认路径：

```
~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub
```

------

### 将公钥添加到服务器

在服务器上（确保当前用户为 `fpc`）执行：

```shell
mkdir -p ~/.ssh
chmod 700 ~/.ssh
```

然后将本地公钥内容复制粘贴到：

```shell
vim ~/.ssh/authorized_keys
```

粘贴公钥后保存。

设置权限：

```shell
chmod 600 ~/.ssh/authorized_keys
```

### 禁止 root 登录以及禁止密码登录

编辑 SSH 配置文件：

```shell
sudo vim /etc/ssh/sshd_config
```

末尾添加

```
PermitRootLogin no
PasswordAuthentication no
```

## 重启 SSH 服务

```shell
sudo systemctl restart ssh
```

> [!WARNING]
> 在关闭当前 root 会话前，请确保可以使用 `fpc` 用户以**SSH 密钥方式**登录，否则可能会被锁在服务器外。

## 三、用户配置

### git配置

设置 git 账户信息，指定 git 的编辑器。

```shell
git config --global user.name "fpc"

git config --global user.email "fupcode@outlook.com"

git config --global core.editor "vim"
```

### 上传 SSH 密钥至 GitHub

创建密钥

```shell
ssh-keygen -t ed25519 -C "fupcode@outlook.com"
```

执行

```shell
cat ~/.ssh/id_ed25519.pub
```

复制到 GitHub

### 配置 Zsh 与自定义主题

安装 zsh 与基础工具：

```shell
sudo apt update
sudo apt install zsh -y
chsh -s $(which zsh)
```

重新登录服务器，遇到 `zsh` 配置界面直接按 `q`

安装 oh-my-zsh：

```shell
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
```

重新登录一次。

下载并安装自定义主题：

```shell
mkdir -p ~/.oh-my-zsh/custom/themes
wget https://fupcode.com/shared/fpc.zsh-theme.tar.gz
tar xzvf fpc.zsh-theme.tar.gz -C ~/.oh-my-zsh/custom/themes/
```

编辑配置文件：

```shell
vim ~/.zshrc
```

修改主题与插件：

```shell
ZSH_THEME="fpc"

plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
    z
)
```

最后增加个人自用的 `shell` 配置：

```shell
alias la='ls -A'
alias ll='ls -lh'
alias l='ls -lah'
alias va='. .venv/bin/activate'
alias da='deactivate'
```

安装插件：

```shell
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，无需额外安装。

加载配置：

```shell
source ~/.zshrc
```


---

> 作者: [fupcode](https://github.com/fupcode)  
> URL: https://fupcode.com/blog/posts/devops/vps-configure/  

