#!/bin/bash
run_command() {
"$@"
local status=$?
if [ $status -eq 0 ]; then
echo "命令 \"$@\" 执行成功。"
else
echo "命令 \"$@\" 失败,退出状态码为 $status。"
exit $status
fi
}
if [ -f /tmp/reboot_marker ]; then
echo "服务器已重启过..."
if [ -d ~/.ssh ]; then
echo "存在.ssh文件夹,正在删除..."
run_command rm -r ~/.ssh
echo ".ssh文件夹已删除。"
fi
echo "正在执行ssh localhost命令..."
run_command ssh localhost <<EOF
yes
123456
123456
EOF
run_command cd ./.ssh
run_command ssh-keygen -t rsa
run_command ssh-copy-id cdh01 <<EOF
yes
123456
123456
EOF
host_name=$(hostname)
ip_address=$(ip addr show eth0 | awk '/inet / {print $2}' | cut -d/ -f1)
specified_host="cdh01"
specified_ip="172.21.32.25"
if [ "$host_name" == "$specified_host" ] && [ "$ip_address" == "$specified_ip" ]; then
echo "该节点为主节点"
else
echo "该节点为从节点"
fi
else
echo "服务器未重启过..."
new_hostname="cdh01"
echo "设置主机名为 $new_hostname"
run_command sudo hostnamectl set-hostname $new_hostname
run_command echo "172.21.32.25 cdh01" >> /etc/hosts
run_command echo "172.21.32.26 cdh02" >> /etc/hosts
run_command echo "172.21.32.27 cdh03" >> /etc/hosts
selinux_status=$(sestatus | awk '/SELinux status:/ {print $3}')
if [ "$selinux_status" == "enabled" ]; then
echo "正在关闭SELinux..."
run_command setenforce 0
run_command sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
echo "已禁用SELinux。"
else
echo "SELinux已处于关闭状态,无需执行任何操作。"
fi
echo "正在关闭防火墙..."
run_command systemctl stop firewalld
run_command systemctl disable firewalld
echo "防火墙已关闭并禁止开机自启动。"
run_command echo never > /sys/kernel/mm/transparent_hugepage/defrag
run_command echo never > /sys/kernel/mm/transparent_hugepage/enabled
run_command echo 'echo never > /sys/kernel/mm/transparent_hugepage/defrag' >> /etc/rc.local
run_command echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
run_command echo 'vm.swappiness=0' >> /etc/sysctl.conf
run_command echo "* soft nofile 655350" >> /etc/security/limits.conf
run_command echo "* hard nofile 655350" >> /etc/security/limits.conf
run_command echo "* soft core unlimited" >> /etc/security/limits.conf
run_command echo "* hard core unlimited" >> /etc/security/limits.conf
read -p "更改将在重新启动后生效。现在重启吗?(y/n):" choice
if [[ $choice == "y" || $choice == "Y" ]]; then
run_command touch /tmp/reboot_marker
run_command reboot
else
echo "请记住在更改生效前重新启动您的系统。"
fi
fi