Test0

星 & 晨 | Stars 的第一个test

一、服务器环境初始化

1、检查

1
2
3
4
5
6
7
8
9
cat /etc/os-release && uname -r
lscpu | grep -E 'Model name|CPU\\(s\\)|Core|Thread' && free -h
df -hT && lsblk
systemctl list-units --type=service --state=running
systemctl list-unit-files --type=service --state=enabled
sudo ss -tulnp
sudo sshd -T | grep -E '^(port|permitrootlogin|passwordauthentication|pubkeyauthentication|maxauthtries|clientaliveinterval|clientalivecountmax)'
ulimit -n && ulimit -u && cat /proc/sys/fs/file-max && cat /proc/sys/fs/file-nr
swapon --show && sysctl vm.swappiness net.core.somaxconn

2、创建swap

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 创建swap文件
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' >> tee -a /etc/fstab

tee /etc/sysctl.d/99-swap.conf <<'EOF'
# Swap usage preference
vm.swappiness = 10
EOF
# vm.swappiness=10 表示降低内核主动使用 Swap 的倾向,但不代表只有内存耗尽时才使用 Swap。
sysctl --system            # 加载配置
free -h

3、apt优化,软件安装

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 备份原 sources.list
cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 替换为阿里云镜像(Ubuntu)
sed -i 's@http://.*.ubuntu.com@http://mirrors.aliyun.com@g' /etc/apt/sources.list

# 更新并升级
apt update && sudo apt upgrade -y

# 清理无用依赖和缓存
apt autoremove --purge -y
apt clean

# 安装所需服务
apt -y install lrzsz

4、ssh配置

4.1、本机创建key

1
2
3
win+r
cmd
ssh-keygen -t ed25519

4.2、上传服务器

1
2
3
4
cd /root/.ssh
rz
chmod 600 ./*
cat id_ed25519.pub >> authorized_keys

4.3、ssh优化

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
vim /etc/ssh/sshd_config
# 关闭 DNS 反向解析(避免客户端 IP 解析域名时因 DNS 响应慢导致卡顿)
UseDNS no
UsePAM yes     # PAM配置需要
# 关闭 GSSAPI 认证(非域环境无用,关闭可减少 PAM 开销和 Kerberos 尝试)
GSSAPIAuthentication no
Port 54322
X11Forwarding no
PrintMotd no     # 由PAM管理,ssh关闭
# AllowUsers ops admin

# 限制认证尝试次数与超时时间(防暴力破解)
MaxAuthTries 3
LoginGraceTime 20

# 启用公钥认证,彻底禁用密码认证
PubkeyAuthentication yes
PasswordAuthentication no
ChallengeResponseAuthentication no
KbdInteractiveAuthentication no

# 单个连接最大通道数(根据业务需求调整)
MaxSessions 10

# 未认证连接限制:50个开始随机拒绝,达到200个全拒绝(防慢速 DoS)
MaxStartups 50:30:200

# 心跳保活:每 60 秒发送一次,连续 5 次无响应则断开(清理僵尸连接)
ClientAliveInterval 60
ClientAliveCountMax 5

# 测试配置是否正确
sshd -t
systemctl restart ssh

5、精简服务

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 一个容器包服务,跟apt一样还吃资源
systemctl disable --now snapd.service snapd.socket
apt purge -y snapd
rm -rf /snap /var/snap /var/lib/snapd

# 硬件相关关掉
# multipathd(多路径设备管理)
systemctl disable --now multipathd.service multipathd.socket
# open-iscsi(iSCSI 远程存储)
systemctl disable --now open-iscsi.service iscsid.service iscsid.socket
systemctl mask open-iscsi.service iscsid.service iscsid.socket
# udisks2(磁盘热插拔/自动挂载守护)
systemctl disable --now udisks2.service
# fwupd(固件更新守护)
systemctl disable --now fwupd.service fwupd-refresh.timer

# tuned:性能调优守护,对 2G 小机器意义不大
systemctl disable --now tuned.service

# packagekit 是桌面环境的包管理抽象层,服务器上用 apt 直接管理即可。atd 是一次性定时任务,如果你不需要 at 命令,可以关掉。
systemctl disable --now packagekit.service
systemctl mask packagekit.service    # mask 会阻止任何服务通过依赖关系把它重新拉起。
systemctl disable --now atd.service

# getty@tty1 和 serial-getty@ttyS0 是本地登录终端和串口控制台。
systemctl disable --now getty@tty1.service
systemctl disable --now serial-getty@ttyS0.service

# 更新关闭
systemctl disable --now unattended-upgrades.service
# 如果进程还在,直接杀掉
pkill -f unattended-upgrade-sh

# 移除错误上报和崩溃报告
systemctl disable --now whoopsie apport
apt purge -y whoopsie apport popularity-contest

systemctl list-units --type=service --state=running

6、配置motd

1
2
3
sed -i 's/^ENABLED=.*/ENABLED=0/' /etc/default/motd-news        # 新闻
chmod -x /etc/update-motd.d/*
vim /etc/motd

7、中文支持(如果vim或系统乱码就执行)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 安装中文语言包(提供 zh_CN.UTF-8 的生成素材)
apt update && apt install -y language-pack-zh-hans language-pack-zh-hans-base

# 同时生成中文和英文 UTF-8 locale(英文用于保证 TTY 兼容性)
locale-gen en_US.UTF-8
locale-gen zh_CN.UTF-8
update-locale --reset LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh
grep -nE 'UsePAM|AcceptEnv' /etc/ssh/sshd_config   #看ssh打开这两个配置没PAM yes
cat /etc/default/locale

locale  # 看配置应用没
locale -a 
locale charmap

# 如果vim还有问题就执行
cat > ~/.vimrc <<'EOF'
" UTF-8 encoding
set encoding=utf-8
set fileencoding=utf-8
set fileencodings=utf-8,gb18030,gbk,gb2312

" Display
set number
set cursorline
set wrap

" Search
set hlsearch
set incsearch

" Indentation
set tabstop=4
set shiftwidth=4
set expandtab

" Terminal compatibility
set termguicolors
EOF