#!/bin/bash# Optimize the system after installationPASSWD=reidNETIP=192.168.56.11PROTOBOOT=staticHOSTNAME=linux-node1.example.comDNS1=192.168.56.2NTPSERVER=ntp1.aliyun.comYUMREPO=http://mirrors.aliyun.com/repo/Centos-7.repoEPELREPO=http://mirrors.aliyun.com/repo/epel-7.repoSSH_PORT=22122# in case of some bad behavioursCHATTR=chenhao# Open the port for iptabeles input or maybe stop iptablesPORTS=80,22,21,8088# record the system user,ip addresse,shell command and detailHISTDIR=/usr/etc/.history# the welcome infocat << EOF+------------------------------------------------------------------+| ********** Welcome to CentOS 7.x System init ********** |+------------------------------------------------------------------+EOF[ `whoami` != "root" ] && echo "please use root" && exit 1function format() { echo -e "\033[32m Success!!!\033[0m\n" echo "#########################################################"}###change the root passwdecho "set root passwd"echo $PASSWD | passwd root --stdin &> /dev/nullformat###change network settingecho ""> /etc/sysconfig/network-scripts/ifcfg-eth0echo "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo "NAME=eth0" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo "DEVICE=eth0" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo "BOOTPROTO=$PROTOBOOT" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo "IPADDR=$NETIP" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo "NETMASK=255.255.255.0" >> /etc/sysconfig/network-scripts/ifcfg-eth0#systemctl restart networkformat#stop NetworkManagersystemctl stop NetworkManagersystemctl diable NetworkManagerformat###add routeroute add default gateway $DNS1echo "route add default gateway $DNS1" >/etc/profile.d/add-route.shformat###change the hostnameecho "set hostname"hostname $HOSTNAME && echo "$HOSTNAME" > /etc/hostnameformat###change the dnsecho "set DNS"echo "" > /etc/resolv.conf echo "nameserver $DNS1" > /etc/resolv.conf#echo "nameserver $DNS2" >> /etc/resolv.confping -c 3 www.baidu.com &> /dev/null || echo "Network is unreachable" || exit 3format###diable selinuxecho "disable selinux"[ `getenforce` != "Disabled" ] && setenforce 0 &> /dev/null && sed -i s/"^SELINUX=.*$"/"SELINUX=disabled"/g /etc/sysconfig/selinuxformat###update yum repoecho "set yum mirrors"cd /etc/yum.repos.d/mv CentOS-Base.repo CentOS-Base.repo.bakcurl -o /etc/yum.repos.d/CentOS-Base.repo $YUMREPO &> /dev/nullcurl -o /etc/yum.repos.d/epel.repo $EPELREPO &> /dev/nullmv /var/cache/yum /tmpyum clean all &> /dev/null && yum makecache &> /dev/nullformat###install the basic commandyum install vim wget openssl-devel ntpdate make gcc-c++ ncurses-devel net-snmp sysstat lrzsz zip unzip tree net-tools lftp -y#yum -y groupinstall "Development Tools" "Server Platform Development" &> /dev/nullformat### change ssh port#echo "set sshd"#cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak#sed -i s/"^Port 22"/"Port $SSH_PORT"/g /etc/ssh/sshd_config#sed -i s/"^UseDNS yes"/"UseDNS no"/g /etc/ssh/sshd_config#service sshd restart &> /dev/null###lock the important file($CHATTR -i to disable)#echo "chattr files"#chattr +i /etc/passwd#chattr +i /etc/inittab#chattr +i /etc/group#chattr +i /etc/shadow#chattr +i /etc/gshadow#chattr +i /etc/resolv.conf#chattr +i /etc/hosts#chattr +i /etc/fstab#mv /usr/bin/chattr /usr/bin/$CHATTR###character setecho "set LANG"#sed -i s/"^LANG=.*$"/"LANG=zh_CN.UTF-8"/ /etc/locale.conf#source /etc/locale.conf###update timezoneecho "set ntptime"<br>mv /etc/localtime /tmpcp /usr/share/zoneinfo/Asia/Shanghai /etc/localtimentpdate $NTPSERVER &> /dev/nullecho "*/5 * * * * /usr/sbin/ntpdate $NTPSERVER &>/dev/null" >> /etc/crontabhwclock -wformat###set the system limitecho "Set ulimit 65535"cat << EOF > /etc/security/limits.conf* soft nofile 65535* hard nofile 65535* soft nproc 65535* hard nproc 65535EOFsed -i ‘s/65535/1024000/g‘ /etc/security/limits.d/90-nproc.confformat###system audit and trouleshootingecho "set history"cat >> /etc/profile.d/system-audit.sh << EOFUSER_IP=`who -u am i 2>/dev/null| awk ‘{print $NF}‘|sed -e ‘s/[()]//g‘`if [ -z $USER_IP ]thenUSER_IP=`hostname`fiif [ ! -d $HISTDIR ]thenmkdir -p $HISTDIRchmod 777 $HISTDIRfiif [ ! -d $HISTDIR/${LOGNAME} ]thenmkdir -p $HISTDIR/${LOGNAME}chmod 300 $HISTDIR/${LOGNAME}fiexport HISTSIZE=2000DT=`date +%Y%m%d_%H%M%S`export HISTFILE="$HISTDIR/${LOGNAME}/${USER_IP}.history.$DT"export HISTTIMEFORMAT="[%Y.%m.%d %H:%M:%S] "chmod 600 $HISTDIR/${LOGNAME}/*.history* 2>/dev/null ulimit -SHn 65535ulimit -SHu unlimitedulimit -SHd unlimitedulimit -SHm unlimitedulimit -SHs unlimitedulimit -SHt unlimitedulimit -SHv unlimitedEOFsource /etc/profile.d/system-audit.shformat###show the system infoecho "Set login message."echo "This is Product Server" > /etc/issueformat###iptables settingecho "set iptables"systemctl stop firewalldsystemctl disable firewalldformat#iptables -F#iptables -A INPUT -p tcp -m multiport --dports $SSH_PORT,$PORTS -j ACCEPT#iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT#iptables -A INPUT -i lo -j ACCEPT#iptables -A OUTPUT -m state --state NEW,ESTABLISHED -j ACCEPT#iptables -P INPUT DROP#iptables -P FORWARD DROP#iptables -P OUTPUT ACCEPT#service iptables save &> /dev/null# reboot the system after settingreboot原文:https://www.cnblogs.com/chenjunwu/p/11896419.html