List item
#!/usr/bin/bash
#Author:fanzhu
#Email:fz717341858@163.com
#Date:2020/05/05
#基于新虚拟机配置阿里源 本地源 和配置IP安装常用的基础命令包
stty erase ^H
ip=`ip a |grep inet |grep -v inet6|awk '{print$2}'|grep -v 127|awk -F"/" '{print$1}'`
dns=`cat /etc/resolv.conf | grep nameserver| awk '{print$2}'`
gateway=`ip r | grep default | awk '{print$3}'`
ip2=`ip r|grep 'kernel'|awk '{print $9}'`
dns2=`cat /etc/resolv.conf |grep 'nameserver'|awk '{print $2}'`
gateway2=`route -n|awk NR==3'{print $2}'`
dvd_define(){
echo "配置本地源包"
cat > /etc/yum.repos.d/dvd.repo << EOF
[dvd]
name=dvd
baseurl=file:///mnt/cdrom
gpgcheck=0
EOF
if [ ! -d /mnt/cdrom ];then
mkdir -p /mnt/cdrom
mount /dev/cdrom /mnt/cdrom
fi
echo "mount /dev/cdrom /mnt/cdrom" >> /root/.bashrc
echo "本地源配置完成"
}
file_define(){
echo "配置最大文件打开数,最大进程打开数"
echo "ulimit -SHn 102400" >> /etc/rc.local
cat >> /etc/security/limits.conf << EOF
* soft nofile 102400
* hard nofile 102400
* soft nproc 102400
* hard nproc 102400
EOF
}
change_define(){
cp /etc/sysctl.conf /etc/sysctl.conf.bak
cat > /etc/sysctl.conf << EOF
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
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.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
EOF
/sbin/sysctl -p
echo "sysctl set OK!!"
}
echo "检查网络是否正常"
ping -c1 -w1 www.baidu.com &>/dev/null
if [ $? -eq 0 ];then
echo "网络正常"
else
sed -i 's/ONBOOT="no"/ONBOOT="yes"/' /etc/sysconfig/network-scripts/ifcfg-ens33
ping -c1 -w1 www.baidu.com &>/dev/null
if [ $? -eq 0 ];then
echo "网络正常"
else
echo "网络错误,请检查网络"
exit 0
fi
fi
if [[ $UID -ne 0 ]];then
echo "使用root 执行此脚本"
exit 1
fi
echo "关闭防火墙和selinux"
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
echo "开始配置阿里源 yum源和epel源"
mv /etc/yum.repos.d/* /tmp
curl http://mirrors.aliyun.com/repo/epel-7.repo -o /etc/yum.repos.d/aliyun.repo
curl http://mirrors.aliyun.com/repo/Centos-7.repo -o /etc/yum.repos.d/centos-aliyun.repo
read -p "阿里云已经配置好了,你是否需要再创建本地的镜像源? y/n" ask
case $ask in
Y|y)
dvd_define
;;
N|n)
break
;;
*)
print "请输入字符 y or n !"
;;
esac
echo "清除旧缓存 建立新缓存"
yum clean all
yum makecache fast
echo "开始配置IP"
ifconfig &>/dev/null
if [ $? -eq 0 ];then
cat > /etc/sysconfig/network--scripts/ifcfg-ens33 << EOF
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=$ip2
GATEWAY=$gateway2
NETMASK=255.255.255.0
DNS1=$dns2
DNS2=8.8.8.8
EOF
else
cat > /etc/sysconfig/network--scripts/ifcfg-ens33 << EOF
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=$ip
GATEWAY=$gateway
NETMASK=255.255.255.0
DNS1=$dns
DNS2=8.8.8.8
EOF
fi
systemctl restart network
echo "配置成功,网卡已重启"
echo "安装常用的工具"
yum install vim lrzsz sysstat elinks wget net-tools bash-completion psmisc
echo "接下来的为加固服务器操作,请考虑清楚后选择"
read -p "修改内核参数,所有用户最大文件打开数? y/n " ask1
case $ask1 in
Y|y)
file_define
;;
N|n)
break
;;
*)
print "请输入字符 y or n !"
;;
esac
read -p "设置常见的内核参数,加固服务器 ? y/n" ask2
case $ask2 in
Y|y)
change_define
;;
N|n)
break
;;
*)
print "请输入字符 y or n !"
;;
esac
echo "所有配置完毕,开始使用吧"