自己写的算力环境基础配置一键脚本,方便自己使用,需要提前配置好用户的sudo免密权限,如果是root用户就直接执行
#!/bin/bash #set -e CURRENT_PATH=`readlink -f $(dirname $0)` HOST_LIST=('10.7.0.12 blog')
function INFO(){ /bin/echo -e "\e[104m\e[97m[I]\e[49m\e[39m ${*}" } function WARNING(){ /bin/echo >&2 -e "\e[101m\e[97m[W]\e[49m\e[39m ${*}" } function ERROR(){ /bin/echo >&2 -e "\e[101m\e[97m[E]\e[49m\e[39m ${*}" } function INSTALL_AUTO_RESTART(){ INFO "设置安装软件无交互重启..." if [ ! -f /etc/needrestart/needrestart.conf_bak ]; then sudo cp /etc/needrestart/needrestart.conf /etc/needrestart/needrestart.conf_bak fi LINE=`cat /etc/needrestart/needrestart.conf | grep -n '^#\$nrconf{restart}' | awk -F ':' '{print $1}'` if [ ! -z "${LINE+x}" ]; then R=`cat /etc/needrestart/needrestart.conf | grep -n '^\$nrconf{restart}' | wc -l` if [ $R -eq 0 ]; then sudo sed -i "${LINE}a \$nrconf{restart} = 'a';" /etc/needrestart/needrestart.conf fi fi } function HOSTS_BIND_IP(){ INFO "设置主机名地址绑定..." for HOST in "${HOST_LIST[@]}" do cat /etc/hosts | grep "${HOST}" if [ $? -ne 0 ]; then echo "${HOST}" | sudo tee -a /etc/hosts fi done } function GENERATE_SSH_ID(){ INFO "生成密钥..." if [ ! -f ~/.ssh/id_rsa ]; then ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa chmod 700 ~/.ssh touch ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_keys else WARNING "密钥文件已存在,跳过!" fi } function SET_CPU_PERFORMANCE(){ INFO "设置CPU Performance性能模式..." echo 'GOVERNOR="performance"' | sudo tee /etc/default/cpufrequtils sudo systemctl restart cpufrequtils } function SET_SECURITY_LIMITS(){ INFO "优化文件描述符..." if [ ! -f /etc/security/limits.conf_bak ]; then sudo cp /etc/security/limits.conf /etc/security/limits.conf_bak fi sudo tee /etc/security/limits.conf > /dev/null <<EOF * soft nofile 1000000 * hard nofile 1000000 * soft nproc 2000000 * hard nproc 2000000 * soft memlock unlimited * hard memlock unlimited * soft stack unlimited * hard stack unlimited root soft nofile 1000000 root hard nofile 1000000 root soft nproc 2000000 root hard nproc 2000000 root soft memlock unlimited root hard memlock unlimited root soft stack unlimited root hard stack unlimited EOF } function DISABLE_AUTO_UPDATE(){ INFO "禁用内核自动更新..." sudo rm -f /etc/apt/apt.conf.d/50unattended-upgrades >/dev/null 2>&1 sudo systemctl stop unattended-upgrades.service sudo systemctl disable unattended-upgrades.service for i in `dpkg --list | grep -E 'linux-(headers|image|modules)-[0-9]' | awk '{print $2}'` do sudo apt-mark hold $i done } function SET_TIME_ZONE(){ ZONE="Asia/Shanghai" INFO "设置时区为 ${ZONE}" sudo timedatectl set-timezone ${ZONE} sudo ntpdate pool.ntp.org >/dev/null 2>&1 } function DISABLE_IPV6(){ INFO "设置禁用IPV6 ..." R=`sudo sysctl -a | grep net.ipv6.conf.all.disable_ipv6 | awk -F '=' '{print $2}'` if [ $R -eq 1 ]; then INFO "已配置禁用ipv6,跳过!" else echo 'net.ipv6.conf.all.disable_ipv6 = 1' | sudo tee -a /etc/sysctl.conf sudo sysctl -p fi } function DISABLE_SLEEP(){ INFO "禁用系统休眠..." sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target } function DISABLE_NOUVEAU(){ INFO "禁用系统nouveau驱动..." R=`lsmod | grep nouveau | wc -l` if [ $R -eq 0 ]; then INFO "nouveau 驱动已禁用,跳过!" else CHECK_ITEM=('blacklist\s+nouveau' 'blacklist\s+lbm‐nouveau' 'options\s+nouveau\s+modeset=0' 'alias\s+nouveau\s+off' 'alias\s+lbm‐nouveau\s+off') for ITEM in "${CHECK_ITEM[@]}" do cat /etc/modprobe.d/blacklist.conf | grep -E ${ITEM} if [ $? -ne 0 ]; then echo ${ITEM//\\s+/ } | sudo tee -a /etc/modprobe.d/blacklist.conf fi done sudo update-initramfs -k $(uname -r) -c fi } function PUBLIC_KEY_LOGIN(){ INFO "设置密钥登录和优化配置..." CHECK_ITEM=('^ClientAliveInterval\s+60' '^ClientAliveCountMax\s+3' '^MaxStartups\s+512' '^AuthorizedKeysFile\s+.ssh/authorized_keys') for ITEM in "${CHECK_ITEM[@]}" do cat /etc/ssh/sshd_config | grep -E ${ITEM} if [ $? -ne 0 ]; then ITEM2=${ITEM//\\s+/ } echo ${ITEM2:1} | sudo tee -a /etc/ssh/sshd_config fi done sudo systemctl restart sshd } sudo apt update -y INSTALL_AUTO_RESTART sudo apt install cpufrequtils build-essential g++ gcc make dkms net-tools ntpdate git curl wget msr-tools sshpass unzip -y HOSTS_BIND_IP GENERATE_SSH_ID SET_CPU_PERFORMANCE SET_SECURITY_LIMITS DISABLE_AUTO_UPDATE SET_TIME_ZONE DISABLE_IPV6 DISABLE_SLEEP DISABLE_NOUVEAU PUBLIC_KEY_LOGIN
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://sulao.cn/post/1138
评论列表