Centos7 新建server的优化脚本--不适用于所有人
生活随笔
收集整理的這篇文章主要介紹了
Centos7 新建server的优化脚本--不适用于所有人
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#!/bin/bash
#optimization script for Centos7
echo " "
echo "|-------------------------------------------------|"
echo "| *****linux系統優化***** |"
echo "|-------------------------------------------------|"
echo "| 1.配置內網yum源 |"
echo "| 2.服務啟動項優化 |"
echo "| 3.SNMP服務配置 |"
echo "| 4.修改網卡為eth0 |"
echo "| 5.單ip配置 |"
echo "| 6.雙網卡配置 |"
echo "| 7.登錄賬戶禁用(默認賬戶) |"
echo "| 8.添加新用戶 |"
echo "| 9.更改主機名 |"
echo "| 10.刪除virbr0虛擬網卡 |"
echo "| 0.退出 |"
echo "|-------------------------------------------------|"
echo -n "請輸入您的選擇:"
read thechoice
if [ "$thechoice" == "1" ];thenecho "您的選擇是 1.配置內網yum源"echo change yum.conf pleaese waiting..cd /etc/yum.repos.dtest -e baks/ && mv -i -n *.repo bakstest ! -e baks/ && mkdir baks && mv -f *.repo baks/echo [local] > local.repoecho name=local >> local.repoecho baseurl=http://10.244.134.49/centos7/ >> local.repoecho enabled=1 >> local.repoecho gpgcheck=0 >> local.repoyum clean allyum makecacheecho " "echo ok,查看local.repo配置 echo cat local.repoecho " "cat local.repoecho -n "按任意鍵繼續,按n鍵退出腳本..."read nyif [ "$ny" == "N" ] || [ "$ny" == "n" ];thenecho ok,quit shell scriptelse/root/Desktop/linuxshellsForCentos7/optimization.shfielif [ "$thechoice" == "2" ];thenecho "您的選擇是 2.服務啟動項優化"Service=(NetworkManager.servicefirewalld.servicebluetooth.service)for i in ${Service[*]}dosystemctl disable $iecho "--------------------show $i status--------------------"echo "systemctl disable $i"systemctl stop $isystemctl status $i | grep Active -B 2echo "systemctl is-enabled $i"systemctl is-enabled $iecho " "doneecho close selinux..cp -i -n /etc/selinux/config /etc/selinux/config.bakcp -f /etc/selinux/config.bak /etc/selinux/configsed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/configsetenforce 0echo show selinux status :getenforceecho " "echo '*/5 * * * * /usr/sbin/ntpdate 10.191.130.130' > /var/spool/cron/rootecho "crontab -l"crontab -lsystemctl restart crond.servicesystemctl enable crond.serviceecho "show crond.service status"systemctl status crond.service | grep Active -B 2echo "systemctl in-enable crond.service"systemctl is-enabled crond.serviceecho " "echo -n "按任意鍵繼續,按n鍵退出腳本..."read nyif [ "$ny" == "N" ] || [ "$ny" == "n" ];thenecho ok,quit shell scriptelse/root/Desktop/linuxshellsForCentos7/optimization.shfielif [ "$thechoice" == "3" ];thenecho "您的選擇是 3.SNMP服務配置"if [ ! -f "/etc/snmp/snmpd.conf" ];thenecho "warning :snmpd.conf not exist,pleaese install snmpd"echo "Input y to install snmp,input n or other cancel instal snmp and exit."read thenyif [ "$theny" == "Y" ] || [ "$theny" == "y" ];thenecho ok install snmp.please waiting...cd /etc/yum.repos.dtest -e baks/ && mv -i -n *.repo bakstest ! -e baks/ && mkdir baks && mv -f *.repo baks/echo [local] > local.repoecho name=local >> local.repoecho baseurl=http://10.244.134.49/centos7/ >> local.repoecho enabled=1 >> local.repoecho gpgcheck=0 >> local.repoyum clean allyum makecacheyum install -y net-snmp net-snmp-utilselseecho ok,quit shell scriptexit 0fielseecho please waiting...echo " "fi#test ! -e /etc/snmp/snmpd.conf && echo "warning :snmpd.conf not exist,pleaese install snmpd" && echo "you can try to yum install -y net-snmp net-snmp-utils" && exit 0cd /etc/snmpcp -i -n snmpd.conf snmpd.conf.bakcp -f snmpd.conf.bak snmpd.confsed -i '56a view all included .1' snmpd.confsed -i 's/access notConfigGroup "" any noauth exact systemview none none/access notConfigGroup "" any noauth exact all none none/g' snmpd.confsed -i 's/#view all included .1 80/view all included .1 80/g' snmpd.confecho ----------------------ok....--------------------------echo ---------------------show file------------------------cat -n snmpd.conf |grep -B2 -A2 "view all included .1"echo ------------------------------------------------------cat -n snmpd.conf |grep -B2 -A2 "any noauth exact all none none"echo ------------------------------------------------------cat -n snmpd.conf |grep -B2 -A2 "view all included .1 80"echo "--------------------show snmpd status--------------------"systemctl restart snmpd.servicesystemctl enable snmpd.serviceecho " "systemctl status snmpd.service | grep Active -B 3echo " "echo "Show the service self-start status"systemctl list-unit-files | grep snmpdecho " "echo -n "按任意鍵繼續,按n鍵退出腳本..."read nyif [ "$ny" == "N" ] || [ "$ny" == "n" ];thenecho ok,quit shell scriptelse/root/Desktop/linuxshellsForCentos7/optimization.shfielif [ "$thechoice" == "4" ];thenecho "您的選擇是 4.修改網卡為eth0"cd /etc/defaultcp -i -n grub grub.baksed -i 's/GRUB_CMDLINE_LINUX/#GRUB_CMDLINE_LINUX/g' grubecho 'GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root net.ifnames=0 biosdevname=0 rd.lvm.lv=centos/swap rhgb quiet"' >> grubgrub2-mkconfig -o /boot/grub2/grub.cfgcd /etc/sysconfig/network-scripts/wangkaname=$(ls|grep ifcfg-en)mv -i -n "$wangkaname" "$wangkaname.bak"#ipdizhi=$(cat $wangkaname | grep IPADDR)#ziwangyanma=$(cat $wangkaname | grep NETMASK)#morenwangguan=$(cat $wangkaname | grep GATEWAY)#cp -i -n ifcfg-eth0 ifcfg-eth0.bakecho 'DEVICE=eth0' > /etc/sysconfig/network-scripts/ifcfg-eth0echo 'TYPE=Ethernet' >> /etc/sysconfig/network-scripts/ifcfg-eth0echo 'ONBOOT=yes' >> /etc/sysconfig/network-scripts/ifcfg-eth0echo 'NM_CONTROLLED=no' >> /etc/sysconfig/network-scripts/ifcfg-eth0echo 'BOOTPROTO=static' >> /etc/sysconfig/network-scripts/ifcfg-eth0(cat $wangkaname.bak | grep IPADDR) >> /etc/sysconfig/network-scripts/ifcfg-eth0(cat $wangkaname.bak | grep NETMASK) >> /etc/sysconfig/network-scripts/ifcfg-eth0(cat $wangkaname.bak | grep GATEWAY) >> /etc/sysconfig/network-scripts/ifcfg-eth0#echo "You must restart to apply these modifications"echo -e "\033[31;40m You must restart to apply the modifications \033[0m"echo " "echo "Press Y to restart the server and press any other key to exit the script"read nyif [ "$ny" == "Y" ] || [ "$ny" == "y" ];thenecho ok,pleaese wait...rebootelseexit 0fiecho " "echo -n "按任意鍵繼續,按n鍵退出腳本..."read nyif [ "$ny" == "N" ] || [ "$ny" == "n" ];thenecho ok,quit shell scriptelse/root/Desktop/linuxshellsForCentos7/optimization.shfielif [ "$thechoice" == "5" ];thenecho "您的選擇是 5.單ip配置"#xianshi yijing changshang wangxian de wangkouecho ----------------xianshi changshang wangxian de wangkouip a|grep "state UP"|awk -F '[: ]+' '{print $2}'echo pleaese choose which port you want to change, eth0-11,input the numberread portnumbercd /etc/sysconfig/network-scriptsif [ "$portnumber" == "0" ]||[ "$portnumber" == "1" ]||[ "$portnumber" == "2" ]||[ "$portnumber" == "3" ]||[ "$portnumber" == "4" ]||[ "$portnumber" == "5" ]||[ "$portnumber" == "6" ]||[ "$portnumber" == "7" ]||[ "$portnumber" == "8" ]||[ "$portnumber" == "9" ]||[ "$portnumber" == "10" ]||[ "$portnumber" == "11" ];thencp -i -n ifcfg-eth$portnumber ifcfg-eth$portnumber.baksed -i 's/ONBOOT=no/ONBOOT=yes/g' ifcfg-eth$portnumbersed -i 's/BOOTPROTO=dhcp/BOOTPROTO=static/g' ifcfg-eth$portnumberecho pleaese write theIPADDRread theipecho pleaese write theNETMASKread thenetmaskecho pleaese write theGATEWAYread thegatewayecho okecho TYPE=Ethernet > /etc/sysconfig/network-scripts/ifcfg-eth$portnumberecho BOOTPROTO=static >> /etc/sysconfig/network-scripts/ifcfg-eth$portnumberecho NAME=eth$portnumber >> /etc/sysconfig/network-scripts/ifcfg-eth$portnumberecho DEVICE=eth0 >> /etc/sysconfig/network-scripts/ifcfg-eth$portnumberecho ONBOOT=yes >> /etc/sysconfig/network-scripts/ifcfg-eth$portnumberecho 'IPADDR='$theip >> /etc/sysconfig/network-scripts/ifcfg-eth$portnumberecho 'NETMASK='$thenetmask >> /etc/sysconfig/network-scripts/ifcfg-eth$portnumberecho 'GATEWAY='$thegateway >> /etc/sysconfig/network-scripts/ifcfg-eth$portnumberecho ------------------------show eth$portnumber-------------------------cat ifcfg-eth$portnumber#yong dangqian shijian zuo beifendatename=$(date +%Y%m%d-%H%M%S)cp ifcfg-eth$portnumber ifcfg-eth$portnumber.$datename.bakelseecho "----------------warning: pleaese input eth(0-11)-----------------"exit 0fiecho ""#panduan shifou xuyao baocun wangguan peizhiecho would you want to restart the network y/nread thenyif [ "$theny" == "Y" ] || [ "$theny" == "y" ];thenecho okservice network restartelif [ "$theny" == "N" ] || [ "$theny" == "n" ];thenecho ok,not restartfiecho " "echo -n "按任意鍵繼續,按n鍵退出腳本..."read nyif [ "$ny" == "N" ] || [ "$ny" == "n" ];thenecho ok,quit shell scriptelse/root/Desktop/linuxshellsForCentos7/optimization.shfielif [ "$thechoice" == "6" ];thenecho "您的選擇是 6.雙網卡配置"#change office ip (eth3)echo change eth0 office ipcd /etc/sysconfig/network-scripts/cp -i -n ifcfg-eth0 ifcfg-eth0.bakecho pleaese write officeIPADDRread officeipecho pleaese write officeNETMASKread officenetmaskecho pleaese write officeGATEWAYread officegatewayecho 'DEVICE=eth0' > /etc/sysconfig/network-scripts/ifcfg-eth0echo 'TYPE=Ethernet' >> /etc/sysconfig/network-scripts/ifcfg-eth0echo 'ONBOOT=yes' >> /etc/sysconfig/network-scripts/ifcfg-eth0echo 'NM_CONTROLLED=no' >> /etc/sysconfig/network-scripts/ifcfg-eth0echo 'BOOTPROTO=static' >> /etc/sysconfig/network-scripts/ifcfg-eth0echo 'IPADDR='$officeip >> /etc/sysconfig/network-scripts/ifcfg-eth0echo 'NETMASK='$officenetmask >> /etc/sysconfig/network-scripts/ifcfg-eth0echo 'DNS1=10.191.130.130' >> /etc/sysconfig/network-scripts/ifcfg-eth0echo 'DNS2=10.191.131.131' >> /etc/sysconfig/network-scripts/ifcfg-eth0#echo 'GATEWAY='$officegateway >> /etc/sysconfig/network-scripts/ifcfg-eth3echo eth0 ok echo change eth1 sfc ipecho pleaese write sfcIPADDRread sfcipecho pleaese write sfcNETMASKread sfcnetmaskecho pleaese write sfcGATEWAYread sfcgatewayecho 'DEVICE=eth1' > /etc/sysconfig/network-scripts/ifcfg-eth1echo 'TYPE=Ethernet' >> /etc/sysconfig/network-scripts/ifcfg-eth1echo 'ONBOOT=yes' >> /etc/sysconfig/network-scripts/ifcfg-eth1echo 'NM_CONTROLLED=no' >> /etc/sysconfig/network-scripts/ifcfg-eth1echo 'BOOTPROTO=none' >> /etc/sysconfig/network-scripts/ifcfg-eth1echo 'IPADDR='$sfcip >> /etc/sysconfig/network-scripts/ifcfg-eth1echo 'NETMASK='$sfcnetmask >> /etc/sysconfig/network-scripts/ifcfg-eth1echo 'GATEWAY='$sfcgateway >> /etc/sysconfig/network-scripts/ifcfg-eth1echo eth1 okcd /etc/rc.dcp -i -n rc.local rc.local.bakcp -f rc.local.bak rc.localecho 'route add -net 10.0.0.0 netmask 255.0.0.0 gw '$officegateway >> /etc/rc.d/rc.localecho tianjia jingtai luyou wanchengecho 'any net 10.0.0.0/8 gw '$officegateway > /etc/sysconfig/static-routesecho tianjia moren luyou wancheng #panduan shifou xuyao baocun wangguan peizhiecho would you want to restart the network y/nread thenyif [ "$theny" == "Y" ] || [ "$theny" == "y" ];thenecho okservice network restartelif [ "$theny" == "N" ] || [ "$theny" == "n" ];thenecho ok,not restartfiecho " "echo -n "按任意鍵繼續,按n鍵退出腳本..."read nyif [ "$ny" == "N" ] || [ "$ny" == "n" ];thenecho ok,quit shell scriptelse/root/Desktop/linuxshellsForCentos7/optimization.shfielif [ "$thechoice" == "7" ];thenecho "您的選擇是 7.登錄賬戶禁用(默認賬戶)"echo " "cat /etc/passwd |grep /bin/bashusermod -s /sbin/nologin mysqlusermod -s /sbin/nologin postgresusermod -s /sbin/nologin amandausermod -s /sbin/nologin pvmecho " "echo "--------------------------- "echo "xiugaihou de zhanghu"echo " "cat /etc/passwd |grep /bin/bashecho " "echo " "echo -n "按任意鍵繼續,按n鍵退出腳本..."read nyif [ "$ny" == "N" ] || [ "$ny" == "n" ];thenecho ok,quit shell scriptelse/root/Desktop/linuxshellsForCentos7/optimization.shfielif [ "$thechoice" == "8" ];thenecho "您的選擇是 8.添加新用戶"echo " "echo Please input the user nameread theuseruseradd $theuserif [ "$theuser" == "cdsm" ] ;thenecho $theuser:"passwd2020@"|chpasswdelseecho $theuser:"passwd123.1234"|chpasswdficp -i -n /etc/sudoers /etc/sudoers.bakecho "$theuser ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoersecho " "echo ------Has been created!------echo " "echo user name: $theuserif [ "$theuser" == "cdsm" ] ;thenecho password : passwd2020@elseecho password : passwd123.1234fi echo " "echo -------ok~! Thanks-----------echo " "echo " "echo -n "按任意鍵繼續,按n鍵退出腳本..."read nyif [ "$ny" == "N" ] || [ "$ny" == "n" ];thenecho ok,quit shell scriptelse/root/Desktop/linuxshellsForCentos7/optimization.shfielif [ "$thechoice" == "9" ];thenecho "您的選擇是 9.更改主機名"echo ----------------pleaese input hostname---------------read thehostnamehostname $thehostnameecho $thehostname > /etc/hostnameecho ------------The hostname has been changed------------echo " "echo -n "按任意鍵繼續,按n鍵退出腳本..."read nyif [ "$ny" == "N" ] || [ "$ny" == "n" ];thenecho ok,quit shell scriptelse/root/Desktop/linuxshellsForCentos7/optimization.shfielif [ "$thechoice" == "10" ];thenecho "您的選擇是 10.刪除virbr0虛擬網卡"echo "show virbr0 status"virsh net-listvirsh net-destroy defaultvirsh net-undefine defaultsystemctl restart libvirtd.serviceecho "delete virbr0 ok..."echo " "echo "show virbr0 status now"virsh net-listecho " "echo -n "按任意鍵繼續,按n鍵退出腳本..."read nyif [ "$ny" == "N" ] || [ "$ny" == "n" ];thenecho ok,quit shell scriptelse/root/Desktop/linuxshellsForRedhat7/optimization.shfielif [ "$thechoice" == "0" ];thenecho ok,quit shell script exit 0else echo " "echo -e "\033[31;40m -----------輸入有誤,請重新輸入------------------- \033[0m"echo " "/root/Desktop/linuxshellsForCentos7/optimization.shfi
?
總結
以上是生活随笔為你收集整理的Centos7 新建server的优化脚本--不适用于所有人的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Android】【UI】ACTION_
- 下一篇: Centos(6、7) —— yum源的