#!/bin/bash# CentOS 7.9 系统初始化脚本# 功能:用户管理、安全配置、网络优化、软件安装、内核调优# 执行要求:需以root用户运行# ------------------------- 初始化检查 -------------------------if [ "$(id -u)" != "0" ]; thenecho "错误:必须使用root权限执行此脚本!"exit 1fi# ------------------------- 用户与权限管理 -------------------------# 1. 创建管理员用户并配置sudo权限useradd -m -s bin/bash adminecho "admin:YourSecurePassword123!" | chpasswdusermod -aG wheel adminsed -i 's/# %wheel ALL=(ALL) ALL/%wheel ALL=(ALL) ALL/g' /etc/sudoers# 2. 禁止root远程登录sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' etc/ssh/sshd_config# ------------------------- 安全加固 -------------------------# 3. SSH安全配置sed -i 's/#Port 22/Port 22222/g' etc/ssh/sshd_configsed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' etc/ssh/sshd_configsed -i 's/UseDNS yes/UseDNS no/g' etc/ssh/sshd_configecho "Protocol 2" >> /etc/ssh/sshd_config# 4. 禁用SELinuxsed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/configsetenforce 0# 5. 防火墙配置(可选)systemctl stop firewalldsystemctl disable firewalld# 若需启用防火墙:# firewall-cmd --permanent --add-port=22222/tcp# firewall-cmd --reload# ------------------------- 网络配置 -------------------------# 6. 静态IP配置模板cat > /etc/sysconfig/network-scripts/ifcfg-ens33 <<EOFTYPE=EthernetBOOTPROTO=staticDEVICE=ens33ONBOOT=yesIPADDR=192.168.1.100NETMASK=255.255.255.0GATEWAY=192.168.1.1DNS1=8.8.8.8DNS2=114.114.114.114EOF# 7. 重启网络服务systemctl restart network# ------------------------- 软件源与工具安装 -------------------------# 8. 更换阿里云yum源(-22)mv /etc/yum.repos.d/CentOS-Base.repo etc/yum.repos.d/CentOS-Base.repo.backupcurl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.reposed -i 's/gpgcheck=1/gpgcheck=0/g' /etc/yum.repos.d/CentOS-Base.repo# 9. 安装EPEL源和基础工具yum install -y epel-releaseyum install -y bash-completion vim-enhanced net-tools lsof \htop iftop iotop wget curl unzip lrzsz tmux \chrony git openssl-devel gcc-c++ make# 10. 时间同步配置timedatectl set-timezone Asia/Shanghaisystemctl enable chronyd && systemctl start chronyd# ------------------------- 系统优化 -------------------------# 11. 内核参数调优cat >> /etc/sysctl.conf <<EOFnet.ipv4.tcp_syncookies = 1net.ipv4.tcp_max_syn_backlog = 4096net.core.somaxconn = 4096net.ipv4.tcp_tw_reuse = 1vm.swappiness = 10EOFsysctl -p# 12. 文件描述符限制echo "* soft nofile 65535" >> /etc/security/limits.confecho "* hard nofile 65535" >> etc/security/limits.conf# 13. 服务管理systemctl stop postfix && systemctl disable postfixsystemctl mask NetworkManager# ------------------------- 自定义配置 -------------------------# 14. VIM优化配置cat > /etc/vimrc <<EOFset tabstop=4set shiftwidth=4set expandtabset numberset showcmdsyntax onEOF# 15. 历史命令优化echo "export HISTTIMEFORMAT=\"%F %T \"" >> /etc/profileecho "export HISTSIZE=10000" >> /etc/profile# ------------------------- 收尾工作 -------------------------# 16. 系统更新与清理yum update -yyum clean allrm -rf /var/cache/yum# 17. 重启服务systemctl restart sshdecho "=============== 初始化完成,建议重启系统 ==============="
脚本功能详解
1. 用户与权限管理
创建具有sudo权限的普通用户admin,禁用root远程登录
通过wheel组实现权限分级管理,符合最小权限原则
2. 安全加固
SSH端口改为22222,禁用GSSAPI和DNS反向解析
关闭SELinux(生产环境建议按需配置)和防火墙
密钥登录配置建议(需手动部署公钥)
3. 网络配置
静态IP模板支持快速修改
DNS配置支持国内外双解析
4. 软件管理
阿里云源加速软件下载
安装开发调试工具包
时间同步确保日志准确性
5. 性能优化
内核参数优化TCP连接性能
文件描述符限制提升服务承载能力
禁用不必要服务释放资源
执行说明
保存为centos7-init.sh
赋予执行权限:chmod +x centos7-init.sh
按需修改以下参数:
第6节:静态IP地址
第1节:管理员密码
第3节:SSH端口号
执行脚本:./centos7-init.sh
文章转载自老柴杂货铺,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




