个人使用,当做记录。主要是每次安装完都要重复这些步骤感觉太麻烦,索性写了个小脚本,看着脚本跑,岂不美哉?
主要实现功能:
- 关闭防火墙,selinux
- 配置阿里源[base,epel,docker-ce(可选)],安装基础软件(vim,gcc,make,unzip,wget等等)
- 配置时钟同步并添加计划任务
- docker安装及docker-compose安装(可选)。
需要输入的地方:
- 输入当前用户名,用来设置计划任务
- 输入【yes/no】选择是否安装docker
最后执行完需要重启服务器,使selinux生效 。
cat >> ./basic-linux.sh <<EOF
#!/bin/bash
#关闭linux防火墙,selinux
echo "关闭linux防火墙,selinux"
sudo systemctl stop firewalld && systemctl disable firewalld #关闭防火墙,开机不自启
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config #替换selinux配置
#配置yum源,安装常用软件包
echo "配置yum源,安装常用软件包"
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo #拉取阿里源
sudo curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo #拉取阿里源
sudo yum clean all #清楚缓存
sudo yum makecache #生成元数据
echo "更新软件包..."
sudo yum update -y > /dev/null #更新
echo "正在安装常用软件包......"
sudo yum install -y yum-utils device-mapper-persistent-data lvm2 wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack ntpdate telnet ipvsadm > /dev/null
#时钟同步
sudo ntpdate -u ntp.aliyun.com #同步时间到阿里云
read -p "please input you username:" name
echo "0 */3 * * * /usr/sbin/ntpdate -u ntp.aliyun.com" >> /var/spool/cron/\$name #配置计划任务
sudo systemctl restart crond
#配置docker&docker-compose
read -p "Please enter whether to install docker?[yes/no]" result
if [ \$result == yes ]
then
echo "配置docker&docker-compose"
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #配置阿里docker源
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
sudo yum makecache fast > /dev/null
sudo yum -y install docker-ce > /dev/null
sudo systemctl start docker && systemctl enable docker
echo "下载docker-compose"
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose > /dev/null#配置docker-compose
sudo chmod 777 /usr/local/bin/docker-compose
echo " {
\"registry-mirrors\": [\"https://t4u8joul.mirror.aliyuncs.com\"]
}" >> /etc/docker/daemon.json
systemctl daemon-reload && systemctl restart docker > /dev/null
echo "基本初始化完毕,请重启服务器。"
else
echo "基本初始化完毕,请重启服务器。"
fi
EOF
chmod +x ./basic-linux.sh
./basic-linux.sh