龙空技术网

Shell脚本--Linux系统初始化

嘚瑟的淨哥 956

前言:

现时各位老铁们对“centosbash补全”大约比较讲究,兄弟们都需要了解一些“centosbash补全”的相关知识。那么小编也在网络上网罗了一些关于“centosbash补全””的相关内容,希望朋友们能喜欢,姐妹们一起来学习一下吧!

以下是一个基本的Shell脚本,用于CentOS 9系统的初始化,包括安装系统源、关闭SELinux和防火墙、优化sysctl设置、时间同步(使用阿里云NTP服务器)、unlimit文件配置、密码策略配置以及用户锁定等。

#!/bin/bash# 安装CentOS官方源yum install -y epel-release# 关闭SELinuxsed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config# 关闭防火墙systemctl stop firewalldsystemctl disable firewalld# 优化sysctl设置cat <<EOF > /etc/sysctl.conf# 最大打开文件数fs.file-max = 100000# 提高系统进程限制kernel.pid_max = 65536# 提高网络性能net.core.somaxconn = 65535net.core.netdev_max_backlog = 32768net.ipv4.tcp_max_syn_backlog = 16384net.ipv4.tcp_syncookies = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_fin_timeout = 10# 改善TCP连接数限制net.ipv4.ip_local_port_range = 1024 65535# 防止放大攻击net.ipv4.icmp_echo_ignore_broadcasts = 1net.ipv4.icmp_ignore_bogus_error_responses = 1# 关闭IPv6net.ipv6.conf.all.disable_ipv6 = 1net.ipv6.conf.default.disable_ipv6 = 1EOFsysctl -p# 时间同步(使用阿里云NTP服务器)yum install -y chronyecho "server ntp.aliyun.com iburst" >> /etc/chrony.confsystemctl enable chronydsystemctl start chronyd# 配置unlimit文件cat <<EOF > /etc/security/limits.d/99-unlimit.conf* soft nofile 65535* hard nofile 65535EOF# 配置密码策略# 修改密码过期天数sed -i 's/PASS_MAX_DAYS\s*[0-9]*/PASS_MAX_DAYS 90/g' /etc/login.defs# 修改密码最小天数sed -i 's/PASS_MIN_DAYS\s*[0-9]*/PASS_MIN_DAYS 1/g' /etc/login.defs# 修改密码警告天数sed -i 's/PASS_WARN_AGE\s*[0-9]*/PASS_WARN_AGE 7/g' /etc/login.defs# 配置密码复杂性要求cat <<EOF > /etc/security/pwquality.conf# 密码最小长度minlen = 8# 密码复杂性要求dcredit = -1ucredit = -1lcredit = -1ocredit = -1EOF# 锁定用户usermod -L rootusermod -s /sbin/nologin root# 完成提示echo "CentOS 9初始化完成!"

请注意,执行此脚本可能会对系统造成一些更改,请确保在运行之前对脚本进行检查,并在生产环境中谨慎使用。

标签: #centosbash补全 #centos命令恢复初始化 #sed关闭selinux