VPS配置

AI 生成

VPS基础配置指南

本文介绍VPS服务器的基础配置,包括用户管理、安全加固、服务管理和代理设置。

用户管理

添加新用户

1
2
3
4
5
6
# 创建新用户
adduser username

# 或使用useradd(需手动设置更多参数)
useradd -m -s /bin/bash username
passwd username

用户权限管理

1
2
3
4
5
6
7
8
9
# 将用户添加到sudo组
usermod -aG sudo username

# 或在CentOS中添加到wheel组
usermod -aG wheel username

# 编辑sudoers文件
visudo
# 添加: username ALL=(ALL:ALL) ALL

修改密码

1
2
3
4
5
# 修改自己的密码
passwd

# 修改其他用户密码(需要root权限)
passwd username

配置软件源

软件源是系统获取软件包的地址,配置合适的软件源可以加快软件安装和更新速度。

Ubuntu/Debian软件源配置

Ubuntu/Debian的软件源配置文件位于/etc/apt/sources.list

1
2
3
4
5
# 备份原配置
cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 编辑源配置文件
vim /etc/apt/sources.list

Ubuntu 22.04(Jammy)阿里云源示例

1
2
3
4
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse

Debian 12(Bookworm)清华源示例

1
2
3
4
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware

更新软件包列表:

1
2
apt update
apt upgrade

CentOS/RHEL软件源配置

CentOS 7 阿里云源

1
2
3
4
5
6
7
8
9
# 备份原repo文件
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

# 下载阿里云repo文件
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

# 清除并重建缓存
yum clean all
yum makecache

CentOS 8 Stream/RHEL 8 源配置

1
2
3
4
5
6
7
8
9
10
11
# 备份原配置
mv /etc/yum.repos.d/CentOS-Stream-BaseOS.repo /etc/yum.repos.d/CentOS-Stream-BaseOS.repo.backup
mv /etc/yum.repos.d/CentOS-Stream-AppStream.repo /etc/yum.repos.d/CentOS-Stream-AppStream.repo.backup

# 下载新的repo文件
curl -o /etc/yum.repos.d/CentOS-Stream-BaseOS.repo https://mirrors.aliyun.com/repo/CentOS-Stream-8-BaseOS.repo
curl -o /etc/yum.repos.d/CentOS-Stream-AppStream.repo https://mirrors.aliyun.com/repo/CentOS-Stream-8-AppStream.repo

# 更新缓存
dnf clean all
dnf makecache

常用镜像源列表

国家/地区 镜像源名称 地址
中国大陆 阿里云 mirrors.aliyun.com
中国大陆 清华源 mirrors.tuna.tsinghua.edu.cn
中国大陆 中科大 mirrors.ustc.edu.cn
国际 官方源 archive.ubuntu.com (Ubuntu) / deb.debian.org (Debian)

自动选择最快的镜像源

Ubuntu/Debian

1
2
3
4
5
# 安装netselect-apt
apt install netselect-apt

# 自动选择最快的源并生成sources.list
netselect-apt -n -o /etc/apt/sources.list

CentOS/RHEL

1
2
3
4
5
6
7
8
# 安装yum-plugin-fastestmirror
yum install yum-plugin-fastestmirror

# 启用插件,编辑/etc/yum/pluginconf.d/fastestmirror.conf
echo "enabled=1" > /etc/yum/pluginconf.d/fastestmirror.conf

# 清除缓存
yum clean all

文件权限管理

基本权限命令

1
2
3
4
5
6
7
8
9
10
11
12
13
# 更改文件所有者
chown user:group filename

# 递归更改目录及其内容的所有者
chown -R user:group directory

# 更改文件权限
chmod 755 filename # rwxr-xr-x
chmod 644 filename # rw-r--r--
chmod 600 filename # rw-------

# 递归更改目录权限
chmod -R 755 directory

重要文件的推荐权限

1
2
3
4
5
6
7
8
9
# SSH密钥
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 644 ~/.ssh/authorized_keys
chmod 700 ~/.ssh

# 网站目录(假设用户是www-data)
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html

SSH安全配置

修改SSH端口

编辑/etc/ssh/sshd_config

1
2
3
4
5
6
7
8
9
# 使用vim编辑
vim /etc/ssh/sshd_config

# 找到并修改端口
# Port 22
Port 2222 # 改为非标准端口

# 保存后重启SSH服务
systemctl restart sshd

禁用root登录

编辑/etc/ssh/sshd_config

