linux初始化脚本

#!/bin/bash
stty erase ^h

echo "------设置时区并同步时间------"
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &> /dev/null
if ! crontab -l |grep ntpdate &>/dev/null ; then
(echo "* 1 * * * ntpdate time.aliyun.com >/dev/null 2>&1";crontab -l) |crontab
fi

echo "------禁用selinux------"
sed -i '/SELINUX/{s/enforcing/disabled/}' /etc/selinux/config

echo "------安装命令补全包------"
yum install -y epel-release &>/dev/null 2>&1
yum install -y bash-completion &>/dev/null 2>&1

echo ================================================
echo = 1.清空firewall防火墙原来所有的规则,只保留ssh                     =
echo = 2.关闭防火墙                                                                           =
echo ================================================
read -p "选择清空防火墙(1)或者关闭防火墙(2)" choice
case $choice in
1)
    cd /etc/firewalld/zones/
    echo "" > public.xml
cat >> public.xml << EOF
<zone>
<short>Public</short>
<service name="ssh"/>
</zone>
EOF
      firewall-cmd --reload >/dev/null 2>&1
;;
2)
      iptables -F
      systemctl disable firewalld &> /dev/null
             if [ $? -eq 0 ];then
                  echo "关闭成功"
             fi
;;
*)
             echo "输入错误,请重新输入"
;;
esac

echo "------设置最大打开文件数------"
if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then
cat >> /etc/security/limits.conf << EOF

* soft nofile 65535
* hard nofile 65535
EOF
fi

echo "------设置最大进程数------"
if ! grep "* hard nproc 1204000" /etc/security/limits.conf &>/dev/null; then
cat >> /etc/security/limits.conf << EOF

* hard nproc 1204000
* soft nproc 1204000
EOF
fi

echo "------高并发内核优化------"
cat >> /etc/sysctl.conf << EOF

net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
EOF

 


echo "------禁止SWAP使用------"
sed -ri 's/.*swap.*/#&/' /etc/fstab

echo "------选择安装常用软件------"
read -p "是否安装常用工具(y|n):" num
case $num in
y|Y|yes|YES|Yes)
            yum install -y wget vim net-tools lrzsz &>/dev/null 2>&1
            echo "安装完成"
;;
N|n|NO|No|no|nO)
            break
;;
*)
            echo "请输入正确选项"
exit
;;
esac

echo "------选择下载阿里云yum源------"
ls -l /etc/yum.repo/Centos-7.repo &>/dev/null 2>&1
if [ $? -eq 0 ]; then
          echo "您已经下载了阿里云yum源"
else
          echo "检测到您还没下载阿里云yum源"
          read -p "是否安装阿里云yum源(y|n):" yum
          case $yum in
y|Y|yes|YES|Yes)
               aa="/etc/yum.repo/"
               rm -rf $aa*.repo
               wget -P $aa http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null 2>&1
;;

N|n|NO|No|no|nO)
               break
;;

*)
              echo "请输入正确选项"
        esac
fi

echo "------清除yum源缓存------"
yum makecache fast &> /dev/null
if [ $? -eq 0 ];then
echo "刷新成功"
fi

#重启之后电脑配置生效
echo "------即将重启电脑------"

read -p "是否重启电脑(y|n):" reboot
case $reboot in
y|Y|yes|YES|Yes)
          reboot
;;

N|n|NO|No|no|nO)
          echo "稍后手动重启"
;;

*)
          echo "请输入正确选项"
exit
;;
esac

 

 

 

添加防火墙端口脚本

#!/bin/bash
echo "------允许iptable的端口------"
ipableport(){
       port=$1
       protocol=$2
       iptables -I INPUT -p $port --dport $protocol -j ACCEPT
       service iptables save
}
ipableport(){
}

 

 

参考:

#配置yum 源并优化
mv /etc/yum.repos.d/CentOS-Base.repo{,.bak}
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum makecahe
var_yum_list=$(yum repolist |tail -n5)

 

#修改PS1
echo 'export PS1="[\u@\[\e[1;31m\]\h \[\e[1;36m\]\W\[\e[0m\]]\\$ "' >> ~/.bashrc
source ~/.bashrc

 

posted @ 2022-04-06 09:18  虞岩  阅读(133)  评论(0编辑  收藏  举报