操作系统配置基线
首先系统应最小化安装Server LTS(服务器长期支持版)版本
通用配置
hostName
hostnamectl set-hostname $newname
#修改或添加/etc/hosts
127.0.0.1 $newname
#修改或添加/etc/sysconfig/network
HOSTNAME=$newname
字符集 & 语言
#修改 /etc/locale.conf
LANG="zh_CN.UTF-8"
SUPPORTED="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
时区
timedatectl set-timezone Asia/Shanghai
NTP(时间同步)
#如果通外网使用公共的ntp,如果不通建议在内部安装一个ntp服务
ntpdate ntp.aliyun.com
echo '*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com > /dev/null 2>&1' >> /var/spool/cron/root
SSH配置
#修改/etc/ssh/sshd_config
#关闭UseDNS和GSSAPIAuthentication
UseDNS no
GSSAPIAuthentication no
禁用ctrl+alt+del重启
rm -rf /usr/lib/systemd/system/ctrl-alt-del.target
关防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
关非必要服务
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
内核优化
#修改 /etc/sysctl.conf
#关闭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
#修改生效
/sbin/sysctl -p
yum更新与安装必要工具
#可连通外网的情况下
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
#设置
cat /sys/block/sda/queue/scheduler
echo noop>/sys/block/sda/queue/scheduler
cat /sys/block/sda/queue/scheduler
BBR算法开启
#开启BBR,需要内核4.9以上
echo 'net.core.default_qdisc=fq' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_congestion_control=bbr' >> /etc/sysctl.conf
swappiness设置
#编辑 /etc/sysctl.conf
#内存有足够空余,设置为0,不够的话按需来吧
#vm.swappiness = 10
关闭图形化界面
systemctl set-default multi-user.target
systemctl get-default
一键脚本
centos7一键脚本
#!/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=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' |head -1)
IP='127.0.0.1'
oldname=$(cat /etc/sysconfig/network | awk -F "=" '{print $2}' | sed -n 2p)
read -r -p "是否要修改主机名? [y/n]" input
case $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.conf
echo -n "#cat /etc/locale.conf"
cat /etc/locale.conf
echo "==========================================================="
echo "修改时区"
timedatectl set-timezone Asia/Shanghai
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
timedatectl |grep zone
echo "==========================================================="
service crond restart &> /dev/null
read -r -p "是否自定义NTP服务? 默认: ntp.aliyun.com [y/n]" input
case $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.target
echo "==========================================================="
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'
#设置history格式
export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [`whoami`] [`who am i|awk '{print $NF}'|sed -r 's#[()]##g'`]: "
#记录shell执行的每一条命令
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 status irqbalance.service
systemctl stop postfix.service
systemctl disable postfix.service
#systemctl status postfix.service
echo "==========================================================="
echo "关闭图形化界面"
systemctl set-default multi-user.target
systemctl get-default
echo "==========================================================="
echo "设置backspace为删除"
echo 'stty erase ^H' >> /etc/profile
echo "syntax on" >> /root/.vimrc
echo "==========================================================="
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升级内核
#!/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
#-----------------------------------------------------------
#内网的话手动下载对应的版本
#https://elrepo.org/linux/kernel/el7/x86_64/RPMS/
#这里选择ml(最新)版本,也可以选择lt(长期支持版)
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 一键脚本(没注释版本)
#!/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 "Adding docker images hosts ..."
#cat >> /etc/hosts << EOF
#10.0.0.178 images.qlteacher.com
#EOF
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/profile
cat >> /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

浙公网安备 33010602011771号