1
2
3
4
5
6
7
8
9
10
11
12
# 找到并修改
PermitRootLogin no

# 只允许特定用户登录
AllowUsers username1 username2

# 使用密钥认证
PubkeyAuthentication yes
PasswordAuthentication no

# 保存后重启SSH服务
systemctl restart sshd

设置SSH密钥登录

1
2
3
4
5
6
7
# 在本地生成密钥对
ssh-keygen -t ed25519 -C "your_email@example.com"

# 将公钥上传到服务器
ssh-copy-id -i ~/.ssh/id_ed25519.pub username@server_ip
# 或手动添加到authorized_keys
cat ~/.ssh/id_ed25519.pub | ssh username@server_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

服务管理

systemd服务管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 启动服务
systemctl start service-name

# 停止服务
systemctl stop service-name

# 重启服务
systemctl restart service-name

# 查看服务状态
systemctl status service-name

# 设置开机启动
systemctl enable service-name

# 禁用开机启动
systemctl disable service-name

查看服务日志

1
2
3
4
5
6
7
8
9
10
11
# 查看服务日志
journalctl -u service-name

# 查看最近的日志
journalctl -u service-name -n 100

# 实时查看日志
journalctl -u service-name -f

# 查看特定时间段的日志
journalctl -u service-name --since "2023-01-01" --until "2023-01-02 03:00"

创建自定义systemd服务

创建文件 /etc/systemd/system/myapp.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Unit]
Description=My Custom Application
After=network.target

[Service]
Type=simple
User=myuser
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/python3 /opt/myapp/app.py
Restart=on-failure
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=myapp

[Install]
WantedBy=multi-user.target

启用并启动服务:

1
2
3
systemctl daemon-reload
systemctl enable myapp
systemctl start myapp

代理服务配置

1. 简单HTTP代理 - Squid

安装Squid:

1
2
3
4
5
6
# Debian/Ubuntu
apt update
apt install squid

# CentOS/RHEL
yum install squid

基本配置 (/etc/squid/squid.conf):

1
2
3
4
5
6
7
8
9
10
11
# 设置允许访问的网络
acl localnet src 192.168.1.0/24
http_access allow localnet
http_access allow localhost
http_access deny all

# 设置监听端口
http_port 3128

# 保存并重启
systemctl restart squid

2. Shadowsocks代理

安装Shadowsocks:

1
2
3
4
5
# 使用pip安装
pip install shadowsocks

# 或使用包管理器
apt install shadowsocks-libev # Debian/Ubuntu

创建配置文件 /etc/shadowsocks.json

1
2
3
4
5
6
7
8
{
"server":"0.0.0.0",
"server_port":8388,
"password":"your_password",
"timeout":300,
"method":"aes-256-gcm",
"fast_open":true
}

创建systemd服务 /etc/systemd/system/shadowsocks.service

1
2
3
4
5
6
7
8
9
10
11
[Unit]
Description=Shadowsocks Server
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/ssserver -c /etc/shadowsocks.json
Restart=on-failure

[Install]
WantedBy=multi-user.target

启动服务:

1
2
3
systemctl daemon-reload
systemctl enable shadowsocks
systemctl start shadowsocks

3. V2Ray代理

详细配置请参考《V2Ray配置教程》

基本安全加固

防火墙配置 (UFW)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 安装UFW
apt install ufw

# 默认拒绝入站,允许出站
ufw default deny incoming
ufw default allow outgoing

# 允许SSH(假设SSH端口已更改为2222)
ufw allow 2222/tcp

# 允许Web服务
ufw allow 80/tcp
ufw allow 443/tcp

# 启用防火墙
ufw enable

# 查看状态
ufw status

防火墙配置 (firewalld)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 安装firewalld(CentOS/RHEL常用)
yum install firewalld

# 启动并设置开机启动
systemctl start firewalld
systemctl enable firewalld

# 添加服务或端口
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --permanent --add-port=2222/tcp

# 移除默认的SSH服务(如果已更改SSH端口)
firewall-cmd --permanent --remove-service=ssh

# 重新加载配置
firewall-cmd --reload

自动安全更新

1
2
3
4
5
6
7
8
# Debian/Ubuntu
apt install unattended-upgrades
dpkg-reconfigure unattended-upgrades

# CentOS/RHEL
yum install yum-cron
systemctl enable yum-cron
systemctl start yum-cron

进阶扩展

本文只涵盖了VPS配置的基础内容,更多高级主题请参考上述链接的专门文章。