|
| 1 | +#!/bin/bash |
| 2 | +# ------------------------------------------ |
| 3 | +# Filename:yjianyouhua.sh |
| 4 | +# Revision: 1.0 |
| 5 | +# Date: 20170810 |
| 6 | +# Author: |
| 7 | +# Email: |
| 8 | +# Website: |
| 9 | +# Description:一键优化脚本 |
| 10 | +# ------------------------------------------ |
| 11 | +#"***********************脚本内容如下*********************" |
| 12 | +#定义变量 |
| 13 | +platform=`uname -i` |
| 14 | + |
| 15 | + |
| 16 | +#判断当前用户是否为root |
| 17 | +if [[ "$(whoami)" != "root" ]];then |
| 18 | + echo "Please run this script as root ." |
| 19 | + exit 1 |
| 20 | +fi |
| 21 | +#判断当前主机是否为64位 |
| 22 | +if [ $platform != "x86_64" ];then |
| 23 | + echo "This script is only for 64bit system!" |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | +echo "This platform is ok !" |
| 27 | +#创建目录 |
| 28 | +mkdir -p /app /date |
| 29 | +#备份并添加国内YUM源 |
| 30 | +mkdir -p /etc/yum.repos.d/old && mv -a /etc/yum.repos.d/* /etc/yum.repos.d/old/ |
| 31 | +if [ ! -f /etc/yum.repos.d/cobbler-config.repo ];then |
| 32 | + wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo |
| 33 | + wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo |
| 34 | + rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm |
| 35 | +fi |
| 36 | + |
| 37 | +sed -i "s/enabled=0/enabled=1/g" /etc/yum.repos.d/cobbler-config.repo |
| 38 | + |
| 39 | +#清空并重建缓存 |
| 40 | +yum clean all |
| 41 | +yum makecache |
| 42 | +#升级并安装必要软件 |
| 43 | +yum -y update |
| 44 | +yum -y upgrade |
| 45 | +yum -y install gcc gcc-c++ ntp lrzsz tree telnet dos2unix sysstat sysstat iptraf ncurses-devel openssl-devel zlib-devel OpenIPMI-tools nmap screen pstree |
| 46 | + |
| 47 | + |
| 48 | +#更新系统时间 |
| 49 | +echo "* 4 * * * /usr/sbin/ntpdate 202.120.2.101 > /dev/null 2>&1" >> /var/spool/cron/root |
| 50 | +systemctl restart crond |
| 51 | + |
| 52 | + |
| 53 | +#设置系统默认语言支持 |
| 54 | +localectl set-locale LANG=en_US.utf8 |
| 55 | + |
| 56 | + |
| 57 | +#添加系统用户 |
| 58 | + |
| 59 | + |
| 60 | +#sudo权限管理 |
| 61 | + |
| 62 | + |
| 63 | +#设置文件保护 |
| 64 | +#cat >> /etc/security/limits.conf << EOF |
| 65 | +#* soft nofile 65535 |
| 66 | +#* hard nofile 65535 |
| 67 | +#EOF |
| 68 | + |
| 69 | +#关闭SElinux |
| 70 | + sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config |
| 71 | + |
| 72 | + |
| 73 | +#限制root用户远程SSH连接 |
| 74 | +#cp /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +"%F %T"` |
| 75 | +#sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config |
| 76 | +#sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config |
| 77 | +#sed -i 's%#PermitRootLogin yes%PermitRootLogin no%g' /etc/ssh/sshd_config |
| 78 | +#sed -i 's%#PermitEmptyPasswords no%PermitEmptyPasswords no%g' /etc/ssh/sshd_config |
| 79 | +#sed -i 's%#Port 22%Port 52020%g' /etc/ssh/sshd_config |
| 80 | +#systemctl restart sshd |
| 81 | +#systemctl enable sshd |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +#调整内核参数 |
| 86 | +cat >> /etc/sysctl.conf << EOF |
| 87 | +net.ipv4.tcp_tw_recycle = 1 |
| 88 | +net.ipv4.tcp_tw_reuse = 1 |
| 89 | +net.ipv4.ip_local_port_range = 10000 65000 |
| 90 | +net.ipv4.tcp_syncookies = 1 |
| 91 | +net.ipv4.tcp_max_tw_buckets = 36000 |
| 92 | +net.ipv4.tcp_max_syn_backlog = 16384 |
| 93 | +net.ipv4.tcp_keepalive_time = 600 |
| 94 | +net.ipv4.tcp_fin_timeout = 30 |
| 95 | +vm.swappiness=10 |
| 96 | +vm.max_map_count = 262144 |
| 97 | +#关闭ipv6 |
| 98 | +net.ipv6.conf.all.disable_ipv6 = 1 |
| 99 | +net.ipv6.conf.default.disable_ipv6 = 1 |
| 100 | +#决定检查过期多久邻居条目 |
| 101 | +net.ipv4.neigh.default.gc_stale_time=120 |
| 102 | +#使用arp_announce / arp_ignore解决ARP映射问题 |
| 103 | +net.ipv4.conf.default.arp_announce = 2 |
| 104 | +net.ipv4.conf.all.arp_announce=2 |
| 105 | +net.ipv4.conf.lo.arp_announce=2 |
| 106 | +# 避免放大攻击 |
| 107 | +net.ipv4.icmp_echo_ignore_broadcasts = 1 |
| 108 | +# 开启恶意icmp错误消息保护 |
| 109 | +net.ipv4.icmp_ignore_bogus_error_responses = 1 |
| 110 | +#关闭路由转发 |
| 111 | +net.ipv4.ip_forward = 0 |
| 112 | +net.ipv4.conf.all.send_redirects = 0 |
| 113 | +net.ipv4.conf.default.send_redirects = 0 |
| 114 | +#开启反向路径过滤 |
| 115 | +net.ipv4.conf.all.rp_filter = 1 |
| 116 | +net.ipv4.conf.default.rp_filter = 1 |
| 117 | +#处理无源路由的包 |
| 118 | +net.ipv4.conf.all.accept_source_route = 0 |
| 119 | +net.ipv4.conf.default.accept_source_route = 0 |
| 120 | +#关闭sysrq功能 |
| 121 | +kernel.sysrq = 0 |
| 122 | +#core文件名中添加pid作为扩展名 |
| 123 | +kernel.core_uses_pid = 1 |
| 124 | +# 开启SYN洪水攻击保护 |
| 125 | +net.ipv4.tcp_syncookies = 1 |
| 126 | +#修改消息队列长度 |
| 127 | +kernel.msgmnb = 65536 |
| 128 | +kernel.msgmax = 65536 |
| 129 | +#设置最大内存共享段大小bytes |
| 130 | +kernel.shmmax = 68719476736 |
| 131 | +kernel.shmall = 4294967296 |
| 132 | +#timewait的数量,默认180000 |
| 133 | +net.ipv4.tcp_max_tw_buckets = 6000 |
| 134 | +net.ipv4.tcp_sack = 1 |
| 135 | +net.ipv4.tcp_window_scaling = 1 |
| 136 | +net.ipv4.tcp_rmem = 4096 87380 4194304 |
| 137 | +net.ipv4.tcp_wmem = 4096 16384 4194304 |
| 138 | +net.core.wmem_default = 8388608 |
| 139 | +net.core.rmem_default = 8388608 |
| 140 | +net.core.rmem_max = 16777216 |
| 141 | +net.core.wmem_max = 16777216 |
| 142 | +#每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目 |
| 143 | +net.core.netdev_max_backlog = 262144 |
| 144 | +#限制仅仅是为了防止简单的DoS 攻击 |
| 145 | +net.ipv4.tcp_max_orphans = 3276800 |
| 146 | +#未收到客户端确认信息的连接请求的最大值 |
| 147 | +net.ipv4.tcp_max_syn_backlog = 262144 |
| 148 | +net.ipv4.tcp_timestamps = 0 |
| 149 | +#内核放弃建立连接之前发送SYNACK 包的数量 |
| 150 | +net.ipv4.tcp_synack_retries = 1 |
| 151 | +#内核放弃建立连接之前发送SYN 包的数量 |
| 152 | +net.ipv4.tcp_syn_retries = 1 |
| 153 | +#启用timewait 快速回收 |
| 154 | +net.ipv4.tcp_tw_recycle = 1 |
| 155 | +#开启重用。允许将TIME-WAIT sockets 重新用于新的TCP 连接 |
| 156 | +net.ipv4.tcp_tw_reuse = 1 |
| 157 | +net.ipv4.tcp_mem = 94500000 915000000 927000000 |
| 158 | +net.ipv4.tcp_fin_timeout = 1 |
| 159 | +#当keepalive 起用的时候,TCP 发送keepalive 消息的频度。缺省是2 小时 |
| 160 | +net.ipv4.tcp_keepalive_time = 1800 |
| 161 | +net.ipv4.tcp_keepalive_probes = 3 |
| 162 | +net.ipv4.tcp_keepalive_intvl = 15 |
| 163 | +#允许系统打开的端口范围 |
| 164 | +net.ipv4.ip_local_port_range = 1024 65000 |
| 165 | +#修改防火墙表大小,默认65536 |
| 166 | +net.netfilter.nf_conntrack_max=655350 |
| 167 | +net.netfilter.nf_conntrack_tcp_timeout_established=1200 |
| 168 | +# 确保无人能修改路由表 |
| 169 | +net.ipv4.conf.all.accept_redirects = 0 |
| 170 | +net.ipv4.conf.default.accept_redirects = 0 |
| 171 | +net.ipv4.conf.all.secure_redirects = 0 |
| 172 | +net.ipv4.conf.default.secure_redirects = 0 |
| 173 | +EOF |
| 174 | +#从指定文件加载系统参数 |
| 175 | +/sbin/sysctl -p |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +#禁用防火墙 |
| 180 | +#systemctl stop firwalld |
| 181 | +#systemctl disable firwalld |
| 182 | + |
| 183 | + |
| 184 | +#设置主机名 |
| 185 | +#hostnamectl set-hostname admin |
| 186 | + |
| 187 | + |
| 188 | +#禁用ipv6 |
| 189 | +#cat > /etc/modprobe.d/ipv6.conf << EOF |
| 190 | +#alias net-pf-10 off |
| 191 | +#options ipv6 disable=1 |
| 192 | +#EOF |
| 193 | +#echo "NETWORKING_IPV6=off" >> /etc/sysconfig/network |
| 194 | + |
| 195 | +#定制登录提示符 |
| 196 | +#modify PS1 |
| 197 | +echo 'export PS1="[ \033[01;33m\u\033[0;36m@\033[01;34m\h \033[01;31m\w\033[0m ]\033[0m \n#"' >> /etc/profile |
| 198 | +echo "the platform is ok" |
| 199 | + |
| 200 | + |
| 201 | +#定制vim配置 |
| 202 | +#modify vimrc |
| 203 | +cat >> /root/.vimrc << EOF |
| 204 | +syntax enable |
| 205 | +syntax on |
| 206 | +set ruler |
| 207 | +set number |
| 208 | +set cursorline |
| 209 | +set cursorcolumn |
| 210 | +set hlsearch |
| 211 | +set incsearch |
| 212 | +set ignorecase |
| 213 | +set nocompatible |
| 214 | +set wildmenu |
| 215 | +set paste |
| 216 | +set nowrap |
| 217 | +set expandtab |
| 218 | +set tabstop=2 |
| 219 | +set shiftwidth=4 |
| 220 | +set softtabstop=4 |
| 221 | +set gcr=a:block-blinkon0 |
| 222 | +set guioptions-=l |
| 223 | +set guioptions-=L |
| 224 | +set guioptions-=r |
| 225 | +set guioptions-=R |
| 226 | +hi CursorColumn cterm=NONE ctermbg=darkred ctermfg=white |
| 227 | +EOF |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | +#重启生效 |
| 234 | +reboot |
| 235 | + |
| 236 | + |
| 237 | + |
| 238 | + |
| 239 | + |
| 240 | + |
| 241 | + |
| 242 | + |
| 243 | + |
| 244 | + |
| 245 | + |
0 commit comments