172 lines
3.4 KiB
Bash

#!/bin/bash
# 设置错误时立即退出
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 日志函数
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 更新系统
update_system() {
log_info "更新系统..."
apt-get update
apt-get upgrade -y
apt-get dist-upgrade -y
}
# 安装基础工具
install_basic_tools() {
log_info "安装基础工具..."
# 安装常用工具
apt-get install -y \
curl \
wget \
git \
vim \
htop \
tmux \
ripgrep \
fd-find \
bat \
exa \
jq \
httpie \
tldr \
glances \
neofetch \
zsh \
build-essential \
software-properties-common \
ca-certificates \
gnupg \
lsb-release \
apt-transport-https \
python3-pip \
python3-venv \
locales \
tzdata
}
# 配置系统语言和时区
configure_locale() {
log_info "配置系统语言和时区..."
# 生成UTF-8语言环境
locale-gen en_US.UTF-8
update-locale LANG=en_US.UTF-8
# 设置时区为Asia/Shanghai
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo "Asia/Shanghai" > /etc/timezone
}
# 配置系统参数
configure_system() {
log_info "配置系统参数..."
# 创建/etc/sysctl.d/99-sysctl.conf文件
cat > /etc/sysctl.d/99-sysctl.conf << EOF
# 系统最大文件描述符
fs.file-max = 65535
# 系统最大进程数
kernel.pid_max = 65535
# 系统最大打开文件数
fs.nr_open = 65535
# 系统最大虚拟内存
vm.max_map_count = 262144
# 系统最大共享内存
kernel.shm.max = 68719476736
# 系统最大消息队列
kernel.msgmnb = 65535
# 系统最大信号量
kernel.sem = 250 32000 32 128
# TCP连接相关参数
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_fastopen = 1
net.ipv4.tcp_rmem = 4096 87380 67108864
net.ipv4.tcp_wmem = 4096 65536 67108864
net.ipv4.tcp_mtu_probing = 1
# 系统最大连接数
net.core.somaxconn = 32768
net.core.netdev_max_backlog = 32768
net.ipv4.ip_local_port_range = 1024 65535
EOF
# 应用系统参数
sysctl -p /etc/sysctl.d/99-sysctl.conf
}
# 配置系统安全
configure_security() {
log_info "配置系统安全..."
# 配置SSH安全
if [ -f /etc/ssh/sshd_config ]; then
sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
systemctl restart sshd
fi
# 配置防火墙
if command -v ufw &> /dev/null; then
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw --force enable
fi
}
# 主函数
main() {
log_info "开始配置基础系统..."
# 更新系统
update_system
# 安装基础工具
install_basic_tools
# 配置系统语言和时区
configure_locale
# 配置系统参数
configure_system
# 配置系统安全
configure_security
log_info "基础系统配置完成!"
}
# 执行主函数
main