暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

CentOS 7.9 系统初始化脚本

老柴杂货铺 2025-02-22
128
初始化配置,包括添加用户、设置防火墙和SELINUX、网络设置和服务设置。比如创建用户并加入wheel组,禁用SELINUX,设置静态IP,禁用IPv6,这些步骤都是初始化的重要部分。需要把这些操作转化为脚本命令。
    #!/bin/bash
    # CentOS 7.9 系统初始化脚本
    # 功能:用户管理、安全配置、网络优化、软件安装、内核调优
    # 执行要求:需以root用户运行


    # ------------------------- 初始化检查 -------------------------
    if [ "$(id -u)" != "0" ]; then
        echo "错误:必须使用root权限执行此脚本!"
        exit 1
    fi


    # ------------------------- 用户与权限管理 -------------------------
    # 1. 创建管理员用户并配置sudo权限
    useradd -m -s bin/bash admin
    echo "admin:YourSecurePassword123!" | chpasswd
    usermod -aG wheel admin
    sed -i 's/# %wheel ALL=(ALLALL/%wheel ALL=(ALL) ALL/g' /etc/sudoers


    2. 禁止root远程登录
    sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' etc/ssh/sshd_config


    ------------------------- 安全加固 -------------------------
    3SSH安全配置
    sed -i 's/#Port 22/Port 22222/g' etc/ssh/sshd_config
    sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' etc/ssh/sshd_config
    sed -i 's/UseDNS yes/UseDNS no/g' etc/ssh/sshd_config
    echo "Protocol 2" >> /etc/ssh/sshd_config


    # 4. 禁用SELinux
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    setenforce 0


    # 5. 防火墙配置(可选)
    systemctl stop firewalld
    systemctl disable firewalld
    # 若需启用防火墙:
    # firewall-cmd --permanent --add-port=22222/tcp
    # firewall-cmd --reload


    ------------------------- 网络配置 -------------------------
    6. 静态IP配置模板
    cat > /etc/sysconfig/network-scripts/ifcfg-ens33 <<EOF
    TYPE=Ethernet
    BOOTPROTO=static
    DEVICE=ens33
    ONBOOT=yes
    IPADDR=192.168.1.100
    NETMASK=255.255.255.0
    GATEWAY=192.168.1.1
    DNS1=8.8.8.8
    DNS2=114.114.114.114
    EOF


    7. 重启网络服务
    systemctl restart network


    ------------------------- 软件源与工具安装 -------------------------
    8. 更换阿里云yum源(-22
    mv /etc/yum.repos.d/CentOS-Base.repo etc/yum.repos.d/CentOS-Base.repo.backup
    curl -/etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    sed -i 's/gpgcheck=1/gpgcheck=0/g' /etc/yum.repos.d/CentOS-Base.repo


    # 9. 安装EPEL源和基础工具
    yum install -y epel-release
    yum 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/Shanghai
    systemctl enable chronyd && systemctl start chronyd


    ------------------------- 系统优化 -------------------------
    11. 内核参数调优
    cat >> /etc/sysctl.conf <<EOF
    net.ipv4.tcp_syncookies = 1
    net.ipv4.tcp_max_syn_backlog = 4096
    net.core.somaxconn = 4096
    net.ipv4.tcp_tw_reuse = 1
    vm.swappiness = 10
    EOF
    sysctl -p


    12. 文件描述符限制
    echo "* soft nofile 65535" >> /etc/security/limits.conf
    echo "* hard nofile 65535" >> etc/security/limits.conf


    13. 服务管理
    systemctl stop postfix && systemctl disable postfix
    systemctl mask NetworkManager


    ------------------------- 自定义配置 -------------------------
    14VIM优化配置
    cat > /etc/vimrc <<EOF
    set tabstop=4
    set shiftwidth=4
    set expandtab
    set number
    set showcmd
    syntax on
    EOF


    15. 历史命令优化
    echo "export HISTTIMEFORMAT=\"%F %T \"" >> /etc/profile
    echo "export HISTSIZE=10000" >> /etc/profile


    ------------------------- 收尾工作 -------------------------
    16. 系统更新与清理
    yum update -y
    yum clean all
    rm -rf /var/cache/yum


    17. 重启服务
    systemctl restart sshd
    echo "=============== 初始化完成,建议重启系统 ==============="


    脚本功能详解

    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进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

    评论