hostName 1 2 3 4 5 hostnamectl set-hostname $newname 127.0.0.1 $newname HOSTNAME=$newname
字符集 & 语言 1 2 3 4 LANG="zh_CN.UTF-8" SUPPORTED="en_US.UTF-8" SYSFONT="latarcyrheb-sun16"
时区 1 timedatectl set-timezone Asia/Shanghai
NTP(时间同步) 1 2 3 ntpdate ntp.aliyun.com echo '*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com > /dev/null 2>&1' >> /var/spool/cron/root
SSH配置 1 2 3 4 UseDNS no GSSAPIAuthentication no
禁用ctrl+alt+del重启 1 rm -rf /usr/lib/systemd/system/ctrl-alt-del.target
关防火墙 1 2 systemctl stop firewalld.service systemctl disable firewalld.service
关非必要服务 1 2 3 4 5 6 7 8 9 10 systemctl stop irqbalance.service systemctl disable irqbalance.service systemctl status irqbalance.service systemctl stop postfix.service systemctl disable postfix.service systemctl status postfix.service sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config setenforce 0
内核优化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 net.ipv4.neigh.default.gc_stale_time=120 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.all.arp_announce=2 net.ipv4.conf.lo.arp_announce=2 net.ipv4.icmp_echo_ignore_broadcasts = 1 net.ipv4.icmp_ignore_bogus_error_responses = 1 net.ipv4.ip_forward = 0 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.all.accept_source_route = 0 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.somaxconn = 262144 net.core.netdev_max_backlog = 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 = 0 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_fin_timeout = 1 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.tcp_keepalive_probes = 3 net.ipv4.tcp_keepalive_intvl = 15 net.ipv4.ip_local_port_range = 1024 65000 net.netfilter.nf_conntrack_max=655350 net.netfilter.nf_conntrack_tcp_timeout_established=1200 net.ipv4.conf.all.accept_redirects = 0 net.ipv4.conf.default.accept_redirects = 0 net.ipv4.conf.all.secure_redirects = 0 net.ipv4.conf.default.secure_redirects = 0 /sbin/sysctl -p
yum更新与安装必要工具 1 2 3 4 5 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo yum clean all yum makecache yum -y install lsof lrzsz vim lrzsz tree nmap nc sysstat man wget ntp gcc gcc-c++
按需配置 磁盘调度算法调整 一定不要使用默认的CFQ算法.SSD用noop/none,普通磁盘用Deadline
1 2 3 4 cat /sys/block/sda/queue/schedulerecho noop>/sys/block/sda/queue/schedulercat /sys/block/sda/queue/scheduler
BBR算法开启 1 2 3 echo 'net.core.default_qdisc=fq' >> /etc/sysctl.confecho 'net.ipv4.tcp_congestion_control=bbr' >> /etc/sysctl.conf
swappiness设置
关闭图形化界面 1 2 systemctl set-default multi-user.target systemctl get-default
一键脚本 centos7一键脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 #!/bin/bash platform=`uname -i` if [ $platform != 'x86_64' ]; then echo "this script is only for 64bit Opetating System!" exit 1 fi echo "the platform is ok" cat << EOF +---------------------------------------+ | your system is CentOS 7 x86_64 | | start optimizing....... | +---------------------------------------+ EOF IP='127.0.0.1' oldname=$(cat /etc/sysconfig/network | awk -F "=" '{print $2}' | sed -n 2p) read -r -p "是否要修改主机名? [y/n]" inputcase $input in y) read -r -p "请输入主机名:" newname hostnamectl set-hostname $newname sed -i '/${oldname}/d' /etc/hosts echo "$IP `hostname`" >> /etc/hosts sed -i '/HOSTNAME/d' /etc/sysconfig/network echo "HOSTNAME=$newname " >> /etc/sysconfig/network echo $oldname "改为" $newname ;; n) echo "当前hostname =" $oldname ;; esac echo "===========================================================" echo "设置字符集 ..." cat > /etc/locale.conf <<EOF LANG="zh_CN.UTF-8" SUPPORTED="en_US.UTF-8" SYSFONT="latarcyrheb-sun16" EOF source /etc/locale.confecho -n "#cat /etc/locale.conf" cat /etc/locale.confecho "===========================================================" echo "修改时区" timedatectl set-timezone Asia/Shanghai ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimetimedatectl |grep zone echo "===========================================================" service crond restart &> /dev/null read -r -p "是否自定义NTP服务? 默认: ntp.aliyun.com [y/n]" inputcase $input in y) read -r -p "请输入NTP:" newntp ntpdate ${newntp} echo '*/5 * * * * /usr/sbin/ntpdate' $newntp '> /dev/null 2>&1' >> /var/spool/cron/root ;; n) ntpdate ntp.aliyun.com echo '*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com > /dev/null 2>&1' >> /var/spool/cron/root ;; esac echo "===========================================================" echo "ssh配置" sed -i '/#UseDNS.*/s@@UseDNS no@' /etc/ssh/sshd_config sed -i '/^GSSAPIAuthentication.*/s@\(GSSAPIAuthentication.*\)@#\1@' /etc/ssh/sshd_config service sshd restart &> /dev/null echo "===========================================================" echo "禁用ctrl+alt+del重启" rm -rf /usr/lib/systemd/system/ctrl-alt-del.targetecho "===========================================================" echo "关防火墙" systemctl stop firewalld.service &> /dev/null firewall-cmd --state systemctl disable firewalld.service &> /dev/null systemctl list-unit-files | grep firewalld echo "===========================================================" echo "history优化" chk_his=`cat /etc/profile | grep HISTTIMEFORMAT |wc -l` if [ $chk_his -eq 0 ];then cat >> /etc/profile <<'EOF' export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [`whoami`] [`who am i|awk '{print $NF }'|sed -r 's#[()]##g'`]: " export PROMPT_COMMAND='\ if [ -z "$OLD_PWD" ];then export OLD_PWD=$PWD; fi; if [ ! -z "$LAST_CMD" ] && [ "$(history 1)" != "$LAST_CMD" ]; then logger -t `whoami`_shell_dir "[$OLD_PWD]$(history 1)"; fi; export LAST_CMD="$(history 1)"; export OLD_PWD=$PWD;' EOF source /etc/profile else echo "优化项已存在。" fi echo "===========================================================" echo "打开文件句柄数" echo "ulimit -SHn 65535" >> /etc/rc.local cat >> /etc/security/limits.conf << EOF * soft nofile 65535 * hard nofile 65535 EOF echo "===========================================================" echo "关闭 selinux" sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config setenforce 0 echo "===========================================================" echo "关闭非必要服务" systemctl stop irqbalance.service systemctl disable irqbalance.service systemctl stop postfix.service systemctl disable postfix.service echo "===========================================================" echo "关闭图形化界面" systemctl set-default multi-user.target systemctl get-default echo "===========================================================" echo "设置backspace为删除" echo 'stty erase ^H' >> /etc/profileecho "syntax on" >> /root/.vimrcecho "===========================================================" echo "内核优化" cat >> /etc/sysctl.conf << EOF #关闭ipv6 net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 #决定检查过期多久邻居条目 net.ipv4.neigh.default.gc_stale_time=120 #使用arp_announce / arp_ignore解决ARP映射问题 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.all.arp_announce=2 net.ipv4.conf.lo.arp_announce=2 # 避免放大攻击 net.ipv4.icmp_echo_ignore_broadcasts = 1 # 开启恶意icmp错误消息保护 net.ipv4.icmp_ignore_bogus_error_responses = 1 #关闭路由转发 net.ipv4.ip_forward = 0 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 #开启反向路径过滤 net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.default.rp_filter = 1 #处理无源路由的包 net.ipv4.conf.all.accept_source_route = 0 net.ipv4.conf.default.accept_source_route = 0 #关闭sysrq功能 kernel.sysrq = 0 #core文件名中添加pid作为扩展名 kernel.core_uses_pid = 1 # 开启SYN洪水攻击保护 net.ipv4.tcp_syncookies = 1 #修改消息队列长度 kernel.msgmnb = 65536 kernel.msgmax = 65536 #设置最大内存共享段大小bytes kernel.shmmax = 68719476736 kernel.shmall = 4294967296 #timewait的数量,默认180000 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 #socket监听队列上线监听队列 net.core.somaxconn = 262144 #每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目 net.core.netdev_max_backlog = 262144 #限制仅仅是为了防止简单的DoS 攻击 net.ipv4.tcp_max_orphans = 3276800 #未收到客户端确认信息的连接请求的最大值 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_timestamps = 0 #内核放弃建立连接之前发送SYNACK 包的数量 net.ipv4.tcp_synack_retries = 1 #内核放弃建立连接之前发送SYN 包的数量 net.ipv4.tcp_syn_retries = 1 #启用timewait 快速回收 net.ipv4.tcp_tw_recycle = 0 #开启重用。允许将TIME-WAIT sockets 重新用于新的TCP 连接 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_fin_timeout = 1 #当keepalive 起用的时候,TCP 发送keepalive 消息的频度。缺省是2 小时 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.tcp_keepalive_probes = 3 net.ipv4.tcp_keepalive_intvl = 15 #允许系统打开的端口范围 net.ipv4.ip_local_port_range = 1024 65000 #修改防火墙表大小,默认65536 net.netfilter.nf_conntrack_max=655350 net.netfilter.nf_conntrack_tcp_timeout_established=1200 # 确保无人能修改路由表 net.ipv4.conf.all.accept_redirects = 0 net.ipv4.conf.default.accept_redirects = 0 net.ipv4.conf.all.secure_redirects = 0 net.ipv4.conf.default.secure_redirects = 0 EOF /sbin/sysctl -p &> /dev/null echo "===========================================================" echo "关闭IPV6" cat > /etc/modprobe.d/ipv6.conf << EOF alias net-pf-10 off options ipv6 disable=1 EOF echo "NETWORKING_IPV6=off" >> /etc/sysconfig/network echo "===========================================================" echo "yum更新阿里源与安装必要工具" cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.$(date +%F) ping -c 1 mirrors.aliyun.com &> /dev/null if [ $? -eq 0 ];then wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null yum clean all &> /dev/null yum makecache &> /dev/null yum -y install lsof lrzsz vim lrzsz tree nmap nc sysstat man wget ntp gcc gcc-c++ &> /dev/null else echo "无法连接外网 放弃yum更新" fi echo "===========================================================" cat << EOF +-------------------------------------------------+ | optimizer is done | | it's recommond to restart this server ! | +-------------------------------------------------+ EOF
centos7升级内核 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #!/bin/bash rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm yum --enablerepo=elrepo-kernel install kernel-ml -y wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-ml-5.9.12-1.el7.elrepo.x86_64.rpm 安装 rpm -ivh kernel-ml-5.9.12-1.el7.elrepo.x86_64.rpm grub2-set-default 0 && shutdown -r now uname -r
Ubuntu20 一键脚本(没注释版本) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 #!/bin/bash platform=`lsb_release -r --short` if [[ $platform != '20.04' ]]; then echo "this script is only for Ubuntu 20.04 LTS!" exit 1 fi echo "System Check PASS!" cat << EOF +---------------------------------------+ | Your System is Ubuntu 20.04 LTS | | start optimizing....... | +--------------------------------------- EOF echo -n "Installing some useful Packages ..." apt-get update -y apt-get upgrade -y apt-get install -y conntrack ipvsadm ipset jq iptables curl sysstat lrzsz apt-transport-https ca-certificates software-properties-common gnupg-agent net-tools echo "Done." echo -n "Installing docker-ce Packages" curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" apt-get update -y apt-get remove -y docker docker-engine docker.io containerd runc apt-get install -y docker-ce sed -i 's/GRUB_CMDLINE_LINUX=\"\"/GRUB_CMDLINE_LINUX=\"cgroup_enable=memory swapaccount=1\"/g' /etc/default/grub update-grub echo "Done." echo -n "Adding docker-ce Configuretion ..." cat >> /etc/docker/daemon.json << EOF { "registry-mirrors": [ "https://docker.mirrors.ustc.edu.cn", "https://dockerhub.mirrors.nwafu.edu.cn", "https://hub-mirror.c.163.com" ], "dns": [ "114.114.114.114" ], "max-concurrent-downloads": 5, "max-concurrent-uploads": 5, "live-restore": true } EOF echo "Done." echo -n "Adjusting timezone to Asia Shanghai ..." /usr/bin/timedatectl set-timezone "Asia/Shanghai" /usr/bin/timedatectl set-ntp true echo "Done." echo -n "Adding ulimits Configuretion ..." echo "ulimit -HSn 102400" |sudo tee -a /etc/profilecat >> /etc/security/limits.conf << EOF * soft nofile 102400 * hard nofile 102400 root soft nofile 102400 root hard nofile 102400 EOF echo "Done." echo -n "Adding sysctl Configuretion ..." cat >> /etc/sysctl.conf << EOF #Net Optimazation net.ipv4.ip_local_port_range = 1024 65535 net.ipv4.ip_forward = 1 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_timestamps = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_slow_start_after_idle = 0 net.ipv4.tcp_low_latency = 1 net.ipv4.tcp_max_tw_buckets = 262144 net.ipv4.tcp_max_orphans = 327680 net.ipv4.tcp_max_syn_backlog = 819200 net.ipv4.tcp_keepalive_intvl = 30 net.ipv4.tcp_keepalive_probes = 3 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 16384 16777216 net.ipv4.tcp_thin_linear_timeouts = 1 net.core.somaxconn = 65536 net.core.rmem_default = 8388608 net.core.wmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.unix.max_dgram_qlen = 30000 net.netfilter.nf_conntrack_max = 1048576 net.ipv4.neigh.default.gc_thresh1 = 10240 net.ipv4.neigh.default.gc_thresh2 = 40960 net.ipv4.neigh.default.gc_thresh3 = 61440 net.bridge.bridge-nf-call-iptables = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-arptables = 1 net.core.netdev_max_backlog = 16384 #Kernel Optimazation kernel.panic = 1 kernel.core_pattern = core_%e #VM Optimazation vm.panic_on_oom = 0 vm.min_free_kbytes = 512000 vm.swappiness = 20 #FS Optimazation fs.file-max = 52706963 fs.nr_open = 52706963 fs.inotify.max_user_instances = 8192 fs.inotify.max_user_watches = 1048576 fs.aio-max-nr = 1048576 #HUGE PAGES vm.nr_hugepages=128 #BBR net.core.default_qdisc=fq net.ipv4.tcp_congestion_control=bbr EOF /sbin/sysctl -p &> /dev/null echo "Done." cat << EOF +-------------------------------------------------+ | This Server has been Optimizd | | Please restart this server ! | +-------------------------------------------------+ EOF