发布时间: 2023-6-4 文章作者: myluzh 分类名称: Linux 朗读文章
[root@CentOS-7 ~]# uname -r # 查看当前内核版本 3.10.0-693.5.2.el7.x86_64 [root@CentOS-7 ~]# cat /boot/grub2/grub.cfg |grep "menuentry " # 查看所有可用内核 menuentry 'CentOS Linux (3.10.0-693.5.2.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.el7.x86_64-advanced-88ae7d3d-ffaf-4402-9d44-56b6845789e4' { menuentry 'CentOS Linux (3.10.0-327.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.el7.x86_64-advanced-88ae7d3d-ffaf-4402-9d44-56b6845789e4' { menuentry 'CentOS Linux (0-rescue-8405cda22c0b421db40478edcf9c1fb2) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-8405cda22c0b421db40478edcf9c1fb2-advanced-88ae7d3d-ffaf-4402-9d44-56b6845789e4' { [root@CentOS-7 ~]# [root@CentOS-7 ~]# grub2-set-default 'CentOS Linux (3.10.0-327.el7.x86_64) 7 (Core)' #修改默认启动内核 [root@CentOS-7 ~]# [root@CentOS-7 ~]# grub2-editenv list # 查看内核修改结果 saved_entry=CentOS Linux (3.10.0-327.el7.x86_64) 7 (Core) [root@CentOS-7 ~]#
#!/bin/bash #OS=`cat /etc/redhat-release | egrep -i 'centos|anolis'| wc -l` #OS_VERSION=`cat /etc/os-release | grep VERSION | awk -F '"' '{print $2}'| head -n 1|awk '{print $1}'|awk -F '.' '{print $1}'` INIT_USER='wpg' INIT_PW='dFlk*El!d03P' ntp_server="101.132.137.71" OS=`cat /etc/os-release | grep PRETTY_NAME|awk -F '"' '{print $2}'| awk '{print $1}'` OS_VERSION=`cat /etc/os-release | grep VERSION | awk -F '"' '{print $2}'| head -n 1|awk '{print $1}'|awk -F '.' '{print $1}'` OS_LIST="CentOS Anolis openEuler" #仅仅是CentOS Anolis openEuler操作系统验证过以下脚本 if [[ "$OS_LIST" =~ "$OS_TYPE" ]] ; then echo "正在安装系统工具包......" if [ "$OS" == "Anolis" ]; then #龙蜥release包 yum install anolis-release -y fi if [ "$OS" == "openEuler" ]; then #欧拉release包 yum install openEuler-release ntp hostname -y fi yum install epel-release -y >>/dev/null if [[ "CentOS Anolis" =~ "$OS" && $OS_VERSION -gt 7 ]]; then #服务器是RHEL 8 rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm yum install -y vim net-tools sysstat wntp wget zip unzip lrzsz telnet tcpdump lvm2 util-linux lsof bash-completion else #服务器是RHEL 7或者openEuler yum install -y vim gcc automake autoconf libtool make gcc gcc-c++ net-tools sysstat ntpdate wget zip unzip lrzsz telnet tcpdump lvm2 util-linux lsof bash-completion fi echo "安装系统工具包完成。" yes |cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime echo '* - nproc 65535' >>/etc/security/limits.conf echo '* - nofile 655350' >>/etc/security/limits.conf sed -i "s/4096/unlimited/g" /etc/security/limits.d/20-nproc.conf sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config setenforce 0 echo "优化系统内核中...." cat >/etc/sysctl.d/wpg.conf<<EOF # 避免放大攻击 net.ipv4.icmp_echo_ignore_broadcasts=1 # 开启恶意icmp错误消息保护 net.ipv4.icmp_ignore_bogus_error_responses=1 #开启反向路径过滤 net.ipv4.conf.all.rp_filter=1 net.ipv4.conf.default.rp_filter=1 #处理无源路由的包 net.ipv4.conf.all.accept_source_route=0 net.ipv4.conf.default.accept_source_route=0 #关闭sysrq功能 kernel.sysrq=0 #core文件名中添加pid作为扩展名 kernel.core_uses_pid=1 # 开启SYN洪水攻击保护 net.ipv4.tcp_syncookies=1 #timewait的数量,默认180000 net.ipv4.tcp_max_tw_buckets=6000 net.ipv4.tcp_sack=1 net.ipv4.tcp_window_scaling=1 #TCP Socket 读写 Buffer 设置 net.ipv4.tcp_rmem=4096 87380 4194304 net.ipv4.tcp_wmem=4096 16384 4194304 net.core.wmem_default=8388608 net.core.rmem_default=8388608 net.core.rmem_max=16777216 net.core.wmem_max=16777216 net.ipv4.tcp_max_orphans=3276800 net.ipv4.tcp_timestamps=0 #net.ipv4.tcp_synack_retries=1 #net.ipv4.tcp_syn_retries=1 net.ipv4.tcp_mem=16777216 16777216 16777216 #当keepalive 起用的时候,TCP 发送keepalive 消息的频度。缺省是2 小时 net.ipv4.tcp_keepalive_time=30 #可用知名端口范围 net.ipv4.ip_local_port_range=15000 65000 #系统全局允许分配的最大文件句柄数 fs.file-max=2097152 fs.nr_open=2097152 #并发连接 backlog 设置 net.core.somaxconn=32768 net.ipv4.tcp_max_syn_backlog =262144 net.core.netdev_max_backlog=262144 #FIN-WAIT-2 Socket 超时设置 net.ipv4.tcp_fin_timeout=15 EOF sysctl -p /etc/sysctl.d/wpg.conf echo "优化完成." echo " " echo " " echo "开始系统加固……" echo " " echo "设置用户10分钟超时自动登出" echo 'TMOUT=600' >>/etc/profile #echo "设置用户密码过期策略" #sed -i 's/PASS_MAX_DAYS\t99999/PASS_MAX_DAYS\t90/' /etc/login.defs echo "设置用户账户密码策略" sed -i 's/PASS_MIN_DAYS\t0/PASS_MIN_DAYS\t2/' /etc/login.defs sed -i 's/PASS_MIN_LEN\t5/PASS_MIN_LEN\t12/' /etc/login.defs sed -i 's/PASS_WARN_AGE\t7/PASS_WARN_AGE\t14/' /etc/login.defs sed -i 's/pam_pwquality.so/pam_pwquality.so minlen=12 dcredit=-1 ucredit=-1 lcredit=-1 ocredit=-1 /' /etc/pam.d/system-auth echo "设置用户账户锁定策略" if [ $OS_VERSION -gt 7 ]; then #服务器是RHEL 8或者Euler echo 'auth required pam_faillock.so deny=5 unlock_time=300' >> /etc/pam.d/sshd echo "auth required pam_faillock.so deny=5 unlock_time=300" >> /etc/pam.d/login else #服务器是RHEL 7 echo 'auth required pam_tally2.so onerr=fail deny=5 unlock_time=300' >> /etc/pam.d/sshd echo "auth required pam_tally2.so onerr=fail deny=5 unlock_time=300" >> /etc/pam.d/login fi echo "初始化ntp校时服务" if [[ "CentOS Anolis" =~ "$OS" && $OS_VERSION -gt 7 ]]; then #服务器是RHEL 8 软链接ntpdate路径 ln -s /usr/local/bin/ntpdate /usr/sbin/ntpdate fi crontab -l >>/tmp/cron-job.log echo "0 */10 * * * /usr/sbin/ntpdate ${ntp_server}" >>/tmp/cron-job.log crontab /tmp/cron-job.log rm -rf /tmp/cron-job.log echo "初始化job完成" echo "执行一次时间同步" /usr/sbin/ntpdate ${ntp_server} hwclock -w #init account echo "初始化登录账户${INIT_USER} ..." useradd ${INIT_USER} echo ${INIT_PW} | passwd ${INIT_USER} --stdin > /dev/null 2>&1 usermod -G wheel ${INIT_USER} if [ ! -d /home/${INIT_USER}/.ssh ]; then mkdir -p /home/${INIT_USER}/.ssh chmod 700 /home/${INIT_USER}/.ssh fi cat >>/home/${INIT_USER}/.ssh/authorized_keys <<EOF ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDC8acxBM6R7eLErvIWb0hFlrtb14Rid/o6u7IdVBSXasWvMwjRYWtQfxTelZbG2xzxhxKEugQHqTyRTfx3RtZtpqAtDT5JQ9hN2Ow5UGVtFKjoRPoVNTEFFJ5zk/Gd772OeydCBHd2BSMZ97Xm29sYcac4Xo4ZpzJGWUIB9KmKNSWmGy/3QqVJeAlg23h4ulgy2bFOa1+rU7/ze5uMAup9K6DZxayV8c7CU66gJ5Hjt7SKuOU6zWKF2mO6chEMI4nsYM9sMHpqdMGRzBWJJ8H1RMRy+P6vN0UbPW6gxD2McfoMtESe20kjTEQSLXt+9+uzkMpfUdcyiSaZ1646K0j7 wpg@suyanshuo EOF chmod 600 /home/${INIT_USER}/.ssh/authorized_keys chown -R ${INIT_USER}:${INIT_USER} /home/${INIT_USER}/.ssh echo "禁用root远程登录" sed -i 's/^#PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config sed -i 's/^PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config echo "MaxAuthTries 5" >> /etc/ssh/sshd_config sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config systemctl restart sshd echo "-----------------------------------" echo "-----------------------------------" echo "账户初始化完成 用户名:${INIT_USER} 密码: ${INIT_PW}" echo " " echo " " echo "注意: 以后请使用${INIT_USER}用户登录系统, 如需要特权请使用: 'sudo 需要的命令' 或者直接使用 'sudo su - ' 切换到root账户, 使用特权的时候按照提示输入${INIT_USER}的密码" echo " " echo " " echo "-----------------------------------" echo "-----------------------------------" history -c echo "系统加固完成" echo "--------------注意-----------------" echo "----------------------------------" echo "30秒后开始安装系统补丁 && 重启系统..." echo "按Ctrl + C 取消" echo "----------------------------------" echo "----------------------------------" sleep 30 yum update -y && reboot else echo "仅适用于$OS_LIST系统" fi
发表评论