Script-Collected/sh/linux/init_linux.sh

121 lines
3.2 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 初始化Linux服务器脚本
# 功能1. 时钟同步配置 2. Yum加速配置 3. 系统基础优化
# 检查是否为root用户
if [ $(id -u) != "0" ]; then
echo "错误请使用root用户执行此脚本"
exit 1
fi
# 定义颜色变量
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
echo -e "${GREEN}开始初始化系统配置...${NC}"
# 1. 配置时钟同步
echo -e "${YELLOW}1. 配置时钟同步服务...${NC}"
# 安装chrony时间同步服务
yum install -y chrony
# 配置chrony使用国内时间服务器
cat > /etc/chrony.conf <<EOF
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
server ntp.ntsc.ac.cn iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
allow 192.168/16
local stratum 10
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony
EOF
# 启动并设置开机自启
systemctl enable chronyd --now
timedatectl set-timezone Asia/Shanghai
chronyc sources
echo -e "${GREEN}时钟同步配置完成${NC}"
# 2. 配置国内Yum镜像源
echo -e "${YELLOW}2. 配置阿里云Yum镜像源...${NC}"
# 备份原配置文件
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
# 生成新的配置文件适配CentOS 7/8
cat > /etc/yum.repos.d/CentOS-Base.repo <<EOF
# CentOS-Base.repo
[base]
name=CentOS-\$releasever - Base - Aliyun
baseurl=http://mirrors.aliyun.com/centos/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[appstream]
name=CentOS-\$releasever - AppStream - Aliyun
baseurl=http://mirrors.aliyun.com/centos/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[extras]
name=CentOS-\$releasever - Extras - Aliyun
baseurl=http://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/os/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[centosplus]
name=CentOS-\$releasever - Plus - Aliyun
baseurl=http://mirrors.aliyun.com/centos/\$releasever/centosplus/\$basearch/os/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
EOF
# 生成缓存
yum clean all
yum makecache
echo -e "${GREEN}Yum镜像源配置完成${NC}"
# 3. 系统基础优化
echo -e "${YELLOW}3. 执行系统基础优化...${NC}"
# 关闭SELinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
# 关闭防火墙(生产环境慎用)
systemctl stop firewalld
systemctl disable firewalld
# 设置最大打开文件数
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf
# 优化内核参数
cat >> /etc/sysctl.conf <<EOF
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_rmem = 4096 87380 6291456
net.ipv4.tcp_wmem = 4096 65536 6291456
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
EOF
sysctl -p
echo -e "${GREEN}系统基础优化完成${NC}"
echo -e "${GREEN}所有初始化配置已完成!建议重启系统后使用${NC}"